0% found this document useful (0 votes)
71 views9 pages

Exercise 1.1:: Student Names: Reviewer Names

The document contains code for several exercises that define and call functions. The code includes function definitions, parameter lists, return types, variable declarations, and function calls. Review questions are provided for each exercise asking about the function definitions, parameters, return types, variables, arguments used in calls, and expected output.

Uploaded by

Monesh Padyachi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views9 pages

Exercise 1.1:: Student Names: Reviewer Names

The document contains code for several exercises that define and call functions. The code includes function definitions, parameter lists, return types, variable declarations, and function calls. Review questions are provided for each exercise asking about the function definitions, parameters, return types, variables, arguments used in calls, and expected output.

Uploaded by

Monesh Padyachi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Student names:

Reviewer names:

_______________________________________
Exercise 1.1:
_______________________________________

1 int foo(int x, int y){ _______________________________________


2 x = x * x;
3 int z = x + y; _______________________________________
4 return z;
5 }
_______________________________________
6
7 int main(){
_______________________________________
8
9 int a = 5;
10 int b = 7;
_______________________________________
11 int c;
12 c = foo(a,b); _______________________________________
13
14 cout << c << endl; _______________________________________
15
16 system ("PAUSE"); _______________________________________
17 return 0;
} _______________________________________

_______________________________________

1. What is the function declaration? _______________________________________


2. What are the parameters of the
function? _______________________________________
3. What is the return type of the function?
4. What are the local variables? _______________________________________
5. What are the global variables?
_______________________________________
6. Where is the function used/called?
7. What are the arguments used in the _______________________________________
function call?
8. What will be printed in line 14? _______________________________________

_______________________________________

____________________________________
Student names:

Reviewer names:

Exercise 1.2: _______________________________________

_______________________________________
1 int foo(int a, int b){
2 a = a * a; _______________________________________
3 int c = a + b;
4 return c; _______________________________________
5 }
6 _______________________________________
7 int main(){
8 _______________________________________
9 int a = 7;
10 int b = 5; _______________________________________
11 int c;
12 c = foo(b,a);
_______________________________________
13
14 cout << c << endl;
_______________________________________
15
16 system ("PAUSE");
17 return 0;
_______________________________________
}
_______________________________________

_______________________________________
1. What is the difference between
function foo on this page, and function _______________________________________
foo for exercise 1.1?
2. Do these differences matter? Can you _______________________________________
imagine a situation where using one
_______________________________________
version of foo would produce a result
different from the other? _______________________________________
3. What are the arguments used in the
function call? _______________________________________
4. What will be printed in line 14?
_______________________________________
5. Explain why it is/it isn’t the same result
compared to exercise 1. _______________________________________

____________________________________
Student names:

Reviewer names:

_______________________________________

Exercise 1.3 _______________________________________

1 double bar(double n1, n2){ _______________________________________


2 double n1 = n1 * n1;
3 n3 = n1 + n2; _______________________________________
4
5 } _______________________________________
6
7 double main(){ _______________________________________
8
9 int m1 = 5 _______________________________________
10 int m2 = 7;
11 _______________________________________
12 m3 = bar(m1+m2);
13 _______________________________________
14 cout << m3 << endl;
15
_______________________________________
16 system ("PAUSE");
17 return 0;
_______________________________________
}

What are the syntax errors in this program? _______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

____________________________________
Student names:

Reviewer names:

Exercise 2.1 _______________________________________

_______________________________________
Please write code for the following pseudo-
code: _______________________________________

_______________________________________
1. declare an integer array of
size 3, and initialize it to
_______________________________________
values 2,-1,7

2. set the second element to -3 _______________________________________

3. sum up all three elements _______________________________________

4. display the sum _______________________________________

_______________________________________
5. What is the output of the program?
_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

____________________________________
Student names:

Reviewer names:

Exercise 2.2
_______________________________________
1 int foo(...){
2 _______________________________________
3 int positive=0;
4 for (...){ _______________________________________
5 if(...){
6 positive++; _______________________________________
7 }
8 }
9 return positive; _______________________________________
10 }
11 _______________________________________
12 int main(){
13 const int N = 5;
_______________________________________
14 double rates[N]={0.4,-2,4.5,6,-7.2};
15
16 int positive_rates = foo(...); _______________________________________
17 cout << positive_rates << endl;
18 return 0; _______________________________________
19 }
_______________________________________

Please complete this code as follows: _______________________________________

1. In line 1 function foo should have as _______________________________________


parameters an array and a companion
variable for the size. _______________________________________
2. In line 4, the for loop should go through the
elements of the array _______________________________________
3. In line 5, the condition should check if the
current element is positive (>=0) _______________________________________
4. In line 16, pass as arguments array rates
and its size. _______________________________________

What will be the output of this program? _______________________________________

_______________________________________

_______________________________________

____________________________________
Student names:

Reviewer names:

Exercise 3

1 #include <iostream>
2 #include <stdlib.h>
3
4 using namespace std;
5
6 const int N = 4;
7
8 bool add_element(int value, int array[], int& used){
9
10 bool success;
11 if(used <N){
12 array[used] = value;
13 used++;
14 success = true;
15 }
16 else {
17 success = false;
18 }
19
20 return success;
21 }
22
23 int main()
24 {
25 int fares[N];
26 int fares_used = 0;
27 int current_rate = 0;
28 bool fares_not_full = true;
29
30 while(fares_not_full){
31 current_rate = -2 * current_rate + 1;
32 fares_not_full = add_element(current_rate,fares, fares_used);
33 }
34
35 cout << "Fares: ";
36 for(int i=0; i< fares_used; i++){
37 cout << fares[i] << ", ";
38 }
39 cout << endl;
40
41 cout << "Number of fares:" << fares_used << endl;
42 cout << "Current rate:" << current_rate << endl;
43 system ("PAUSE");
44 return 0;
45 }
Student names:

Reviewer names:

Questions: _______________________________________

1. What are the global constants or _______________________________________


variables?
2. Which parameters of add_element are _______________________________________
passed by reference, and which by
_______________________________________
value?
3. Describe in less than 50 words what _______________________________________
function add_element does. Which
parameters can change, what are they _______________________________________
used for, and what does the return
value mean? _______________________________________
4. What will be the value of array
_______________________________________
current_rate in line 32 after the
first iteration (first time it is executed) _______________________________________
5. What will be the value of array fares
in line 32 after the first iteration (first _______________________________________
time it is executed)
6. What will be the value of array _______________________________________
fares_used in line 32 after the first
_______________________________________
iteration (first time it is executed)
7. What will be the value of array _______________________________________
fares_not_full in line 32 after the
first iteration (first time it is executed) _______________________________________
8. What will be the value of fares,
_______________________________________
fares_used, fares_not_full,
and current_rate in line 34, after _______________________________________
the while loop?
9. What will the for-loop from line 36 to _______________________________________
38 print?
10. What will the print statements in line _______________________________________
41 and 42 print?
_______________________________________

_______________________________________
Student names:

Reviewer names:

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________

_______________________________________ _______________________________________
Student names:

Reviewer names:

Exercise 4 _______________________________________
Please write code for the following pseudo-
_______________________________________
code:
_______________________________________
1. Open an output file report.txt
2. Write the text “This is output” to the file _______________________________________
report.txt.
3. Write the text “Program completed” to _______________________________________
standard output
4. Close the file _______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

_______________________________________

You might also like