Exercise 1.1:: Student Names: Reviewer Names
Exercise 1.1:: Student Names: Reviewer Names
Exercise 1.1:: Student Names: Reviewer Names
Reviewer names:
_______________________________________
Exercise 1.1:
_______________________________________
_______________________________________
_______________________________________
____________________________________
Student names:
Reviewer names:
_______________________________________
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:
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
____________________________________
Student names:
Reviewer names:
_______________________________________
Please write code for the following pseudo-
code: _______________________________________
_______________________________________
1. declare an integer array of
size 3, and initialize it to
_______________________________________
values 2,-1,7
_______________________________________
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 }
_______________________________________
_______________________________________
_______________________________________
____________________________________
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: _______________________________________
_______________________________________
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 _______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________
_______________________________________