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

Exercise 1 (10 points each): (a) Implement RandMultipByVal function, which gets

ID: 667395 • Letter: E

Question

Exercise 1 (10 points each):
(a) Implement RandMultipByVal function, which gets one integervariable as its argument
and multiply it by a random number between 1 to10 (use call-by-value approach).
(b) Implement printByVal function, which gets one integer variable as its argument
and prints the value of the variable on the screen using cout (use call-by-value approach).
c) In your main function define and initialize a variable Num and first call RandMultipByVal
to multiply a random number to your variable and then use printByVal in the main function
to print the result of RandMultipByVal.

Exercise 2 (10 points):
a). Repeat exercise 1 with call-by-reference approach with pointers. Replace the “Val” in
the name of functions by “Ref”.

***C++ PROGRAMMING

Explanation / Answer

Exercise 1:

#include <iostream>
using namespace std;
#include <stdlib.h>

// function declaration
void RandMultipByVal (int x);
void printByVal (int y);

int main ()
{
// local variable declaration:
int Num = 5;
RandMultipByVal(Num);
printByVal(Num);

return 0;
}

void RandMultipByVal (int Num)
{
int r = rand() % 10 + 1;
// cout<<r<<endl;
Num = Num * r;
}
void printByVal (int y)
{
cout << y << endl;
}

Exercise 2:

#include <iostream>
using namespace std;
#include <stdlib.h>

// function declaration
void RandMultipByVal (int &x);
void printByVal (int &y);

int main ()
{
// local variable declaration:
int Num = 5;
RandMultipByVal(Num);
printByVal(Num);

return 0;
}

void RandMultipByVal (int &Num)
{
int r = rand() % 10 + 1;
// cout<<r<<endl;
Num = Num * r;
}
void printByVal (int &y)
{
cout << y << endl;
}