Pic 10A Midterm 1 - Answers

5

Click here to load reader

description

Pic 10A - Virtanen. Fall 2014 MT1. UCLA.

Transcript of Pic 10A Midterm 1 - Answers

Page 1: Pic 10A Midterm 1 - Answers

1. [20pts, 5pts each] Console fun!!!

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!";

Page 2: Pic 10A Midterm 1 - Answers

2. [20 pts] Nested for loops

Write a code snippet that asks a user for an int n and then outputs a "triangle" using asterisks (*). That is, output to the console n rows of asterisks where the first row has n asterisks, the second row has n-1 asterisks and so on. For example, if the user enters 5, the output to the console should be:

***** **** *** ** *

NOTE: Pay attention to where the spaces are. This is slightly different from the problem we did in class!

Hint: There are many ways to do this problem. Here are two possibilities: One is, for each row, build a string that has the appropriate number of *s. Then use setw. Another way is to use an if statement inside some loop to decide whether a * or a space should be outputted.

,

Page 3: Pic 10A Midterm 1 - Answers

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).

Page 4: Pic 10A Midterm 1 - Answers

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 userclicked.

Page 5: Pic 10A Midterm 1 - Answers

5. [20pts] Strings and loops

Write a complete console program starting from #include. Ask the user for a sentence and store it in a string s. Output to the screen the number of words in the sentence. Hint: count the spaces.