QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1, version A Exam Fall 2015 10.13.15 Solutions 09.00am 09.50am, Tuesday, October 13, 2015 Problem 1 Write a complete C++ program that does the following. 1. It asks the user to enter the base and height of a triangle. 2. If either the base or height is less than or equal to 0, the program should immediately terminate. 3. The program calculates and prints the area of the triangle using the formula: Here is an example of how the program should work: Enter the base and height of a triangle: 8 10 Area: 40.0 double base, height; double area; Area = 1 Base Height 2 cout << "Enter the base and height of a triangle:"; cin >> base >> height; if ((base <= 0) (height <= 0)) area = base * height / 2.0; cout << "Area: " << area << endl; Problem 2 Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. No answer can use more than two lines. Assume the following variables have been declared and have legal values int x, y; string name, message; (a) Ask for, and read the value for name from the user. cout << "Enter your name: "; cin >> name; (b) Print the cube of x. cout << x * x * x << endl;
(c) Print the last digit of y. cout << y % 10 << endl; (d) If x is greater than y print the value of x, otherwise print the value of message. if (x > y) cout << x << endl; else cout << message << endl; (e) Print x copies of the value of name followed by y copies of the value of message. for (int a = 1; a <= x; a++) cout << name; for (int a = 1; a <= y; a++) cout << message; Problem 3 Consider the following C++ program. int p = 11, q = 13, r = 15; string a = "x"; string x = "xx"; if (a == x) cout << p; else cout << q; if ((p <= q) && (p <= r)) cout << "a" << endl; if ((a == x) ("a" == "x")) r++; cout << r << endl; if (!((p % 2) < (q % 2))) cout << "no"; while (p < r) p++; cout << p << "\n"; (a) What is the output at line (a)? 13 (b) What is the output at line (b)? a (c) What is the output at line (c)? 15 (d) What is the output at line (d)? // line (a) // line (b) // line (c) else cout << "yes"; // line (d) // line (e)
no (e) What is the output at line (e)? 15 Problem 4 Write a complete C++ program that asks the user for a number n and prints n triangles (each with height n) in a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: ** ** ** ** *** *** *** *** **************** (Each triangle should begin in the column after the previous one ends. Do not try to check whether the user input is legal or sensible.) int n; cout << "Enter the number n: "; cin >> n; for (int r = 1; r <= n; r++) { for (int triangle = 1; triangle <= n; triangle++) { for (int c = 1; c <=n; c++) { if (r >= c) cout << "*"; else cout << " ";
QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1, version B Exam Fall 2015 10.13.15 Solutions 11.10am 12.00noon, Tuesday, October 13, 2015 Problem 1 Write a complete C++ program that does the following. 1. It asks the user to enter a temperature in degrees fahrenheit. 2. If the temperature f does not satisfy 0 f 100 the program should print the message Out of range and terminate. 3. The program calculates and prints the celsius value of temperature using the formula: Here is an example of how the program should work: Celsius = 5 (F ahrenheit 32) 9 Enter the temperature in degrees fahrenheit: 68 Celsius: 20.0 double celsius, fahrenheit; cout << "Enter the temperature in degrees fahrenheit: "; cin >> fahrenheit; if (!((fahrenheit >= 0) && (fahrenheit <= 100))) { cout << "Out of range." << endl; celsius = 5.0 / 9 * (fahrenheit - 32); cout << "Celsius: " << celsius << endl; Problem 2 Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. No answer can use more than two lines. Assume the following variables have been declared and have legal values int x, y; string name, message; (a) Ask for, and read the value for name from the user. cout << "Enter your name: "; cin >> name; (b) Print the square of y.
cout << y * y << endl; (c) Print the last digit of x + y. cout << (x + y) % 10 << endl; (d) If x is not greater than y print the value of name, otherwise print the value of message. if (!(x > y)) cout << name<< endl; else cout << message << endl; (e) Print x copies of the value of x followed by y copies of the value of message. for (int a = 1; a <= x; a++) cout << x; for (int a = 1; a <= y; a++) cout << message; Problem 3 Consider the following C++ program. int p = 31, q = 23, r = 15; string a = "abc"; string x = "abc"; if (a == x) cout << p; else cout << q; if ((p <= q) && (p <= r)) cout << "a" << endl; if ((a == x) ("a" == "x")) r++; cout << r << endl; if (!((p % 2) < (q % 2))) cout << "no"; while (p < r) p++; cout << p << "\n"; (a) What is the output at line (a)? 31 (b) What is the output at line (b)? // line (a) // line (b) // line (c) else cout << "yes"; // line (d) // line (e) (c) What is the output at line (c)?
16 (d) What is the output at line (d)? no (e) What is the output at line (e)? 31 Problem 4 Write a complete C++ program that asks the user for a number n and prints n diagonal stripes (each with height n and width n) in a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: (Each stripe should begin in the column after the previous one ends. Do not try to check whether the user input is legal or sensible.) int n; cout << "Enter the number n: "; cin >> n; for (int r = 1; r <= n; r++) { for (int stripe = 1; stripe <= n; stripe++) { for (int c = 1; c <=n; c++) { if (r == c) cout << "*"; else cout << " ";
QUEENS COLLEGE Department of Computer Science CSCI 111 Midterm 1, version B Exam Fall 2015 10.13.15 Solutions 11.10am 12.00noon, Tuesday, October 13, 2015 Problem 1 Write a complete C++ program that does the following. 1. It asks the user to enter a temperature in degrees celsius. 2. If the temperature is greater than 40, the program should once ask the user to enter a different value. 3. The program calculates and prints the fahrenheit value of temperature using the formula: Here is an example of how the program should work: Enter the temperature in degrees celsius: 60 Enter a different value: 60 Fahrenheit: 140.0 double celsius, fahrenheit; F ahrenheit = 9 Celsius + 32 5 cout << "Enter the temperature in degrees celsius: "; cin >> celsius; if (celsius > 40) { cout << "Enter a different value: "; cin >> celsius; fahrenheit = 9.0 / 5 * celsius + 32; cout << "Fahrenheit: " << fahrenheit << endl; Problem 2 Write C++ statements to carry out the following tasks. Do not write complete programs, just give a few lines of C++ code. No answer can use more than two lines. Assume the following variables have been declared and have legal values int a, b; string word, second; (a) Ask for, and read the value for second from the user. cout << "Enter second: "; cin >> second; (b) Print the cube of b.
cout << b * b * b << endl; (c) Print the number a without its last digit. cout << a / 10 << endl; (d) If word and second are equal print the value of a, otherwise print the value of b. if (word == second) cout << a << endl; else cout << b << endl; (e) Print a copies of the value of word on one line separated by single spaces. for (int x = 1; x <= a; x++) cout << word << " "; Problem 3 Consider the following C++ program. int p = 36, q = 49, r = 25; string a = "yes"; string x = "no"; if (a == x) cout << p; else cout << q; // line (a) if ((p <= q) && (r <= q)) cout << "ok" << endl; // line (b) if ((a == x) ("x" == "x")) r+=5; cout << r << endl; // line (c) if (!((p % 2) < (q % 2))) cout << "gg"; else cout << "ll"; // line (d) while (p > r) p--; cout << p << "\n"; // line (e) (a) What is the output at line (a)? 49 (b) What is the output at line (b)? ok (c) What is the output at line (c)?
30 (d) What is the output at line (d)? ll (e) What is the output at line (e)? 30 Problem 4 Write a complete C++ program that asks the user for a number n and prints n diagonal stripes (each with height n and width n) in a horizontal sequence. For example, if the user specified 4 for n, the program would print as follows: (Each stripe should begin in the column after the previous one ends. Do not try to check whether the user input is legal or sensible.) int n; cout << "Enter the number n: "; cin >> n; for (int r = 1; r <= n; r++) { for (int stripe = 1; stripe <= n; stripe++) { for (int c = 1; c <=n; c++) { if (r == c) cout << "*"; else cout << " ";