Homework 7
Due October 29, 2002
Solutions
1a) True
1b) False
1c) True
1d) False
1e) True
1f) False
1g) False
1h) False
2a) prototype: void hello(int&, double, char);
heading: void hello(int& first, double second, char ch)
body: {
int num;
double y;
int u ;
.
.
.
}
function definition: both the heading and the body of the function
2b) call statements: hello(x,y,z);
hello(x, y-3.5, 'S');
formal parameters: int& first, double second, char ch
actual parameters: x, y, z for the first call statement
x, y-3.5, 'S' for the second call statement
2c) value parameters: second and ch
reference parameters: first
2d) local variables: for main x, y, z
for hello num, y, u
global variables: one
4)
-19 10 51
10
0 20 112
20
-380 40 802
40
40 80 1682
80
6)
10 5 6
10 10 20
7)
1 cout << "Please enter two integers.\n";
2 cin >> num1 >> num2;
3 func (num1, num2);
4 val3 = val1 + val2;
5 val4 = val1 * val2;
6 cout << "The sum and product are " << val3 << " and " << val4;
7 cout << " The two integers are " << num1 << ", " << num2 << endl;
8 return 0;
A) x = 4
x = 6
x = 8
x = 10
x = 4
x = 4
x = 4
x = 4
B)
Here are the lines that you should have added:
//prototype:
string shift_cipher(string text, int key);
// call function to do encryption, and output the result:
cout << shift_cipher(text, key) << endl;
// call function to do decryption, and output the result:
cout << shift_cipher(text, -key) << endl;
Here's the whole program for problem B:
shift_cipher.cpp
C)
Nothing should be changed with the function shift_cipher itself - just
the function main needed to be enhanced. Here's main:
int main()
{
string text; // text they want to encrypt/decrypt
char command; // one-letter command, d or e (for decrypt or encrypt)
int key; // key to use for the shifting encryption/decryption
int i;
cout << "Please input the word: ";
cin >> text;
cout << "Do you want to encrypt, decrypt or analyze (e/d/a)? ";
cin >> command;
if (tolower(command) == 'e') {
cout << "Please type the key (choose from 0-25): ";
cin >> key;
cout << shift_cipher(text, key) << endl;
} else if (tolower(command) == 'd') {
cout << "Please type the key (choose from 0-25): ";
cin >> key;
cout << shift_cipher(text, -key) << endl;
} else if (tolower(command) == 'a') {
for (i = 0; i < 25; i++)
cout << shift_cipher(text, i) << endl;
} else
cout << "Unknown command, program will exit" << endl;
return 0;
}
Here's the whole program for problem C:
shift_cipher2.cpp