For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i <= 10; ++i) { buf[i] = i; } for (i = 0; i <= 10; ++i) { printf("Index %s = %s\n", i, buf[i]); } /* Error: */ // P2 str = malloc(sizeof(char) * 10); strcpy(str, "Something is wrong"); printf("%s\n", printThisOne); /* Error: */ // P3 word = "Part 3"; *(word + 4) = '-'; printf("%s\n", word); /* Error: */ // P4 *(integers + 10) = 10; printf("Part 4: %d\n", *(integers + 10)); free(integers); /* Error: */ // P5 printf("Print this whole \0 line\n"); /* Error: */ // P6 x = 2147483647; printf("%d is positive\n", x); x += 1000000000; printf("%d is positive\n", x); /* Error: */ // P7 printf("Cleaning up memory from previous parts\n"); free(str); free(buf); /* Error: */ // P8 fib(foo, 7); printf("fib(7) = %d\n", foo); /* Error: */ // P9 bar = 0; *bar = 123; printf("bar = %d\n", *bar); /* Error: */ // P10 someText = malloc(10); strcpy(someText, "testing"); free(someText); printf("someText = %s\n", someText); /* Error: */ exit(0); } // fib calculates the nth fibonacci number and puts it in A. // There is nothing wrong with this function. void fib(int *A, int n) { int temp; if (n == 0 || n == 1) *A = 1; else { fib(A, n - 1); temp = *A; fib(A, n - 2); *A += temp; } }
i have written the reason after every error between /*Error */ don't forget to declare temp variables in starting of main function
// P0
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
/* Error:
use header file string.h
*/
void fib(int* A, int n);
int main(int argc, char *argv[]) {
int buf[10];
unsigned int i,x;
char *str;
char *printThisOne;
char *word;
int *integers;
int foo;
int *bar;
char *someText;
// P1
for (i = 0; i <= 10; ++i) {
buf[i] = i;
}
for (i = 0; i <= 10; ++i) {
printf("Index %d = %d\n", i,
buf[i]);
}
/* Error:
i and buf are of integer type use
%d insted of %s
since %s is for string.
*/
// P2
str =(char *) malloc(10 * sizeof(char) );
strcpy(str, "Something is wrong");
printf("%s\n", printThisOne);
/* Error:
->you have to use explicit type conversion to
convert in char type
->to use strcmp you have to use string.h
headerfile
*/
// P3
word = "Part 3";
printf("%s\n", word);
word+=4;
word="-";
printf("%s\n", word);
/* Error:
you have to give proper address in left hand side for
assinging a value.
since (word+4) is not giving a proper address on
left hand of '=' so it will give error of
'lvalue required as left operand of assignment'
or you can also use
so firt you have increment the value and the assing
it
*/
// P4
int temp=10;
integers+=10;
integers = &temp;
printf("Part 4: %d\n", *integers
);
integers=NULL;
/* Error:
initialize temp in starting
error is same as in 3
and clear the memory for pointer you have assign it to
NULL
free function is used when memory is assigned using
malloc function
*/
// P5
printf("Print this whole \0 line\n");
/* Error:
NO ERROR
*/
// P6
x = 2147483647;
printf("%d is positive\n", x);
x += 1000000000;
printf("%d is positive\n", x);
/* Error:
x was defined above
*/
// P7
printf("Cleaning up memory from previous
parts\n");
free(str);
memset(buf, 0, sizeof(buf));
/* Error:
free function used when memeroy is allocated using
malloc
to clean buf you have to use memset
*/
// P8 solve&
fib(&foo, 7);
printf("fib(7) = %d\n", foo);
/* Error:
use '&' operator to pass the
address
*/
// P9
int temp=123
bar = &temp;
bar = &temp;
printf("bar = %d\n", *bar);
/* Error:
you're not initializing the pointer. You've created a
pointer to "anywhere you want"—which could be the address of some
other variable, or the middle of your code, or some memory that
isn't mapped at all.
so you have to use an interger variable to assing it to a
pointer
intialize the temp variable in starting
*/
// P10 solve (char *)
someText =(char *) malloc(10);
strcpy(someText, "testing");
free(someText);
printf("someText = %s\n", someText);
/* Error:
->you have to use explicit type conversion to
convert in char type
->to use strcmp you have to use string.h
headerfile
*/
*/
exit(0);
return 0;
/* use of return 0 in mandatory */
}
// fib calculates the nth fibonacci number and puts it in A.
// There is nothing wrong with this function.
void fib(int *A, int n)
{
int temp;
if (n == 0 || n == 1)
*A = 1;
else {
fib(A, n - 1);
temp = *A;
fib(A, n - 2);
*A += temp;
}
}
Get Answers For Free
Most questions answered within 1 hours.