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

#1 Using the “Pass by Reference ( & )” method, write a program that prompts the

ID: 2083747 • Letter: #

Question

#1     Using the “Pass by Reference ( & )” method, write a program that prompts the user to      enter 3 numbers, store them in the variables num1, num2, and num3. You are to        “call” the function Multby234 which will send the addresses of the variables num1,      num2, and num3 to the function. Within the function, you are to change the values in      the variables by multiplying num1 by 2, multiplying num2 by 3, and multiplying num3       by 4. The newly transformed values in num1, num2, and num3 must be displayed back      in the main() function. #2        Using the “Pass by Reference ( & )” method, write a program that prompts the user to      enter a single integer number (either negative, zero, or positive), store it in the variable       num. From main() you are to send the addresses of the variable num along with the      address of another integer variable called sign to a function called PosNeg. Within the      function, you are to inspect the value of num such that if its value is:       - less than zero, then make the value in sign equal to a -1,       - greater than or equal to zero, then make the value in sign equal to a +1.      Back in main(), use the newly transformed value in sign to test whether your number was      negative or positive with an “if-statement” and then print out either “Your number was      positive!” or “Your number was negative!”.

Explanation / Answer

#1

Code :

#include <stdio.h>

/* function declaration */
void Multby234(int *n1, int *n2, int *n3);

int main () {

/* local variable definition */
printf("Enter 3 numbers ");
int num1, num2, num3;
scanf("%d%d%d",&num1, &num2, &num3);


printf("Before function, value of num1 : %d ", num1 );
printf("Before function, value of num2 : %d ", num2 );
printf("Before function, value of num3 : %d ", num3 );

/* calling a function to multiply the variables num1, num2, num3.
* &num1 indicates pointer to num1 ie. address of variable num1
&num2 indicates pointer to num2 ie. address of variable num2 and
* &num3 indicates pointer to num3 ie. address of variable num3.
*/
Multby234(&num1, &num2, &num3); /* Function Calling */

printf("After function, value of num1 : %d ", num1 );
printf("After function, value of num2 : %d ", num2 );
printf("After function, value of num3 : %d ", num3 );

return 0;

}


/* function definition */
void Multby234(int *n1, int *n2, int *n3) {

*n1 = *n1 * 2 ; /*adress num1 value is multiplying with 2 */
*n2 = *n2 * 3 ; /*adress num2 value is multiplying with 3 */
*n3 = *n3 * 4 ; /*adress num3 value is multiplying with 4 */

return;
}

OUTPUT :

Enter 3 numbers
2
3
4
Before function, value of num1 : 2
Before function, value of num2 : 3
Before function, value of num3 : 4
After function, value of num1 : 4
After function, value of num2 : 9
After function, value of num3 : 16