Structs¶
Q) Compute number of quizzes¶
Given the following C++ struct and variable declaration which stores information about a class, including the course's name, and the number of quizzes and number of exams in the class:
struct course_t {
string name;
int numQuizzes;
int numExams;
};
course_t firstCourse;
Which of the following computes the number of quizzes and exams in the course?
a) int count = numQuizzes + numExams;
b) int count = course_t.numQuizzes + course_t.numExams;
c) int count = firstCourse.numQuizzes + firstCourse.numExams;
d) int count = numQuizzes.course_t + numExams.course_t;
Q) computeAverageHouseholdSize()
function:¶
Imagine that the struct named country_t stores a huge amount if information about a country (say a megabyte of information). If there were a function that was going to compute the average household size from the data contained in this struct, which of the following would be the best prototype for such a function?
a) double computeAverageHouseholdSize(const country_t &country);
b) double computeAverageHouseholdSize(country_t &country);
c) double computeAverageHouseholdSize(const country_t country);
d) double computeAverageHouseholdSize(country_t country);
Q) Consider the following C++ code to represent buckets for water:¶
struct bucket_t {
string owner;
string colour;
double capacity;
};
double totalCapacities(vector<bucket_t> buckets)
{
double total = 0;
for (unsigned int i = 0; i < buckets.size(); i++) {
// XXXXXXXX
}
return total;
}
Which of the following best replaces the line XXXXXXXX so that the function can add up the capacity of all buckets?
a) total += buckets.capacity.at(i);
b) total += buckets.capacity(i);
c) total += buckets(i).capacity;
d) total += buckets.at(i).capacity;
Answers¶
1 = c) int count = firstCourse.numQuizzes + firstCourse.numExams;
2 = a) double computeAverageHouseholdSize(const country_t &country);
3 = d) total += buckets.at(i).capacity;