0% found this document useful (0 votes)
10 views17 pages

Assignment 5

The document consists of a programming assignment where the student, Ramkrishna Patidar, is tasked with debugging several pieces of incorrect C code related to factorial calculation, pointer usage, and memory management. Each section includes wrong code, wrong output, debugging notes, and space for corrected output and code. The assignment emphasizes the importance of debugging and correcting code to achieve the desired functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views17 pages

Assignment 5

The document consists of a programming assignment where the student, Ramkrishna Patidar, is tasked with debugging several pieces of incorrect C code related to factorial calculation, pointer usage, and memory management. Each section includes wrong code, wrong output, debugging notes, and space for corrected output and code. The assignment emphasizes the importance of debugging and correcting code to achieve the desired functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

PP Assignment 5

Name : Ramkrishna Patidar


Enrollment number : 0801CS211073
Compile the given code and check whether output of the following program is
correct or not , if not debug it using gdb and correct the code accordingly.
Q1.

Wrong Code :
#include <stdio.h>
#include <stdlib.h>
int factorial(int n);
int main(void) {
int n = 5;
int f = factorial(n);
printf("The factorial of %d is %d.\n", n, f);
n = 17;
f = factorial(n);
printf("The factorial of %d is %d.\n", n, f);
return 0;
}
//A factorial is calculated by n! = n * (n - 1) * (n - 2) * ... * 1
//E.g. 5! = 5 * 4 * 3 * 2 * 1 = 120
int factorial(int n) {
int f = 1;
int i = 1;
while (i <= n) {
f = f * i;
i++;
}
return f;
}
Wrong Output :
Debugging :
Coorected Output :

Corrected Code :
Q2.
Wrong Code :
int main()
{
int* p = NULL;
*p = 1;
cout << *p;
return 0;
}
Wrong Output :

Debbuging :
Corrected Output :

Corrected Code :
Q3)
Wrong Code:
#include <stdio.h>
int main()
{
int n = 2;
scanf(" ",n);
return 0;
}
Wrong Output :
Debugging:

Correct Output:

Correct Code:
Q4)
Wrong Code:
#include <stdio.h>
int main()
{
int *p;
printf("%d",*p);
return 0;
}
Wrong Output :

Debugging:
Correct Output:

Correct Code:
Wrong Code :
Q5)
#include <stdio.h>
int main(void)
{
int arr[2];
arr[3] = 10; // Accessing out of bound
return (0);
}
Wrong Output:

Debugging:
Correct Output:
Correct Code:

Wrong Code:
Q6
#include <stdio.h>
#include<alloc.h>
int main(void)
{
// allocating memory to p
int* p = malloc(8);
*p = 100;
// deallocated the space allocated to p
free(p);
*p = 110;
return 0;
}

Wrong Output:

Debugging:
Correct Code:

You might also like