Exam #2 Spring 2007 WITH ANSWERS

advertisement
CSC126
Exam 2/ Page 1
Spring 2007
Dr. Sarah Zelikovitz
ANSWERS ARE IN RED INK BELOW EACH QUESTION:
All answers should be clearly numbered in the blue book only. Calculators and cell
phones may not be used during the exam.
1) (20 points) Trace the following two programs:
a) What is printed by the following code?
#include<iostream>
using namespace std;
void compute(double & , double);
int main ()
{
double number, howMany;
number = 15;
howMany = 25;
compute(howMany, number);
cout << "First Results\n";
cout << "-------------\n";
cout << number << ' '<<howMany <<endl;
cout << "Second Results\n";
cout << "-------------\n";
compute(howMany, 8 );
cout << ' '<<number << ' ' <<howMany;
return 0;
}
void
{
compute(double & a, double
if (a > b)
a = a * b;
else
a = a * 2;
}
First Results
------------15 375
Second Results
------------15 3000
b)
CSC126
Exam 2/ Page 2
b) The contents of the file junk.txt is:
512
23.1
29
64.3
70
85.7
19
13.1
94
0.987
12
21
456
45.0
What is printed by the following code?
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int x;
double doubleVal;
ifstream infile;
infile.open("junk.txt");
if (!infile)
{
cout<<"error in the input file";
return 1;
}
infile>>x >> doubleVal;
while (x > 65 || doubleVal > x)
{
cout<<x<<endl;
infile>>x>>doubleVal;
cout<<"Next Value...";
}
return 0;
}
512
Next Value...29
Next Value...70
Next Value...
Spring 2007
Dr. Sarah Zelikovitz
CSC126
Exam 2/ Page 3
2)
Spring 2007
Dr. Sarah Zelikovitz
Short Answer questions:
a) (18 points) Assume the following declaration:
int measures[100];
i) How many bytes does measures take up?
400 (100 int at 4 bytes each)
ii) Write the C++ statement(s) that sets every element of measures equal to
10.
int i;
for (i = 0; i < 100; i++)
measures [i] = 10;
iii) Write the C++ statement(s) that assigns the first and the last elements of
measures the value 500. Be careful with index values!
measures[0] = 500;
measures[99] = 500;
iv) Assume that you do not know the values of the elements in measures. Write
the C++ statement(s) that prints “YES” if measures contains at least two
elements that are greater than 100, and “NO” otherwise.
int count = 0;
for (i = 0; i < 100; i++)
if (measures[i] > 100)
count++;
if (count >= 2)
cout << “YES”;
else
cout << “NO”;
OR
int count = 0;
int i = 0;
while (i < 100 && count < 2)
{
CSC126
Exam 2/ Page 4
Spring 2007
Dr. Sarah Zelikovitz
if (measures[i] > 100)
count++;
i++;
}
if (count == 2)
cout << “YES”;
else
cout << “NO”;
b) (6 points) Suppose that I want to compute the sum of the squares of all the odd
numbers between 0 and 100. That is, I want to compute the following expression:
12  3 2  5 2  7 2  9 2    99 2
Write C++ code using a for loop or a while loop that will compute the above
expression.
int sumOfSquares = 0;
for (i = 1; i < 100; i+=2)
sumOfSquares+=pow (i, 2);
c) (6 points) Change the following while loop into a for loop:
int number;
number = 8;
while (number < 15)
{
cout << "NEXT: "<<number<<endl;
number++;
}
int number;
for (number =8; number < 15; number++)
cout << "NEXT: "<<number<<endl;
CSC126
Exam 2/ Page 5
Spring 2007
Dr. Sarah Zelikovitz
d) (6 points) What is the difference between a variable that is sent by reference to a
function, and one that is sent by value?
A variable sent by reference (using an &) sends an address to the location of the
actual parameter. Hence all changes to the formal parameter in the function changes
the actual parameter. When a variable is sent by reference, a copy is made for the
function, and changes to the variable in the function do not change the actual
parameter.
e) (6 points) Show a picture of the array arr after the following code executes.
int arr[7];
int i;
for (i = 0; i < 7; i++)
if (i < 3)
arr[i] = 0;
else
arr[i] = 34;
0 0 0 34 34 34 34
CSC126
Exam 2/ Page 6
Spring 2007
Dr. Sarah Zelikovitz
3) (20 points) Function coding:
a) Write a function prototype, header and body for the void function setVal.
This function takes three integer parameters. It assigns the first two the value of
the third parameter. For example, assume the main program calls:
setVal(x, y, value);
if value has a 15, when after returning from the function x and y will also be
equal to 15.
prototype: void setVal(int &, int &, int);
header and body: void setVal(int & a, int & b, int c)
{
a = c;
b = c;
}
b) Write a function prototype, header, body and example of call for a value
returning function isTriple. isTriple accepts two parameters of type
double. It returns true if the second parameter is equal to three times the first
parameter and false otherwise. Write a prototype, header, body and example
call.
prototype: bool isTriple(double, double);
header and body: bool isTriple(double x, double y)
{
if (y == x * 3)
return true;
return false;
}
call: int one,two;
if (isTriple(one,two))
cout << “hi”;
4) (18 points)
CSC126
Exam 2/ Page 7
Spring 2007
Dr. Sarah Zelikovitz
Write a complete C++ program that accomplishes the task indicated below. Use good
form by including comments and meaningful identifiers. Be accurate with the syntax -as if you were typing the program on the computer.
Assume that there is a file on disk named SCORES.txt that contains information about
student results on four 25 point quizzes taken in a semester. Each line contains the
student name and 4 quiz scores. A few lines in the file are shown:
Jim
21
Sam 22
Chris 24
23
19
25
25
18
23
20
21
25
.
.
Write the program on the assumption that you do not know how many students are
in the class. You may assume however, that every student has four scores.
Write a C++ program that will do the following:
<i> Open the file and read in the data.
<ii> Call a value returning function that returns the total of the four scores (e.g. Jim’s
total would be 89). Make sure that you write the code for this function.
<iii> Compute the class average (of the total scores) accurate to the nearest tenth.
<iv> Print the output so that it is organized as below. (Of course there will be more
lines, because this is shown only for the lines above). All printing is done in the main
function.
STUDENT TOTAL
Q1
Q2
Q3
Q4
----------------------------------------------------------------------Jim
89
21
23
25
20
Sam
80
22
19
18
21
Chris
97
24
25
23
25
CSC126
Exam 2/ Page 8
Spring 2007
Dr. Sarah Zelikovitz
Class Average: 88.7
//Programmer:
//Date:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
//prototype for total function
double getTotal (double, double, double, double);
//this program computes total scores for students
//in a class based on three quizzes
int main()
{
string name;
double score1, score2, score3, score4, total;
double sumOfTotal=0; //to hold the sum of all students' grades
int count=0; //counter for the number of students
ifstream infile;
infile.open("scores.txt");
if (!infile)
{
cout << "error opening file";
return 1;
}
cout << fixed << setprecision(2);
//output to two decimal
//printing titles on the output screen
cout << "Name" << '\t' << setw(8)<<"score1" << setw(8)<< "score2"
<< setw(8) << "score3" << setw(8) << "score4"<<setw(8)
<< "total" << endl;
//loop to process each of the students
while (!infile.eof())
{
infile >> name >> score1 >> score2
>> score3 >> score4;
//call the function getTotal to retrieve the total score
total = getTotal(score1,score2,score3,score4);
CSC126
Exam 2/ Page 9
Spring 2007
Dr. Sarah Zelikovitz
cout << name << '\t' << setw(8)<<score1 << setw(8) <<score2
<< setw(8) << score3 << setw(8) << score4 << setw(8)
<< total << endl;
//accumulate a sum of all totals to find the average;
sumOfTotal += total;
//count this student
count++;
}
cout << endl << "Average grade is: " << sumOfTotal/count;
return 0;
}
double getTotal (double x, double y , double z, double w)
{
return x + y + z + w;
}
Download