Professional Documents
Culture Documents
10 pts
I Show what is printed by the following program.
#include <iostream>
using namespace std;
int main()
{
int b[5];
return 0;
}
11 pts
II 1. The smallest unit of information in a computer is called a(n) ____________
2. The purpose of a(n) ________________ is to allow two computers to communicate with each
other using telephone lines.
5. The purpose of a(n) ___________________________ is to allow the efficient and orderly use of
a computer.
(a)
(b)
9. In one sentence, describe one major advantage of using a file for input.
Do not describe how to use a file for input; instead, give a sentence explaining one advantage of
using a file for input.
10. In one sentence, describe one major advantage of using a file for output.
Do not describe how to use a file for output; instead, give a sentence explaining one advantage of
using a file for output.
9 pts
III Assume that the program shown below is run. CIS is fun
Assume that the file "theinput.txt" contains the values 1.5 2.5 3.5
shown to the right:
a. What output will appear on the screen when the program runs?
b. What will be stored in the file "theoutput.txt" when the program is finished?
#include <iostream>
#include <fstream>
using namespace std;
ifstream file1("theinput.txt");
ofstream file2("theoutput.txt");
int main()
{
string message;
int a,b,c;
double p,q,r;
getline(file1, message);
file1 >> p;
file1 >> q;
file1 >> r;
cout << message << endl;
file2 << "p = " << p << " and a = " << a;
cout << " q = " << q << endl;
file2 << " r = " << r << " and b = " << b << endl;
cout << " p = " << p << " and c = " << c << endl;
file1.close();
file2.close();
return 0;
}
10 pts
IV Show what is printed by the following program.
#include <iostream>
using namespace std;
void funky(int &, int, int [ ]);
int main()
{
int x = 5, y = 1, z[3] = {10, 20, 30};
funky(x,y,z);
return 0;
}
d = a - b;
if (d < 0) {
b = 9;
a = 50;
}
else {
b = 25;
a = 14;
}
10 pts
V Show what is printed by the following program.
#include <iostream>
using namespace std;
int main()
{
int p = 8, q = 2, r = 1;
cout << "at the start: " << p << " "
<< q << " " << r << endl;
Note: Each part has an example, but your code should work for any set of values stored in the
variables, not just the ones shown in the example.
a. Assume that a person's name has been stored in two string variables,
called first and last. (You can assume that each part of the name has at least three
characters.) Construct a new string which will consist of the first two characters from the first
name and the first three characters from the last name. Store this new string in a variable
called newname
For example, if first holds "JOHN" and last holds "PHILIPS", then newname should hold
"JOPHI"
b. Assume that the substring "the" occurs at least one time in the string called parts. Find the
position of the first occurrence of "the" in parts, print this value, then store all of the characters
from this point until the end of the string parts in a variable named newparts. (Do not change the
string parts.)
For example, if parts holds "are the cat and the dog here?", you would say the substring "the"
occurs at index 4, and you would store "the cat and the dog here?" in newparts.
c. Print the first and the last characters stored in the string variable str.
You can assume that the original value stored in str has at least three characters.
Then remove both of these characters from the value stored in that variable.
For example, if str starts out with "woke", the first character is 'w' and the last character is
'e'. These two characters will be printed, and the value stored in str will be "ok".
9 pts
VII Here is a prototype for a function named func(), followed by some declarations that
appeared in a main program:
void func(int, int[]); // function prototype
Here are some calls from the main program to the function. Each call generated a compilation
error. For each call, first, explain why there is a compilation error; then show how to fix the call
so that there is no longer a compilation error.
Example: If the call to the function is: func(b,c);
then there will be a compilation error because the parameters are in the wrong order; the array is
the first parameter, but it should be the second.
The correct call is: … (and you will have to put in the correct call here)
b. func(b,a);
c. func(c,b[]);
32 pts
Write a complete C++ program, including a main program and three functions. Include a
good comment in the main program and one in each function – 2 1/2 points for comments.
a. First the main program will read a header value which it will call m. Then the main program
will call the function readdata() (see part 1) to read a set of data containing m groups into two
arrays which the main program will call numb1 and numb2. The first array will contain integers,
and the second array will contain numbers with decimal places.
b. Then the main program will call findneg() (see part 2) to determine how many of
the first m elements of the numb2 array are negative. After the function returns, the main program
will print this value with a message.
c. Finally, the main program will call the function changearr() (see part 3) to modify the
first m values stored in the numb1 array. After the function finishes, the main program will print
the new values stored in the numb1 array.
The main program will do nothing except read m, call the functions, and print, making sure that all
parameters are of the appropriate type, etc. Assume that each array has no more than 125
numbers. You do not need to include prompts; have all output go to the screen.
1. Write a function called readdata(). The function has three parameters: an integer k and two
arrays x and y. The x array holds integers, and the y array holds numbers with decimal places.
The function will read k groups of data. Each group contains two values, an integer and a
number with decimal places. Store the first data value read in the first position of array x, the
second value read in the first position of array y, and so on. The function will print the original data
values as they are read in.
2. Write a function called findneg() which will receive two parameters: an integer k and an
array parr of numbers with decimal places. The function will count and return the number of
negative values in the firstk elements of the parr array.
Example: Assume the array holds: 70.3 -1.2 1.3 -10.7 4.9, with k = 5. There are two negative
values (-1.2 and -10.7). Therefore, the function will return 2.
3. Write a function called changearr() which will receive two parameters: one integer k, and an
array of integers qarr of size k. The function will change each element of the qarr array, as
follows:
• if an element (for example, position 3) in the array is originally between 0 and 10 ("between"
includes both end points), that array element (in position 3) is changed to 57;
• if an element in the array is originally above 150, that array element is changed to 15.
For example, if initially the array holds 105 242 -1 150 7 (k = 5), then the array is changed
to 7 15 -1 7 57. Note that the -1 element in the array is not changed.