Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C. Please fill out the code and do not make modifications to the functions

ID: 3912270 • Letter: U

Question

Using C. Please fill out the code and do not make modifications to the functions or headers :

#include
#include

int foo(int* a, int* b, int c){
/*Swap the addresses stored in the pointer variables a and b*/
  
/*Decrement the value of integer variable c*/
  
/*Return c*/
}

int main(){
/*Declare three integers x,y and z and initialize them randomly to values in [0,10] */
  
/*Print the values of x, y and z*/
  
/*Call foo() appropriately, passing x,y,z as parameters*/
  
/*Print the values of x, y and z*/
  
/*Print the value returned by foo*/

  
return 0;
}

1) The function int foo(int *a, int *b, int c) should perform the following computations -Swap the addresses of the integers pointed to by a and b (not the values of the integers pointed to by a and b). - Decrement the value of c - Return the value of c. ) In the main function, declare three integers x, y, and z, and assign them random integer values in the interval [O, 10] 3) Print the values of the integers x, y, and z. Call foo() appropriately passing x, y, and z as arguments. Print out the values of integers x, y, and z after calling the function foo(.). Also, print the value returned by foo(). 4) Answer the following questions in a comment at the bottom of the file: a) Is the return value different than the value of integer z? Why or why not? b) Are the values of integers x and y different before and after calling the function foo(.)? Why or why not?

Explanation / Answer

#include <stdio.h>

int foo(int* a, int* b, int c){

printf(" Inside foo, Address of a = %d Address of b = %d c = %d",a,b,c);
/*Swap the addresses stored in the pointer variables a and b*/
int *temp;
temp = a;
a = b;
b = temp;

printf(" Inside foo After Swapping addresses , Address of a = %d Address of b = %d c = %d",a,b,c);
/*Decrement the value of integer variable c*/
c--;

/*Return c*/
return c;

}
int main(){
/*Declare three integers x,y and z and initialize them randomly to values in [0,10] */
int x,y,z;

x = 5;
y = 7;
z = 4;

/*Print the values of x, y and z*/
printf("x = %d y = %d z = %d",x,y,z);

/*Call foo() appropriately, passing x,y,z as parameters*/
int result = foo(&x,&y,z);

/*Print the values of x, y and z*/
printf(" x = %d y = %d z = %d",x,y,z);


/*Print the value returned by foo*/
printf(" Value returned from foo : %d",result);

return 0;
}

Output:

x = 5 y = 7 z = 4
Inside foo, Address of a = 309520424 Address of b = 309520428 c = 4
Inside foo After Swapping addresses , Address of a = 309520428 Address of b = 309520424 c = 4
x = 5 y = 7 z = 4
Value returned from foo : 3

4 a) yes , the return value is different than the value of integer z. It will be the decremented value of z by 1.

4 b) No, the value of x and y will be same after the call to foo() because the scope of a and b is local inside the function foo() . Inside the function , the addresses are swapped but will not reflect outside the function .

Do ask if any doubt. Please upvote.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote