Pic 10A Midterm 1 - Answers
Pic 10A Midterm 1 - Answers
Pic 10A Midterm 1 - Answers
The following code snippets produce some output to the console. Write the output.
a)
int x = 19;
while (x > 2) {
x = x/2;
cout << x;
if (x%2 == 0)
cout << "\n";
}
b)
string q = "7";
int p = 7;
cout << q + q << " 7+7 " << p+p;
c)
string s="Simpsons";
int index=0;
while (index < s.length() )
{
s.erase(index,1);
index++;
cout << s << "\n";
}
d)
cout << "\this \"C++\"";
cout << "programmi\ng";
cout << "is fun!";
3. [20pts] Functions
a) Write a function prototype for a function called is_leap_year. The function returns a
boolean value (true/false) and takes as an argument an integer n.
b) Assume that you have a function called is_leap_year that determines if a given year is a
leap year or not. A description of this function's prototype is given in part a). It takes as an
argument a year (integer) and returns true if the year is a leap year, otherwise it returns
false. Use this function to write a program where you ask the user for a year and then call
the function is_leap_year to output to the console either that the year that the user entered
was a leap year or that the year that the user entered was not a leap year. Sample run:
Please enter a year.
1945 (user enters)
1945 is not a leap year.
c) Write the function definition for is_leap_year. A year is a leap year if it is divisible by four
(for example, 1980), except it is not a leap year if it is divisible by 100 (for example, 1900);
however, it is a leap year if it is divisible by 400 (for example, 2000).
4. [20pts] Graphics
You may assume that the following graphics project has been set up for you, and you only
need to write a snippet of code for the ccc_win_main routine.
Ask the user for an integer and store this is an int variable n (note we are in the graphics
environment!.) Then prompt the user to click on the screen n times and each time draw a
point where the user clicks. Then draw a line connecting the origin and the point that the user
clicked.