Page 1 of 4 Exam #2 Engr 101: Intro to Algorithms and Programming Section 400 ? Fall 2007 ? You have 2 hours to complete this exam. ? You are allowed to use the books, your notes, and a calculator. ? You are not allowed to use any electronic devices that allow the passing of messages or data between others. ? You are not allowed to access the internet or use a computer. ? This exam contains four multi-part questions. ? Write your name on the top of each page of the exam. ? 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. Printed Name: _____________________________________ Lab Section: ______________________________________ Date: ____________________________________________ Sign the Honor Code: I have neither given nor received unauthorized aid on this examination, nor have I concealed any violations of the Honor Code. Signature: __________________________ Question 1 ? Short Answer (15 Points) a. Circle either T or F indicating whether the conditional statement evaluates to true (T) or false (F). (8 pts) T / F (4 < 3 < 2) T / F !((10/3 != 3) and 1 == 1) T / F (1 or !!!(9/2 > 4) + 3 > !(2 > 3.0)) T / F (9>=7 and !(5-3==2) || 6-(-14) <= 10) b. Convert the following 12-bit binary numbers into decimal (base 10). Write your results in the spaces provided. (2 pts) 010101100101 = ______________ 000111001100 = ______________ The following table will be of value to you: 1024 512 256 128 64 32 16 8 4 2 1 2 10 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 c. Do the following arithmetic and write your results in binary in the spaces provided below each equation. Your answers should be 12-bits in length. (4 pts) 010010100110 001011010111 ? 010010001101 + 001011000010 --------------------- --------------------- d. Negate the following binary number and write the resultant binary number in the space provided. (1 pt) 001110111000 = ______________ Q1 Q2 Q3 Q4 Total __ / 15 __/20 __ / 30 __ / 35 __ / 100 NAME: ___________________________________________ Page 2 of 4 Question 2 ? Compute the values (20 points) Examine the function definitions below and the expressions to the right. Use the definitions to determine what each expression will evaluate to and write your answer in the space provided. The contents of the file entitled ?input.dat? are given below. int scout(int N) { ifstream inFile("input.dat"); int num, x=0; while (!inFile.fail()) { inFile >> num; if (!inFile.fail()) { if (num < N) x++; else if (num > N) x--; } } inFile.close(); return x; } int ranger(int N) { ifstream inFile("input.dat"); int num, x=N, y=N; while (!inFile.fail()) { inFile >> num; if (num < x) x = num; else if (num > y) y = num; } inFile.close(); return (y-x); } input.dat -3 5 2 -15 13 -2 7 -9 10 4 -12 11 3 14 -4 -7 b) ranger(20) c) ranger(scout(7)) d) scout(ranger(-10)) a) scout(9) NAME: ___________________________________________ Page 3 of 4 Question 3 ? What is the output? (30 points) Write what is output to the screen by the program on the right in the box below. Then, write the final contents of the file ?out.txt? after the program is run in the box below. The contents of the input file, ?in.txt?, are provided below the program. You must do this without the use of a computer. #include #include using namespace std; int punksRevenge(int & x, int & y) { if (x >= 5) y++; else if (x >= 0) x++; else x = (x*y)/y + x - y - 1; return x; } int main() { int num; ifstream inFile("in.txt"); ofstream outFile("out.txt"); while (!inFile.fail()) { inFile >> num; if (!inFile.fail()) { outFile << punksRevenge(num, num) << " "; cout << punksRevenge(num, num) << endl; } } outFile << endl; cout << endl; inFile.close(); outFile.close(); return 0; } in.txt -15 13 7 0 4 -4 -7 Program Screen Output Contents of ?out.txt? NAME: ___________________________________________ Page 4 of 4 Question 4 ? Write a program (35 points) In the space provided on the right, write a short program that will output the following "picture" of a leaf to the file "graph.txt" using only the upper case letters 'X' and periods. You program cannot simply output lines of text that correspond to the rows in the picture. Instead, it must make use of (among other things) nested loops and selection. The picture consists of 16 rows and 21 columns. Hint: Create a table of x and y values wherever there is an ?X?. Then use this to create conditional statements that will help produce the picture. You may need to break the picture up into regions. ..........X.......... ........X.X.X........ ......X...X...X...... ....X.....X.....X.... ..X.......X.......X.. XXXXXXXXXXXXXXXXXXXXX ..X.......X.......X.. ....X.....X.....X.... ......X...X...X...... ........X.X.X........ ..........X.......... ..........X.......... ..........X.......... ..........X.......... ..........X.......... ..........X.......... Program root Microsoft Word - Exam 2.doc
STUDYBLUE makes things that make you better at school.
Things like
online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free.
Join us.
“Simply amazing. The flash cards are smooth, there are many different types of studying tools, and there is a great search engine. I praise you on the awesomeness.”
Dennis