Assignment 5
Assignment 5
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: