Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Write your uniqname on the top of each page of the exam (excluding the first page).
You are responsible for the legibility of your work. Any work that is not clear to the grader due to sloppiness
or poor handwriting will be marked incorrect.
You must read and sign the Honor Code below after you have completed the exam.
Circle your Lab Section below (you will lose 1 point if you dont do this or it is incorrect):
101:
102:
103:
104:
105:
106:
107:
108:
109:
201:
202:
203:
204:
205:
206:
207:
208:
209:
Number
301:
302:
303:
304:
305:
306:
307:
308:
309:
II
III
Oz Mon, 8:30-10:30a
Oz Mon, 10:30a-12:30p
Jeff Mon, 12:30-2:30p
Sumit Mon, 4:30p-6:30p
Oz Wed, 8:30-10:30a
Jeff Wed, 10:30a-12:30p
Jeff Wed, 12:30-2:30p
Sumit Wed, 2:30-4:30p
Sumit Wed, 4:30p-6:30p
IV
Total
Grade
Signature:_________________________
Page 1 of 10
Uniqname: ____________________________
The remainder of this page is intentionally left blank.
Page 2 of 10
Uniqname: ____________________________
Question 1 - Matching (10 points)
Match each concept on the left-hand side with the phrase on the right that fits best. Note that there are
more items in the right column than in the left, so some answers will not be used.
_____ #include directives
_____ {
_____ insertion
_____ ==
_____ bool
G. part of scaffolding
_____ main
_____ %
_____ double
Page 3 of 10
Uniqname: ____________________________
Question 2 - Multiple Choice (20 points)
Check the single most appropriate answer for each.
a) If the command g++ myprog.cpp -o out completes without errors or warnings it means:
____
____
____
____
____
=
/
+
%
c) Suppose the function mystery has been defined with the signature int mystery(int baz);
What is the value of bar after the following loop executes?
int foo, bar = 0;
while (bar < 30) {
foo = mystery(bar);
bar++;
}
____
____
____
____
____
0
mystery(29)
mystery(30)
30
cannot tell from information given
d) Which of the following captures the formula 5.5x in a C++ expression (assume x is a positive
integer)?
____
____
____
____
____
5.5 * x
(5.5 * x) / 1
(5.5 * x) % 1
(55 * x) / 10
None of the above
Page 4 of 10
Uniqname: ____________________________
e) Which of the following prints GoBlue! on a single line of output?
____
____
____
____
____
cout
cout
cout
cout
cout
<<
<<
<<
>>
>>
if
if
if
if
if
(1+1
(2+2
true
(not
(not
g) Which of the following declarations signals a violation of our strict distinction between functions and
procedures?
____
____
____
____
____
Page 5 of 10
Uniqname: ____________________________
Question 3 What does the code do? (35 points)
In the space provided beside each of the following six programs, write one of the following:
The screen output of the program, using underscore to denote any spaces (ie, 6_24_42)
Does not compile if the program as written would not compile
Infinite loop if the program would enter an infinite loop and never finish during runtime
Assume that each program has #include <iostream> and using namespace std; at the
beginning. Do not put any scratch work in the answer box.
int main() {
int year = 1984;
while(year > 0)
{
cout << year % 10;
year = year / 10;
}
return 0;
}
int main() {
double x = 0;
int n = 1;
while (n<5)
{
if (n%2==1)
cout << n << " ";
x = x + n;
n++;
}
cout << x;
return 0;
}
int main() {
int year = 1900;
cout << year;
if (year % 4 == 0)
cout << " is";
if (year % 100 == 0)
cout << " is not";
else if (year % 400 == 0)
cout << " is";
cout << " a leap year";
return 0;
}
Answer:
Answer:
Answer:
Page 6 of 10
Uniqname: ____________________________
int main() {
double x = 0;
int n=1;
while(n<5);
{
x = x + 2/n;
n++;
}
cout << x;
return 0;
}
Answer:
Answer:
int main() {
int evil = 2, good = 0;
while(evil > good)
{
adjust(evil, good);
}
return 0;
}
void cycle(int a) {
while (a != 1) {
cout << a << " ";
switch (a%2) {
case 1:
a = 3*a+1;
break;
default:
a = a/2;
}
}
return;
}
Answer:
int main(){
cycle(6);
return 0;
}
Page 7 of 10
Uniqname: ____________________________
Question 4 Write some code (35 points)
For this question, you must write code for the main() function in the space provided on the next page
(you have already been given the scaffolding). Your completed program will help a group of students
decide at which location they should hold a party. The location data where your program will gather its
input is stored in a file called locations.txt. The information that your program calculates will be
output to a pair of files called options.txt and ignores.txt, and also to the screen. Make sure
to open files before they are used and close them when you are finished. Each line in
locations.txt corresponds to a location where the party could be held, formatted as follows:
<locNum> <capacity> <cost> <xCoord> <yCoord>
where:
<locNum>
is the location number expressed as an integer
<capacity>
is the number of people the location can hold expressed as an integer
<cost>
is the total cost of the location expressed as a double
<xCoord> and <yCoord>
are the x and y coordinates of the location expressed as integers
In order for your program to be correct, it must do the following:
1. Prompt the user to enter the number of people that will be attending the party by outputting the
following message to the screen: Number of people:
2. Read the number of people, numPeople, from the standard input stream.
3. Using iteration, continually read data from the input file locations.txt until the end of the file
is reached. Data from the file must be read one line at a time. In other words, your program must
read data from the file in groups of 5 values each time it is read. You can assume that the input file
will be properly formatted and will always contain at least one line of data.
4. For each location in the file read in step 3, check if the locations capacity is large enough to
accommodate the number of people that will be attending the party that was input in step 2. Then,
a. If the location can accommodate the party, output the following on single line to the
options.txt file:
<locNum> <costPerPerson> <distance>
where:
<locNum> is the current location number expressed as an integer
<costPerPerson> is the cost of the current location per person who will attend the
party expressed as a double
<distance> is the distance to the current location expressed as a double calculated using
the following equation: = 2 + 2 where xCoord and yCoord are
the x and y coordinates of the current location that were read from the file in step 3.
b. If the location cannot accommodate the party, output the following on single line to the
ignores.txt file:
<locNum>
where:
<locNum> is the current location number expressed as an integer
5. Once all locations have been read from the file, if no locations were able to accommodate the party
in step 4a, output the following message to the screen:
The party is canceled!
If there were locations that could accommodate the party, output the following to the screen:
There are <numOps> options!
where:
<numOps> is the number of locations that can accommodate the party expressed as an integer.
Note: The last page of the exam contains some examples of how your program should function.
ENGR 101: Intro to Computers & Programming - Fall 2011
Page 8 of 10
Uniqname: ____________________________
Note: This question can be completed using fewer than 30 lines of code.
Page 9 of 10
Uniqname: ____________________________
Contents of the
locations.txt
input file
123 50 350.0 3 4
456 200 240.0 7 7
789 100 100.0 2 1
Contents of
options.txt output file
Screen
Output
Number of people:
50
There are 3 options!
Screen
Output
Number of people:
400
There are 2 options!
Screen
Output
Number of people:
1000
The party is canceled!
123 7 5
456 4.8 9.89949
789 2 2.23607
Contents of
ignores.txt output file
Contents of the
locations.txt
input file
111
123
456
789
700
450
200
100
800.0
600.5
240.5
100.0
9
3
7
2
10
4
7
1
Contents of
options.txt output file
111 2 13.4536
123 1.50125 5
Contents of
ignores.txt output file
456
789
Contents of the
locations.txt
input file
Contents of
options.txt output file
Contents of
ignores.txt output file
123
456
789
Page 10 of 10