Docsity

Exam 3 for EECS 183 - Winter 2012 - Prof. Mary Lou Dorf, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
EECS 183 Winter 2012 Exam 3
Part 2 (145 points)
Closed Book Closed Notes Closed Electronic Devices Closed Neighbor
Turn off Your Cell Phones
Analyze / Write Small Programs
Name:
UM unique name: @umich.edu
Discussion section: (circle)
Eva(Tue 9-10) Eva/JT(Tue 10-11) JT(Tue 11-12) Anthony(Tue 1-2) Nisha(Tue 2-3)
Alex(Tue 3-4) Stefan(Tue 4-5) Anthony(Wed 4-5) Nick(Thu 10-11) Alex(Thu 3-4) Robert(Thu 4-5)
Instructions: Read Carefully!
1. This is a closed book exam. You may not refer to any materials during the exam.
2. Some questions are not simple, therefore, read carefully.
3. Assume all code and code fragments compile, unless otherwise specified.
4. Assume/use only the standard ISO/ANSI C++.
5. Write clearly. If we cannot read your writing, it will be marked wrong.
6. This course operates under the rules of the College of Engineering Honor Code. Your
signature endorses the pledge below. After you finish your exam, sign below:
I have neither given nor received aid on this examination, nor have I concealed any
violations of the Honor Code.
Points Possible
Points Received
Question 1
30
Question 2
40
Question 3
40
Question 4
35
Total
145
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Exam 3 for EECS 183 - Winter 2012 - Prof. Mary Lou Dorf and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

EECS 183 Winter 2012 Exam 3

Part 2 (145 points)

Closed Book Closed Notes Closed Electronic Devices Closed Neighbor

Turn off Your Cell Phones

Analyze / Write Small Programs

Name:

UM unique name: @umich.edu

Discussion section: (circle)

Eva(Tue 9-10) Eva/JT(Tue 10-11) JT(Tue 11-12) Anthony(Tue 1-2) Nisha(Tue 2-3) Alex(Tue 3-4) Stefan(Tue 4-5) Anthony(Wed 4-5) Nick(Thu 10-11) Alex(Thu 3-4) Robert(Thu 4-5)

Instructions: Read Carefully!

1. This is a closed book exam. You may not refer to any materials during the exam.

2. Some questions are not simple, therefore, read carefully.

3. Assume all code and code fragments compile, unless otherwise specified.

4. Assume/use only the standard ISO/ANSI C++.

5. Write clearly. If we cannot read your writing, it will be marked wrong.

6. This course operates under the rules of the College of Engineering Honor Code. Your

signature endorses the pledge below. After you finish your exam, sign below:

I have neither given nor received aid on this examination, nor have I concealed any

violations of the Honor Code.

Points Possible Points Received

Question 1 30

Question 2 40

Question 3 40

Question 4 35

Total 145

1. What does the following program print?

#include

using namespace std;

void doSomething( int a[], int n);

void printArray (const int a[], int n);

int main()

int a1[] = {1, 2, 3, 4};

int a2[] = {4, 3, 2, 1};

int a3[] = {4, 5, 1, 2, 1, 3};

doSomething(a1, 4);

doSomething(a2, 4);

doSomething(a3, 6);

printArray (a1, 4);

printArray (a2, 4);

printArray (a3, 6);

return 0;

// Requires: The size of array a is n

// Effects: ???

void doSomething(int a[], int n)

for (int i=1; i<n; i++)

for (int j=i; j>0 && a[j]<a[j-1]; j--)

int temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;

// Requires: The size of array a is n

// Effects: Prints all the elements of array a

void printArray(const int a[], int n)

for (int i=0; i<n; i++)

cout << a[i] << " ";

cout << endl;

Answer:

2. Complete the following program by writing the function charArrayToInt that takes

a char array containing only chars '0' to '9' and returns the corresponding int.

#include #include using namespace std; int charArrayToInt(const char c[], int n); int main() { char c1[] = {'1', '2', '3', '4', '5', '6'}; char c2[] = {'0', '0', '1', '2', '3', '4', '5', '6'}; char c3[] = {'9', '9', '8', '0', '0', '1', '0', '0'}; cout << charArrayToInt(c1,6) << endl; // Expected output: 123456 cout << charArrayToInt(c2,8) << endl; // Expected output: 123456 cout << charArrayToInt(c3,8) << endl; // Expected output: 99800100 return 0; } // Requires: The size of c is n && c only contains chars '0' to '9' inclusive // Effects: Returns the int corresponding to c, as in above examples int charArrayToInt(const char c[], int n) { }

This page is intentionally left blank.

This page is intentionally left blank.

4. What does the following program print? The program consists of three files: Map.h,

Map.cpp, and Client.cpp.

Client.cpp #include "Map.h" #include using namespace std; int main() { Map m; m.insert(1, 10); m.insert(2, 20); m.insert(3, 30); cout << m.lookup(3) << endl; m.insert(2, 21); cout << m.lookup(2) << endl; cout << m.size() << endl; m.remove(1); cout << m.size() << endl; m.remove(30); m.remove(1); cout << m.size() << endl; return 0; } Map.h const int MAXSIZE = 100; class Map { public: Map(); void insert(int key, int value); void remove(int key); int lookup(int key) const; int size() const; private: int key [MAXSIZE]; int value[MAXSIZE]; int n; };

Answer:

This page is intentionally left blank.