1-D Array
2-D Array
File Streams
Classes
Misc.
100

Given an array of size 7 named arr, how would you access the final element?

arr[6]; 

100

Given a 2-D array with 5 rows and 9 columns named arr, access the element in the final row and 6th column. 

arr[4][5]; 

100

Why is it important to close file streams after you are done using them?

To avoid accidentally reading/writing to a file when you don't want to. Prevents modifying data that should be left alone

100

Which members of Student are accessible outside of the Student clas? 

Everything under "public"


Student();

  Student(int umid_in, string uniqname_in);

  int get_id();

  void set_id(int umid_in);

  string get_uniqname();

  void set_uniqname(string uniqname_in);

  double get_gpa();

 void set_grades(double c_one, double c_two, double c_three, double c_four);

100

What day and time is the exam? 

Wednesday, March 25th @ 6:00pm

200

Arrays are pass by ____

reference

200

Which of the following are invalid declarations of a 2-D array?:

1) int arr[4][];

2) int arr[4][4]

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

1

200

Which of the following functions should you avoid using?

a) .good()

b) .fail()

c) .eof()

d) .clear()

e) .is_open()

c) .eof()

200

Write the definition of get_id and set_id as they would appear in Student.cpp.  

int Student::get_id() {

return umid; 

}

void Student::set_id(int id_in) {

umid = id_in;

}

200

What are some resources to study for the exam?

Lecture slides/recordings, lab slides/recordings, Problem Roulette, past exams (take these!!!!), office hours

300

Declare and initialize an array of strings named days to hold each day of the week

string days[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday};

//note that including the size is optional, since it will be implicitly assigned 

300

Declare and initialize a 2-D array with 3 rows and 3 columns of ints named arr. It should contain 1-9 in ascending order. 

int arr[3][3] = {{1, 2, 3},

                        {4, 5, 6},

                        {7, 8, 9}};

//note: also valid to exclude the number of rows with the initializer list

300

Declare a input file stream named input_file, open "data.txt", and read in a variable named x. 

ifsteam input_file;

input_file.open("data.txt");

input_file >> x;

//should always do this!

input_file.close();

300

Write the definition of Student(int umid_in, string uniqname_in) as it would appear in Student.cpp. This constructor should set the umid and uniqname to the passed arguments, and set gpa to 0. 

Student::Student(int umid_in, string uniqname_in) {

umid = umid_in;

uniqname = uniqname_in

gpa = 0; 

}

300

You are creating a class named Silly. What would you put in Silly.h, and what would you put in Silly.cpp?

Silly.h: Class declaration, declaration of functions and member variables. 


Silly.cpp: Implementation of member functions

400

Consider the following function: double find_mean(int arr[], int size); Write the implementation of this function. It should calculate the mean of arr. Note that the values in arr[] are already sorted, and size >= 1.

double find_mean(int arr[], int size) {

double total = 0; 

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

total += arr[i]; 

}

return total / size; 

}

400

Consider the function: int find_target(char target, char arr[][], int rows, int cols). This function returns the sum of the row and column the target char was found on. Complete the definition. Note that target is guaranteed to appear once. 

int find_target(char target, char arr[][], int rows, int cols) {

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

      for (int j = 0; j < cols; ++j) {

            if (arr[i][j] == target) return i + j; 

      }

}

}

400

Check if the file stream input_file successfully opened its file. If it didn't, print an error message. 

if (!input_file.good()) {

cout << "Error in opening file";

}

400

Define the calculate_gpa function. It should calculate the average of the students grade. If avg > 92, gpa = 4.0. If avg > 82, gpa = 3.5. If avg > 72, gpa = 3.0. Otherwise, gpa = 2.5

double calculate_gpa(int grades[], int num_grades) {

    int avg = 0; 

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

      avg += grades[i];

    }

    avg /= 4; 

    if (avg > 92) {

      return 4.0;

    }

    else if (avg > 82) {

      return 3.5;

    }

    else if (avg > 72) {

      return 3.0;

    }

    else {

      return 2.5;

    }

  }

400

What is the name for- ::

Scope resolution operator

500

Consider the following function: double find_median(int arr[], int size); Write the implementation of this function. It should calculate the median of arr[]. Note that the values in arr[] are already sorted, and size >= 2.

double find_median(int arr[], int size) {

double median = 0; 

if (size % 2 != 0) {

median = arr[size / 2]; 

}

else {

median = (arr[n / 2 - 1] + arr[n / 2]) / 2.0; 

}

return median; 

}

500

Write a function and its header which takes an int 2-D array as input, and returns the sum of the indices of the largest element in the array. Note that the array is not sorted, and there are no negative values. 

int find_max(int arr[][], int rows, int cols) {

int max = 0;

int max_indices = -1;  

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

     for (int j = 0; j < cols; ++j) {

          if (arr[i][j] > max) {

              max = arr[i][j];

              max_indices = i + j; 

}

}

}

}

return max_indices; 

}

500

Declare an output file stream named out_file, and open "write.txt". Use a for loop to continually write a variable x to the file. While x is being written to output_file, cout << "Writing to file"; 

ofstream output_file("write.txt");

while (output_file << x) {

cout << "Writing to file";

}

500

In main, complete the following steps:

1. Create a default constructed Student named s1

2. Set the UMID to 54346728 and the uniqname to "student"

3. Set the students grade with 93, 95.6, 82.9, and 88.7 as the arguments

4. Print the students uniquname and GPA seperated by a space

Student s1;

s1.set_id(54346728);

s1.set_uniqname("student);

s1.set_grades(93, 95.6, 82.9, 88.7);

cout << s1.get_uniqname() << ' ' << s1.get_gpa();

500

What language is C++ derived from?

C, C++ was originally called C with classes!

M
e
n
u