Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

C++ Programming Assignment 4

Question 1: Functions and pass-by-value

a) Write down the output of the following program. 

#include <iostream>

using namespace std;

int calArea(int, int);

int main(){

int height=2;

int width = height*2;

int area  = calArea(height,width);

cout << height << " " << width << " " << area << endl;

return 0;

}

int calArea(int h, int w){

int a = h * w;

h++;

w++;

return a;

}

b) Write down the output of the following program.

#include <iostream>

using namespace std;

int toMultiply(int);

int toSum(int,int);

int main(){

int result;

result = toSum( toMultiply(toMultiply(10)), toMultiply(10));

cout << result << endl;

return 0;

}

int toMultiply(int input){

int output = input*input;

return output;

}

int toSum(int a, int b){

return (a+b);

}

c) In the following question, we have 3 files: partial.h, partial.cpp, main.cpp. You are required to:

1. Write down function prototypes in partial.h

2. Fill in the blank in partial.cpp, main.cpp   

so that the whole program can run properly, which outputs the following result:

RMB:66 .

3. Implement above program in visual studio, execute the program, and take a screenshot of your program and running result, and put the screenshot below.

// File partial.h

// File partial.cpp

double toRMB(___________) {

return usdollar * 6.6;

// File main.cpp

#include ________

using namespace std;

#include ________

void main() {

    double usd = 10;

______ rmb = toRMB(usd);

cout << "RMB: " << rmb << endl;

}

Question 2: Pass-by-Reference

a) What is the output of the following program?

#include <iostream>

using namespace std;

void fun(int& a, int& b){

cout << a << " " << b;

a = (a+b)%b;

b = b-a;

cout << " --> " << a << " " << b << endl;

}

int main(){

int a = 3, b = 5;

fun(b, a);

return 0;

}

b) What is the output of the following program? 

#include <iostream>

using namespace std;

void fun2(int& a, int& b, int c, int& d){

int temp;

temp = a;

a = b;

b = c;

c = d;

d = temp;

}

int main(){

int a, b, c, d;

a = b = c = d = 0;

for(int i = 0; i < 2; i++){

fun2(a, b, c, d);

a += 4;

b -= 3;

c *= 2;

d -= 1;

}

cout << a << " " << b << " " << c << " " << d << endl;

}

c) Fill in the blanks with appropriate parameter-passing mechanism (i.e., int or int&) to fill in the blanks so that the output of the following program is 8 6 6.

#include <iostream>

using namespace std;

int fun3(____ a, ____ b)

{

int result;

result = a + b;

int temp = a;

a = b;

b = temp;

return result;

}

void main()

{

int a=3, b=5;

cout << fun3(b, a) << " " << fun3(a, b) << " " << fun3(b,a);

}

Question 3: Passing Array to Functions

a) The following program copies the elements in source array to destination array. There are one or more errors in the program. Please identify and correct it/all of them.

1  #include <iostream>

2  using namespace std;

3  

4  void copy(int, const int[], const int[]);

5  

6  int main() {

7      int source[3] = {1, 2, 3};

8      int destination[3];

9

10     copy(3, source, destination);

11

12     return 0;

13 }

14

15 void copy(int size, const int a[], const int b[]) {

16     for (int i = 0; i < size; i++) {

17        b[i] = a[i];

18    }

19 }

b) What is the output of the following program?

#include <iostream>

using namespace std;

int compute(int, int, const int[], const int[]);

int main() {

int c[6] = {2, 3, 5, 7, 93, 4};

int d[7] = {23, 4, 9, 3, 40, 33, 1};

cout << compute(2, 4, d, c) << endl;;

return 0;

}

int compute(int start, int end, int const a[], int const b[]) {

int sum = 0;

for (int i = start; i <= end; i++) {

sum += b[i] - a[i];

}

return sum;

}

c) The following program will compute and display the sum of all elements in array arr. Please complete the function computeSum.

#include <iostream>

using namespace std;

int computeSum(int, const int[][3]);

int main() {

int arr[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};

cout << "Sum of double array = " << computeSum(4, arr) << endl;

return 0;

}

// add your code for function computeSum here

Question 4: Local and global variables

a) If the program is correct (i.e. no compilation problem), write down its output. If you think there is a bug, write down where it is.

#include <iostream>

using namespace std;

int i = 0;

const int j = 5;

void first(){

i = i+j;

int j = ++i;

int i = j;

}

void main() {

first();

cout<<i<<" "<<j<<endl;

}

b) Integer variables names a, b, c, d appear in both first() and main() function. Determine whether a, b, c, d are referring to the same variable in the two functions, respectively.  (For example, if you think variable name a in first() and main() functions are referring to the same variable, please write ‘a: yes’, otherwise write ‘a: no’.)

#include <iostream>

using namespace std;

int a = 1, b = 2, c = 3, d = 4;

void first(){

int a, d;

b++;

c--;

}

void main() {

const int a = 0;

b = a*d;

c = b++;

}

Question 5: Recursion

a) What is wrong with the following recursion program?

#include <iostream>

using namespace std;

int f(int n){

int result = 3+2*f(n-1);

return result;

}

void main(){

int r, n;

cout << “Enter value for n:” << endl;

cin >> n;

r = f(n);

cout << “Result is:” << r << endl;

}

b) What is the output of the following program?

int rec(int n, int m){

int result = rec(n-1, m)*m;

if (n <= 0)

return result;

}

void main(){

cout << rec(5, 2) << endl;

}

c) What is the output of the following program?

int rec(int s, int t){

if (s*t == 0)

return 0;

return rec(s-2, t-3)+2;

}

void main(){

cout << rec(6, 9) << endl;

}