Exam 1 Flashcards
(30 cards)
- Assume a string object has been defined as follows:
string description;
Write a cin statement that reads in one word to description.
cin >> description;
Assume a string object has been defined as follows:
string description;
Write a statement that reads into description multiple words separated by blanks.
getline(cin , description);
Assume the following variables are defined:
int age; double pay; char section;
Write a single cin statement that will read input into each of these variables.
cin >> age >> pay >> section;
What header files must be included in the following program?
int main() { double amount = 89.7; cout << fixed << showpoint << setprecision(1); cout << setw(8) << amount << endl; return 0; }
#include <iostream> // cin and cout #include <iomanip> // setw, showpoint, fixed and setprecision
Write a statement that defines a constant variable named RATE whose value is set to 0.12
const double RATE = 0.12;
What header file must be included to perform mathematical functions like sqrt?
#include <cmath>
What header file must be included to use cin and cout?
#include <iostream>
What header file must be included to use stream manipluators like setprecision?
#include <iomanip>
What header file must be included to use random numbers rand() and srand()?
#include <cstdlib>
What header file must be included to use time for random generator?
#include <ctime>
Find the Errors
Cout << "Enter a number: "; Cin << number1; Cout << "Enter another number: "; Cin << number2; number1 + number2 = sum; Cout "The sum of the two numbers is " << sum;
**c**out << "Enter a number: "; **c**in **>>** number1; **c**out << "Enter another number: "; **c**in >> number2; **sum =** number1 + number2; **c**out **<<** "The sum of the two numbers is " << sum ;
Find the errors:
const int number1, number2, product; cout << "Enter two numbers and I will multiply\n"; cout << "them for you.\n"; cin >> number1, number2; product = number1 * number2; cout << “product is” << “product”;
int number1, number2, product; // **remove const keyword ** cin >> number1** >> **number2; cout << “product is “ << **product**; //no double quotes around the variable
Find the Error
The following program contains many syntax errors. Locate as many as you can.
2. #include iostream 3. using namespace std; 4. 5. int main(); 6. } 7. int a, b, c \\ Define 3 integers 8. a = 3 9. b = 4 10. c = a + b 11. Cout >> "The value of c is " >> C; 12. return 0; 13. {
2. #include **<iostream>** 3. using namespace std; 4. 5. int main() //no semicolon after main() 6. **{** //open 7. int a, b, c**;** //Define 3 integers 8. a = 3**; ** 9. b = 4**; ** 10. c = a + b**;** 11. **c**out**<<** "The value of c is " **<<** **c**; 12. return 0; 13. }
What will the following program segment display?
int unus, duo, tres; unus = duo = tres = 5; unus += 4; duo *= 2; tres −= 4; unus /= 3; duo += tres; cout << unus << endl << duo << endl << tres << endl;
3
11
1
Convert the following pseudocode to C++ code. (Don’t need to write the whole program.)
Store 172.5 in the force variable.
Store 27.5 in the area variable.
Divide area by force and store the result in the pressure variable.
Display the contents of the pressure variable.
double force, area, pressure; force = 172.5; area = 27.5; pressure = area / force; cout << “ the pressure is “ << pressure;
What is the exact output when the following code is executed?
int price = 100; if (price = 50) cout << "The total price is 50." << endl; else cout << "The total price is not 50." << endl;
The total price is not 50.
- What is the exact output when the following code is executed?
int price = 100; if (price == 50) cout << "The total price is 50." << endl; else cout << "The total price is not 50." << endl;
The total price is not 50.
- What is the exact output when the following code is executed?
int price = 100; if (price == 100) cout << "The total price is 50." << endl; else cout << "The total price is not 50." << endl;
The total price is 50.
Rewrite the following program. Use a switch statement instead of the if/else if statement.
#include <iostream> using namespace std; int main() { int selection; cout << "Which formula do you want to see?\n\n"; cout << "1. Area of a circle\n"; cout << "2. Area of a rectangle\n"; cout << "3. Area of a cylinder\n"; cout << "4. None of them!\n"; cin >> selection; if (selection == 1) cout << "Pi times radius squared\n"; else if (selection == 2) cout << "Length times width\n"; else if (selection == 3) cout << "Pi times radius squared times height\n"; else if (selection == 4) cout << "Well okay then, good bye!\n"; else cout << "Not good with numbers, eh?\n"; return 0; }
#include <iostream> using namespace std; int main() { int selection; cout << "Which formula do you want to see?\n\n"; cout << "1. Area of a circle\n"; cout << "2. Area of a rectangle\n"; cout << "3. Area of a cylinder\n"; cout << "4. None of them!\n"; cin >> selection; switch (selection) { case 1: cout << "Pi times radius squared\n"; break; case 2: cout << "Length times width\n"; break; case 3: cout << "Pi times radius squared times height\n"; break; case 4: cout << "Well okay then, good bye!\n"; break; default: cout << "Not good with numbers, eh?\n"; } return 0; }
What will the following program display?
#include <iostream> using namespace std; int main() { const int UPPER = 8, LOWER = 2; int num1, num2, num3 = 12, num4 = 3; num1 = num3 > num4 ? UPPER : LOWER; num2 = num4 > UPPER ? num3 : LOWER; cout << num1 << " " << num2 << endl; return
8 2
What is the exact result?
int credits = 9; if (credits < 16) { if (credits > 11) cout << "You have enough courses."; } else cout << "Add another course."; cout << "Go register for courses.";
Go register for courses.
What is the exact result?
int credits =13; if (credits < 16) { if (credits > 11) cout << "You have enough courses."; } else { cout << "Add another course."; cout << "Go register for courses."; }
You have enough courses
Rewrite the following as an exactly equivalent do …while statement:
for ( int numComputers = 1; numComputers < 10; numComputers++) cout << “Computer #” << numComputers << endl;
1```
int numComputers = 0;
do {
cout «_space;“Computer #” «_space;numComputers «_space;endl;
numComputers++;
} while(numComputers < 10);
~~~
Given int variables k and total that have already been declared, use a do..while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 11 + 22 + 33 +… + 4949 + 50*50 into total. Use no variables other than k and total.
total=0; k=1; do { total+=k*k; k++; } while(k<=50);