For each of the following exam questions, place your
answers on the question paper. There
will be no scrap paper given. If you
need scrap, two blank pages are provided at the back of this exam paper. Your name must appear at the top of each
page, including the scrap pages in the back.
NO CALCULATORS WILL BE PERMITTED!!
1.(4 points) Write the C++ statement(s) (NOT A PROGRAM) that will print the following limerick exactly as it appears below. The indented lines are indented one tab position. Watch the punctuation marks!
Mr. Franklin was
flying his kite
In the middle of a dark stormy night.
"It's
for science," he said
And
he went on ahead.
Now he's generally thought of as bright!!!
cout << "Mr. Franklin was flying his
kite" << endl;
cout << "In the middle of a dark stormy night." << endl;
cout << "\t\"It\'s for
science,\" he said" << endl;
cout << "\tAnd
he went on ahead." << endl;
cout << "Now he\'s generally thought of as bright!!!" << endl;
2. (4 points) What value is assigned to the integer variable y ?
a) y = 3/5 * 3 + 4;
y = 0
* 3 + 4
y = 0
+ 4
y = 4
b)
y = 53 % (4 * 3) + 1
y = 53
% 12 + 1
y = 5
+ 1
y = 6
3. (15 points) What is printed by each of the following program segments? Place your answers in the boxes at the side.
a)
a) int i,j;
***** * * * * ***** * * * * *****
for (i = 0; i < 2;
++i)
{
cout << "*****";
cout<<endl;
for (j = 0; j < 2; ++j)
{
cout <<
"* *" << endl;
//3
spaces between
}
}
cout << "*****"<<endl;
b) int A[2][3], i, j;
b)
for (i=0;
i<2; i++)
2
2
2 2
3 4
{
for(j=0; j<3; j++)
{
A[i][j] = i * j + 2;
cout << A[i][j] <<" ";
}
cout << endl;
}
c) int
A[8]={1,2,4,6,7,7,2,1};
c)
int i = 2;
1 2 2 3
3
2 2 1
while (i <= 5)
{
A[i] = A[i] - i;
i++;
}
for(i=0;i<8;i++)
{
cout <<
A[i] <<" ";
}
d)
d) int k = 6;
Goodbye Hello
do{
cout<<"Goodbye"
<< endl;
}while(k < 5);
cout <<
"Hello" << endl;
e ) int
m = 5;
m = 5 m = 3 m = 1 e )
while (m >= 1)
{
cout << "m = " << m << endl;
m = m - 2;
}
4. (8 points) Matching column. Write the appropriate letter in the blank space to the left of the term.
__f__1. RAM __c__2. syntax __h__3. secondary storage __b__4. low level language __e__5. high level language __ j __6. compiled language __k__7. source code __d__8. programming |
a. Directs and controls the overall operation of the computer. b. Examples are machine and assembly languages. This type of language is dependent on the computer that is using it. c. The set of rules for formulating grammatically correct language statements. d. The process of writing a piece of software. e. Instructions which resemble written language and can be run on a variety of computer types. f. Volatile memory where your programs and data are stored while you use the computer. g. Where all the arithmetic and logical functions, such as addition, subtraction, comparison, etc. are performed. h. A permanent storage area for programs and data. i. Contains the fundamental instructions for a computer which cannot be lost or changed by the casual computer user j. A language where the source code is translated as a whole before any one statement is executed. k. A program written in a computer |
5. (4 points) What is printed by the following C++ program?
#include
<iostream.h>
#include <iomanip.h>
int
main()
{
float num1;
num1 = 123.456;
cout << '|' << num1 << '|' << endl;
cout << setiosflags(ios::fixed);
cout
<< setiosflags(ios::showpoint);
cout
<< setprecision(2) << num1 << endl;
cout << '|'
<< setw(8) << num1 << '|' <<
endl;
cout << '|' << setw(1)
<< num1 << '|' << endl;
}
|123.456|
123.46
| 123.46|
|123.46|
6. (6
points) A C++ program
contains the following statements:
float a = 0.1;
float b = 0.3;
float c, *pa, *pb;
pa = &a;
*pa = 2 * a;
pb = &b;
c = 3 * (*pb - *pa);
Suppose each floating-point number occupies 4
bytes of memory. Assume further that the
address of variable a is 1130, the address
of variable b is 1134, and the address of variable c is
1138.
Draw a layout of memory, its variables and its pointers. Then answer the following questions.
i)
What value is assigned to
&b?_______1134________________
ii)
What value is
assigned to pa?
________1130___________________
iii)
What value
is represented by *pa? _______.2___________________
iv)
What value
is represented by &(*pa)?
_______1130________________
v)
What value
is assigned to c?
_____________.3_______________
vi)
What value
is represented by *(&a)? ________.2________________
7. Use the following C++ program to answer questions a - e
#include
<iostream.h>
const float num = 2.5;
void stuff (float& a, int b);
void
main()
{
float r = 0;
int s, t;
s = 5;
t = 10;
cout <<
"The value of r, s, and t are: "
<< r << ' ' << s << ' '
<< t << endl;
stuff(r, t);
cout <<
"The value of r, s, and t are: "
<< r << ' ' << s << ' '
<< t << endl;
stuff(r, s);
cout <<
"The value of r, s, and t are: "
<< r << ' ' << s << ' '
<< t << endl;
}
void stuff(float& a, int b)
{
float c;
c = a + num;
++a;
++b;
cout <<
"In stuff the values of a, b, and c are: "
<< a << ' ' << b << ' '
<< c << endl;
}
a) (1 points) Which variable(s) are global to main and stuff?_______num__________________
b) (1 points) List the reference parameter(s) in stuff. ___________a____________________
c) (1 points) Which variable(s) are local to stuff?_____________c_____________________
continued on next page
d) (3 points) A CSC 126 student from the evening class, added the following function call to the program.
stuff(t,r);
The compiler flagged this statement with an error message. Why?
function stuff expects a float and then an integer. The function call gives it an integer and then a float.
e) (4 points) What is printed by this program?
The value of r, s, and t are: 0 5 10
In stuff the values of a, b, and c are: 1 11 2.5
The value of r, s, and t are: 1 5 10
In stuff the values of a, b, and c are: 2 6 3.5
The value of r, s, and t are: 2 5 10
8. a) (3 points) Sort the following numbers using the bubble sort. Do not write any code. Show the values after each pass (iteration) through the numbers.
6 12 38
3 6 37
6 12 3
6 37 38
6 3 6
12 37 38
3 6 6 12
37 38
b. (3 points) Using the same unsorted array: 6 12
38 3 6
37
Show the first three passes for the selection sort.
3 12 38 6 6 37
3 6 38 12 6 37
3 6 6 12 38 37
9. (6 points) Determine if the following relational expressions evaluates to a 0 (false) or a 1 (true). Assume the following declarations:
int i = 10, j = 14, k = 24;
double x = 11.5;
a) i + 2 == k – 12 |
Ans:1 |
10 + 2 == 24 - 12 12 == 12 true |
|
|
|
b) j % i * k || k
% i * j |
Ans:1 |
14 % 10 * 24 || 24
% 10 * 14 96 || 56 true any value not zero is equivalent to a true |
|
c) (i + (2 * j))
> k (10 + (2 * 14)) > 24 (10 + 28) > 24 38 > 24 true |
Ans:1 |
10. (4 points) Convert the following if…else chain to a switch statement. Write ONLY THE SWITCH STATEMENT.
if (code == 1)
cout<<
”The sum is “ << fnum + snum
<< endl;
else if (code == 2)
cout<< "The product is" << fnum * snum << endl;
else
cout << ”The difference is: " << fnum – snum << endl;
switch(code){
case
1: cout<< "The sum is " << fnum
+ snum << endl;
break;
case
2: cout<< "The product is" << fnum
* snum << endl;
break;
default
: cout <<"The difference is: "
<< fnum - snum
<< endl;
}
11.(6 points) Assuming the following is the input file called personnel.txt (and it opens properly), what is the output of the following program:
Roy 163-98-4182 7.32 37
Richter 189-53-2147 8.32 40
Nolan 145-32-9826 6.54 40
Jeter 163-09-4263 9.80 35
The program:
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
void main()
{
ifstream in;
char ch, name[30], ss[12], a;
float rate, hours, net;
in.open("personnel.txt",ios::nocreate);
if (in.fail())
{
cout<<"\nThe input file failed to open"<<endl;
exit(1);
}
cout<<"\nSoc. Sec. No.\t\tName\t\tNet Pay\n";
cout<<"-------------\t\t----\t\t-------\n";
cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)
<<setprecision(2);
while ((ch=in.peek())!=EOF)
{
in >> name >> ss
>> rate >> hours;
net = rate * hours;
cout<< ss <<
"\t\t" << name << "\t\t" << net <<
endl;
}
in.close();
return;
}
Soc. Sec. No. |
Name |
Net Pay |
------------- |
---- |
------- |
163-98-4182 |
Roy |
270.84 |
189-53-2147 |
Richter |
332.80 |
145-32-9826 |
Nolan |
261.60 |
163-09-4263 |
Jeter
|
343.00 |
12. (a - d are 2 points each) Suppose you have strings:
char S1[20] = “fast”;
char S2[20] = “break”;
char S3[20];
int X, Y, Z;
a) What is the value of S2[1]? __r___________________
b) Z = strcmp (S1, S2); Is Z positive, negative or 0?________1_________
c) After strcat (S2, “neck”) what is in S2?___________breakneck__________
d) After strcpy (S3, S1) what is in S3?________fast________
For string S5 of unknown length and value.
e) (4 points) Write C++ to count the number of occurrences of the letter 'z' in string S5.
i = 0;
count = 0;
while (S5[i] != '\0')
{
if (S5[i] == 'z') count ++;
i++;
}
13. (15 points) The Bite-a Vite-a vitamin store needs an “expert” computer programmer to help keep track of their inventory. The store maintains all their vitamin stock information on disk. The data is organized as follows:
The first column contains the vitamin name (A, B, C, etc.)
The second column contains the unit price of a single jar of that particular vitamin.
The third column has the number of jars of that vitamin in the store.
For example:
A 12.95 23
K 9.99
56
Write a C++ program, on the next page, that will do the following:
i) The average price of a vitamin
ii) The total inventory (total number of jars)
--------------------------------------------------
The total store
inventory is 104 jars of vitamins
WRITE
YOUR PROGRAM IN THE SPACE PROVIDED BELOW
SOLUTION I (The better solution! Why?)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
double
value (double, int);
using
namespace std;
int
main()
{
float price;
int invent;
int count = 0, total_jars = 0;
float total_vite_price = 0;
ofstream outfile;
char vitamin;
ifstream infile;
infile.open("inventory.txt");
if(infile.fail())
{
cerr << "file failed to
open";
abort();
}
outfile.open("output.txt");
outfile <<
"Vitamin Price Inventory Total Inventory " << endl;
outfile <<
"-----------------------------------------------------" << endl;
outfile << setprecision(2) << setiosflags
(ios::fixed);
while (!infile.eof())
{
infile >> vitamin >>
price >> invent;
outfile << vitamin << setw(15) << price << setw(15)
<<
invent << setw(15) << value (price, invent)<< endl;
count ++;
total_jars += invent;
total_vite_price += price;
}
outfile << "The average
price for a vitamin is: " << total_vite_price
/ count << endl;
outfile << "The total
store inventory is " << total_jars
<< " jars of vitamins" << endl;
infile.close();
outfile.close();
return 0;
}
double
value (double p, int i)
{
return p * i;
}
SOLUTION II
#include
<fstream.h>
#include
<stdlib.h>
#include
<iomanip.h>
float total_value (float , int);
void
main()
{
char vitamin[20];
int inventory[20];
float price[20],
average;
int total_inventory;
float total_price;
int count = 0;
int i;
ifstream Infile;
Infile.open("c:\\store\\inventory.txt");
if (Infile.fail())
{
cerr
<< "File failed to open" ;
abort();
}
total_inventory =
0;
total_price = 0;
cout <<
"Vitamin\t\tPrice\t\tInventory\tTotal
Inventory\n";
cout <<
"---------------------------------------------------------" <<
endl;
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint)
<< setprecision(2);
while (!Infile.eof())
{
Infile
>> vitamin[count] >> price[count] >>
inventory[count];
++count;
}
for (i = 0; i <
count; ++i)
{
total_inventory
+= inventory[i];
total_price
+= price[i];
}
for (i = 0; i <
count; ++i)
cout
<< vitamin[i] << "\t\t" << price[i] <<
"\t\t"
<< inventory[i] <<
"\t\t"
<< total_value(price[i], inventory[i]) << endl;
average = total_price/count;
cout
<< "The average price for a vitamin is: " << average << endl;
cout <<
"The total store inventory is " << total_inventory
<< " jars of vitamins" << endl;
Infile.close();
}
float total_value (float p, int i)
{
return (i * p);
}