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

CodeFix C Programs

Uploaded by

samabishek75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CodeFix C Programs

Uploaded by

samabishek75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Debugging

1)Program with Bugs

#include <stdio.h>

void swap(int a, int b) {


int temp;
temp = a;
a = b;
b = temp;
}

int main() {
int x = 5, y = 10;

swap(x, y);

printf("x = %d, y = %d\n", x, y);

return 0;
}

Corrected Program

#include <stdio.h>

void swap(int *a, int *b) { // Use pointers to modify the original variables
int temp;
temp = *a; // Dereference pointers to access the values
*a = *b;
*b = temp;
}

int main() {
int x = 5, y = 10;

swap(&x, &y); // Pass the addresses of x and y to swap

printf("x = %d, y = %d\n", x, y); // Now prints swapped values

return 0;
}

Expected output:
x = 10, y = 5

=== Code Execution Successful ===

2) Program with Bugs

#include <stdio.h>

void printArray(int arr[], int size) {


for (int i = 0; i <= size; i++) { // Bug #1
printf("%d ", arr[i]); // Bug #2
}
}

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

printArray(arr, size);

arr[5] = 10; // Bug #3


printf("Updated value at index 5: %f\n", arr[5]);

return 0;
}

Corrected Program

#include <stdio.h>

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++) { // Bug #1 fixed: Change i <= size to i < size
printf("%d ", arr[i]); // Bug #2 fixed: This now prints valid array indices
}
printf("\n");
}

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

printArray(arr, size);

if (size > 5) { // Bug #3 fixed: Bounds checking before writing to arr[5]


arr[5] = 10;
printf("Updated value at index 5: %d\n", arr[5]);
} else {
printf("Index 5 is out of bounds\n");
}

return 0;
}

Expected output:
12345
Index 5 is out of bounds

=== Code Execution Successful ===

3)Program with Bugs

#include <stdio.h>

void addNumbers(int a, int b) {


int sum;
sum = a + b;
printf("Sum: %f\n", sum);
}

int main() {
int x, y;

printf("Enter two numbers: ");


scanf("%d %d", x, &y); // Bug #1

addNumbers(x, y);

printf("The sum of %d and %d is %d\n", x, y, sum); // Bug #2

return 0;
}

Corrected Program

#include <stdio.h>

int addNumbers(int a, int b) {


int sum;
sum = a + b;
return sum; // Fixed Bug #2 and Bug #3: Return the sum
}

int main() {
int x, y;

printf("Enter two numbers: ");


if (scanf("%d %d", &x, &y) != 2) { // Fixed Bug #1 and Bug #4: Correct scanf usage and
error check
printf("Invalid input\n");
return 1; // Exit the program if input is invalid
}

int result = addNumbers(x, y); // Call function and store the result
printf("The sum of %d and %d is %d\n", x, y, result); // Print the result

return 0;
}

Expected Output:

Enter two numbers: 5


5
The sum of 5 and 5 is 10

=== Code Execution Successful ===

4) Program with Bugs

#include <stdio.h>

#define MAX_SIZE 10
void fillArray(int arr[], int size) {
for (int i = 0; i <= size; i++) { // Bug 1: Off-by-one error
printf("Enter element %d: ", i);
scanf("%d", &arr[i]); // This can cause a buffer overflow on the last iteration
}
}

void displayArray(int arr[], int size) {


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int numbers[MAX_SIZE];

fillArray(numbers, MAX_SIZE); // Correct call to fill the array

displayArray(numbers, MAX_SIZE); // Correct call to display the array

int sum = 0;
for (int i = 0; i < MAX_SIZE; i++) {
sum += numbers[i]; // Bug 2: Should check for overflow, but this is not causing a crash
}

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

float average; // Bug 3: Average is not initialized


average = sum / MAX_SIZE; // Bug 4: Integer division may lead to incorrect average if
sum is less than MAX_SIZE

printf("Average: %.2f\n", average); // Potentially incorrect average due to integer division

return 0;
}

Corrected Program

#include <stdio.h>

#define MAX_SIZE 10

void fillArray(int arr[], int size) {


for (int i = 0; i < size; i++) { // Fixed: Changed <= to <
printf("Enter element %d: ", i);
scanf("%d", &arr[i]); // No longer causes a buffer overflow
}
}

void displayArray(int arr[], int size) {


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int numbers[MAX_SIZE];

fillArray(numbers, MAX_SIZE); // Correct call to fill the array

displayArray(numbers, MAX_SIZE); // Correct call to display the array

int sum = 0;
for (int i = 0; i < MAX_SIZE; i++) {
sum += numbers[i]; // No change needed here, but consider overflow checks in a real
application
}

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

float average; // Average variable declared


average = (float)sum / MAX_SIZE; // Fixed: Cast sum to float for correct average
calculation

printf("Average: %.2f\n", average); // Correctly displays the average

return 0;
}

Expected Output:

Enter element 0: 5
Enter element 1: 10
Enter element 2: 15
Enter element 3: 20
Enter element 4: 25
Enter element 5: 30
Enter element 6: 35
Enter element 7: 40
Enter element 8: 45
Enter element 9: 50
5 10 15 20 25 30 35 40 45 50
Sum: 275
Average: 27.50

=== Code Execution Successful ===

5) Program with Bugs

#include <stdio.h>

int factorial(int n) {
if (n < 0) // Bug 1: No check for negative input
return -1; // Should handle negative input properly
if (n == 0)
return 1;
return n * factorial(n - 1); // Bug 2: Potential stack overflow for large n
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", num); // Bug 3: scanf should use &num
int result = factorial(num); // Bug 4: No check for invalid result from factorial
printf("Factorial of %d is %d\n", num, result);
return 0;
}

Corrected Program

#include <stdio.h>

int factorial(int n) {
if (n < 0) { // Fixed: Check for negative input
printf("Error: Factorial is not defined for negative numbers.\n");
return -1; // Return error code
}
if (n == 0)
return 1;
return n * factorial(n - 1); // Still recursive, but can be improved for large n
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num); // Fixed: Added & to scanf
int result = factorial(num); // Call factorial function
if (result != -1) { // Fixed: Check for invalid result
printf("Factorial of %d is %d\n", num, result);
}
return 0;
}

Expected Output:

Enter a number: 5
Factorial of 5 is 120

=== Code Execution Successful ===

You might also like