0% found this document useful (0 votes)
5 views

Debugging Contest Question Set

The document outlines several coding problems related to array handling in C, including indexing mistakes, memory overwrite issues, pointer arithmetic errors, and improper array allocation. Each problem includes a code snippet and a task to identify and fix the errors. The document serves as a practical exercise for students in the Computer Science and Engineering department at Dr. Sivanti Aditanar College of Engineering.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Debugging Contest Question Set

The document outlines several coding problems related to array handling in C, including indexing mistakes, memory overwrite issues, pointer arithmetic errors, and improper array allocation. Each problem includes a code snippet and a task to identify and fix the errors. The document serves as a practical exercise for students in the Computer Science and Engineering department at Dr. Sivanti Aditanar College of Engineering.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dr.

SIVANTHI ADITANAR COLLEGE OF ENGINEERING – TIRUCHENDUR


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
&
COMPUTER SOCIETY OF INDIA
SACOSIUM -2025
Coding Desenvolvimento

1. Array Indexing Mistake

Problem: You are given the following code that attempts to calculate the sum of the
elements in an array, but there’s an indexing bug.

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;

for (int i = 0; i <= sizeof(arr)/sizeof(arr[0]); i++) {


sum += arr[i];
}

printf("Sum of array elements: %d\n", sum);


return 0;
}

Task: Identify and fix the bug in the code.

2. Memory Overwrite with Arrays

Problem: The following code snippet aims to copy an array of integers into another array.
However, due to improper handling of the array size, it causes a memory overwrite issue.

#include <stdio.h>
#include <string.h>

void copy_array(int* source, int* dest, int n) {


for (int i = 0; i < n; i++) {
dest[i] = source[i];
}
}

int main() {
int source[] = {10, 20, 30, 40, 50};
int dest[4];
copy_array(source, dest, 5);

for (int i = 0; i < 4; i++) {


printf("%d ", dest[i]);
}
printf("\n");

return 0;
}

Task: Find and fix the issue that causes an out-of-bounds write and potential memory
corruption.

3. Pointer Arithmetic Error

Problem: The following program attempts to reverse an array using pointer arithmetic.
However, due to a logical error, the reversal does not occur correctly.

#include <stdio.h>

void reverse_array(int *arr, int n) {


int *start = arr;
int *end = arr + n - 1;

while (start < end) {


int temp = *start;
*start = *end;
*end = temp;

start++;
end--;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;

reverse_array(arr, n);

for (int i = 0; i < n; i++) {


printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Task: Find the logical error in the array reversal function and correct it.

4. Array Allocation and Pointer Dereferencing Issue


Problem: In the following code, memory for the array arr is dynamically allocated.
However, there's a mistake in how the array elements are accessed, leading to undefined
behavior.

#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr;
int size = 5;

arr = (int*) malloc(size * sizeof(int));

for (int i = 1; i < size; i++) { // Bug: starts from index 1 instead
of 0
arr[i] = i * 10;
}

for (int i = 0; i < size; i++) {


printf("%d ", arr[i]);
}
printf("\n");

free(arr);

return 0;
}

Task: Identify the mistake in array indexing and suggest the correct approach to fix the code.

5. Array Bounds with Negative Indexing

Problem: The following code is supposed to find the maximum and minimum values in an
array. However, it contains an error that leads to undefined behavior due to invalid indexing.

#include <stdio.h>

void find_max_min(int arr[], int n) {


int max = arr[0];
int min = arr[0];

for (int i = -1; i < n; i++) { // Bug: invalid starting index


if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}

printf("Max: %d, Min: %d\n", max, min);


}

int main() {
int arr[] = {4, 9, 2, 7, 5};
int n = sizeof(arr) / sizeof(arr[0]);

find_max_min(arr, n);

return 0;
}

Task: Find the issue with array indexing in the find_max_min function and fix it.

You might also like