Rewriting C code (also in C++) 4. 120 points] Consider the following Pascal prog
ID: 3704822 • Letter: R
Question
Rewriting C code (also in C++)
4. 120 points] Consider the following Pascal program: program Progl (input, output); function test (var x, y: integer; z : integer) integer; begin z:= x + y; x:= 2 * z; := x + 3; testz; end; var i,j,k,w integer; begin write 'Enter values readln (i, j, k) w := test (i, j, k); writeln ('output: ', i,'',j,'', k,'',w) end a. Rewrite the above code in C using pointers, printf, and scanf. b. Rewrite the above code in C++ using reference parameters, cout, and cin c. What does the program print if the input is entered with 3 4 5?Explanation / Answer
(A)
#include<stdio.h>
int test(int *x, int *y, int *z)
{
*z = (*x) + (*y);
*x = 2 * (*z);
*y = (*x) + 3;
return *z;
}
int main()
{
int i , j , k , w;
printf("Enter values : ");
// get user input
scanf("%d%d%d", &i, &j, &k);
w = test( &i , &j , &k );
printf("Output : %d , %d , %d , %d", i , j , k , w);
return 0;
}
(B)
#include<iostream>
using namespace std;
int test(int& x, int& y, int& z)
{
z = x + y;
x = 2 * z;
y = x + 3;
return z;
}
int main()
{
int i , j , k , w;
cout<<"Enter values : ";
// get user input
cin>>i>>j>>k;
w = test( i , j , k );
cout<<"Output : "<<i<<" , "<<j<<" , "<<k<<" , "<<w<<endl;
return 0;
}
(C)
i = 3
j = 4
k = 5
Inside test():
x = 3 ( i in main )
y = 4 ( j in main )
z = 5 ( k in main )
Now,
z = x + y = 3 + 4
z = 7
x = 2 * z = 2 * 7
x = 14
y = x + 3
y = 17
In main():
i = 14
j = 17
k = 7
w = 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.