Given an array of size 7 named arr, how would you access the final element?
arr[6];
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];
Which members of Student are accessible outside of the Student clas?
Everything under "public:"
Student();
Student(int umid_in, string uniqname_in);
Student(int umid_in, string uniqname_in, int[] grades, int num_grades);
void set_umid(int umid_in);
void set_uniqname(string uniqname_in);
void set_gpa(int[] grades, int num_grades);
int get_umid();
string get_uniqname();
double get_gpa();
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
What day and time is the exam?
Wednesday, November 11th at 8:00pm.
By default, arrays are pass by ____
Reference
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}};
Option 1
Write the definition of get_umid and set_umid as they would appear in Student.cpp.
int Student::get_umid() {
return umid;
}
void Student::set_umid(int umid_in) {
umid = umiid_in;
}
Which of the following functions should you avoid using?
a) .good()
b) .fail()
c) .eof()
d) .clear()
e) .is_open()
c) .eof()
What are some resources to study for the exam?
Lecture slides/recordings, lab slides/recordings, Problem Roulette, past exams (take these!!!!), office hours
Declare and initialize an array of strings named days which contains a string representing 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
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
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 corresponding arguments, and set gpa to 0.
Student::Student(int umid_in, string uniqname_in) {
umid = umid_in;
uniqname = uniqname_in
gpa = 0;
}
Declare a input file stream named input_file, open "data.txt", and read in a variable named x. Assume x has already been declared.
ifsteam input_file;
input_file.open("data.txt");
input_file >> x;
//should always do this!
input_file.close();
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, constructors, and member variables.
Silly.cpp: Implementation of member functions and constructors.
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;
}
Consider the function:
int find_target(char target, char arr[][], int rows, int cols).
This function returns the sum of the indices target was found at. 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;
}
}
return 0;
}
Define the set_gpa function as it would appear in Student.cpp. It should use the calculate_gpa function to determine the student's gpa.
void Student::set_gpa(int[] grades, int num_grades) {
gpa = calculate_gpa(grades, num_grades);
}
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";
}
What is the name for this:
::
Scope resolution operator
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;
}
Write a function named find_max, 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 = 0;
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;
}
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");
int grades[] = {93, 95.6, 82.9, 88.7};
s1.set_grades(grades, 4);
cout << s1.get_uniqname() << ' ' << s1.get_gpa();
Declare an output file stream named out_file, and open "write.txt". After opening "write.txt", continually read from user input, store the value in x, and write x to the file. Assume x has already been declared. While x is being written to output_file, print "Writing to file".
ofstream out_file("write.txt");
while (cin >> x) {
output_file << x;
cout << "Writing to file";
}
What is the difference between pass by reference and pass by value?
Pass by reference: The original variable passed into the function is directly modified by the function call
Pass by value: During the function call, a copy of the argument is created, which is only in scope during the function call.