e01 : Midterm Exam-I

num ready? description exam date
e01 true Midterm Exam-I Wed 04/24 09:30AM

Instructions for the exam

Collaborative notes

A fun way to prepare for the exam is to work with your classmates to create notes collaborative. I have created a document with the above list of topics. Jump right in and add content, code examples and other useful information. I will review and comment your notes as will the tutors and TAs! Students who contribute will get star points :)

Click here to contribute to Collaborative Notes

Review notes

Review problems

Solutions available HERE

1) Given the head to a linked list, write a function to determine if there’s a cycle. (A cycle appears when a Node’s next pointer points to a Node previously visited)

2) Write two functions to return the sum of a linked list’s values - one iterative and one recursive.

3) Find and explain any errors in this class definition:

  class Student{
	public:
		Student(string name, int ID, double gpa){
			this.name = name;
			ID_number = ID;
			GPA = gpa;
		}
		string getName() const{ return name;}
		double getGPA() const{ return GPA;}
	private:
		string name;
                double GPA;
		const ID_number;
};

4) The StudentList class represents this year’s graduating class. Implement getAvgGPA() function. This should return the average GPA of the graduating class. Implement the getValedictorianName() function. The valedictorian is the student with the highest GPA. If there are multiple students with the highest GPA, return any one of their names.

class StudentList{
	public:
		StudentList(const StudentList& source);
		StudentList();
		~StudentList();
		string getValedictorianName() const; // implement this
		double getAvgGPA() const; 		    // implement this
	private:
		class Node{
			public:
		  Node();
			Node(Student& s);
			Student data;
			Node* next;
		};
		Node* head;
		Node* tail;
};

5) OutFit is a class that has objects of type ClothingItem as private member variables. (1) Implement the constructor for the OutFit class. (Keep in mind private member variables of a given class may only be accessed directly within that class’s member functions).

class ClothingItem{
	public:
		ClothingItem();
		~ClothingItem();
		string getBrand() const;
		string getColor() const;
		int getSize() const;
		void setBrand(string BrandOfItem);
		void setColor(string ColorOfItem);
		void setSize(int SizeOfItem);
	private:
		string brand;
                string color;
                int size;
};

class OutFit{
	public:
		OutFit(ClothingItem Top, ClothingItem Bottoms);		//Implement
		~OutFit();
		friend bool operator==(const OutFit& FirstOutfit; const OutFit& SecondOutfit); //implement 
			
	private:
		ClothingItem tShirt;		
		ClothingItem Pants;
};

6) Looking at the above classes, implement the == operator used to check if two outfits are the same. (Two outfits are considered the same if they have the same tShirt and same bottoms, two tShirts and two bottoms are considered the same if they are of the same brand, color, and size).

7) IntList is a class that defines a LinkedList of integers. Write a non-recursive implementation of the reverse() function that reverses the order of the linked list. The class definition is given below.

    class IntList {
    public:
    // constructor and 3 methods already done in intlist.cpp (NO CHANGE):
    IntList();              // constructor
    void append(int value); // append value at end of list
    void reverse(); // function to implement, reverse the linked list
    ~IntList();                      // destructor

    private:
    Node* reverse(Node *node); // helper function for reverse(), returns the new head node
    // definition of Node structure (DO NOT CHANGE):
    struct Node {
        int info;
        Node* next;
    };
    Node* first; // pointer to first node (DO NOT CHANGE):
    };

8) Write the reverse() function again, but this time recursively. You will likely need to use the private helper function to accomplish this. The private helper function returns a pointer to a Node, so try using it to return the last node (which will be the new first node in the list)

9) Consider the following definition of the class LinkedList:

class LinkedList {
public:
	LinkedList(){head = 0; tail = 0;}   //constructor
	~LinkedList();		//destructor
	void append(int value);	//already implemented, takes an integer value and adds a Node with that value to the end of the linked list
private:
	class Node{
		int data;
		Node *next;
	}
	Node *head;
	Node *tail;
	void clear(Node *h);   // Recursively deletes all the nodes starting at h
};

Implement the private method clear(Node *h)

10) Using the LinkedList definition from above, describe what happens when this code is run. Draw a pointer diagram if you need to.

LinkedList l1;
l1.append(1);
l1.append(2);
l1.append(3);
LinkedList l2(l1);

Solutions available HERE

Reference material

You are expected to know the material from the following lectures, labs and homeworks:

Study guide

Past exams

Link to the past exam is available on gauchospace.