Sie sind auf Seite 1von 6

EECS

183 Fall 2012 Exam 3


Part 2: Free Response
Closed Book Closed Notes Closed Electronic Devices Closed Neighbor
Turn off Your Cell Phones
We will confiscate all electronic devices that we see including cell phones

UM unique name: ________________________ @umich.edu


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. All code and input is written in a fixed width font. If it looks like a space, it is a space.
6. Write clearly. If we cannot read your writing, it will be marked wrong.

7. 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. __________________________________________________________________

Question 1
Question 2 a
Question 2 b
Question 4
Total

Points
Possible
30
40
40
30
140

Points Received

1) Consider the following code fragment. What is the output?


(Hint: begin with int main())
const int MAX_SIZE = 10;
class Holder {
public:
Holder(int s); // Requires s < MAX_SIZE
Holder();
bool canInsert();
void insert(int item);
void remove(int item);
void print();
private:
int size;
int items[MAX_SIZE];
};
Holder::Holder() {
size = 0;
}
Holder::Holder(int s) {
size = s;
for (int i = 0; i < size; ++i) {
items[i] = 0;
}
}
bool Holder::canInsert() {
return size < MAX_SIZE;
}
void Holder::insert(int item) {
if (!canInsert()) return;
items[size] = item;
size++;
}
void Holder::remove(int item) {
for (int i = 0; i < size; ++i) {
if (items[i] == item) items[i] = 0;
}
}
2

void Holder::print() {
for (int i = 0; i < size; ++i) {
cout << items[i] << " ";
}
cout << endl;
}
int main() {

Holder h(1);

h.print();


h.insert(3);
h.insert(4);
h.print();


h.insert(3);
h.print();

h.remove(3);
h.print();

for (int i = 0; i < MAX_SIZE; ++i) {



h.insert(100);
}

cout << h.canInsert() << endl;

return 0;

0
034
0343
0040
0

2 a) Write the code


Imagine that you want to model a course at the University of Michigan.
A student has
1) name
2) age
Every course has a
1) name
2) courseID
3) professor
4) numStudents
5) all information of all Students in the Course

The maximum number of students that any course can have is MAX_STUDENTS;

Create structs to model this situation

const int MAX_STUDENTS = 400;
// Write the definition of your struct(s) here.

struct Student {

string name;


int age;



};






struct Course {


string name;


int courseId;


string professor;

int numStudents;


Student students[MAX_STUDENTS];
};




2 b) A In a main() function, create the following 2 students.


name
Arnold
Timmy

age
9
12

Also create the following two courses.


name
Science
Math

courseID
32991
92911

professor
Ms. Frizzle
Mr. Crocker

numStudents
1
1

students
Arnold, 9
Timmy, 12

// Create the two above students/courses here.


int main() {


Student arnold = {"Arnold", 9};






Student timmy = {"Timmy", 12};








Course science = {"Science", 32991, "Ms. Frizzle", 1, arnold};







Course math = {"Math", 92911, "Mr. Crocker", 1, timmy};














return 0;
}

3. (30 pts) Draw a line through unnecessary code that still allows these two

functions to work -- if there is any unnecessary code.

// this code is within file: HelperFunctions.h


#ifndef HELPERFUNCTIONS_H
#define HELPERFUNCTIONS_H
int correctValue(int value, int upperBound);
int correctValue(int value);
#endif

// the following code is within HelperFunctions.cpp
#include "HelperFunctions.h"
#include "Graphics.h"
#include "Point.h"
#include "Line.h"
#include "Color.h"
#include "Circle.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

int correctValue(int value) {
if (value > 255) return 255;
else if (value < 0) return 0;
else return value;
}

int correctValue(int value, int upperBound) {
if (value >= upperBound) return upperBound - 1;
if (value < 0) return 0;
return value;
}

Das könnte Ihnen auch gefallen