Comp Sci 2
(1<x) && (x<3)
Consider a quadratic expression x^2-4x+3 Describing where the quadratic is negative involves describing a set of numbers that are simultaneously greater than the smaller root (+1) and less than the larger root (+3). Write a C++ Boolean expression that is true when the value of this quadratic is negative.
(x<-1)||(x>2)
Consider a quadratic expression, say x^2-x-2 Describing where the quadratic is positive (that is, greater than 0) involves describing a set of numbers that are either less than the smaller root (which is -1) or greater than the larger root (which is +2). Write a C++ Boolean expression that is true when this formula has positive values.
a. 3*x b. 3*x + y c. (x+y)/7 d. (3*x + y)/(z+2)
Convert each of the following mathematical formulas to a C++ expression: a. 3x b. 3x+y c. (x+y)/7 d. (3x+y)/(z+2)
cout <<"Enter a whole number and press return: "; cin >> the_number;
Give an input statement that will fill the variable the_number (of type int) with a number typed in at the keyboard. Precede the input statement with a prompt statement asking the user to enter a whole number.
cout << endl << "\t";
Give an output statement that produces the new-line character and a tab character.
cout << "The answer to the question of\n" << "Life, the Universe, and Everything is 42.\n";
Give an output statement that will produce the following message on the screen: The answer to the question of Life, the Universe, and everything is 42
a. 52 b. 9/5 has int value 1, since numerator and denominator are both int, integer division is done; the fractional part is discarded. f = (9.0/5)* c +32.0; or this f = 1.8*c +32.0;
Given the following fragments that purports to convert from degrees Celsius to degrees Fahrenheit, answer the following questions: double c = 20; double f; f = (9/5)*c + 32; a. What is the value assigned to f? b. Explain what is actually happening, and what the programmer likely wanted. c. Rewrite the code as the programmer intended.
if(savings >= expenses) { savings = savings - expenses; expenses = 0; cout << "Solvent"; } else { cout<<"Bankrupt"; }
Suppose savings and expenses are variables of type double that have been given values. Write an if-else statement that outputs the word Solvent, decreases the value of savings by the value of expenses, and sets the value of expenses to 0, provided that savings is at least as large as expenses. If, however, savings is less than expenses, the if-else statement simply outputs the word Bankrupt, and does not change the value of any variables.
With a do-while statement the loop body is always executed at least once. With a while statement there can be conditions under which the loop body is not executed at all.
What is the most important difference between a while statement and a do-while statement?
030406
What is the output of the following program lines (when embedded in a correct program that declares month, day, year, and date to be of type string)? month = "03"; day = "04"; year = "06"; date = month + day + year cout << date << endl
(1/3) *3 is equal to 0
What is the output of the following program lines (when embedded in a correct program that declares number to be of type int)? number = (1/3) * 3; cout << "(1/3)*3 is equal to " <<number;
bcbc
What is the output of the following program lines, when embedded in a correct program that declares all variables to be of type char? a = 'b'; b = 'c'; c = 'a'; cout << 'a'<<'b'<<c<<'c';
the output is exactly the same as it was for Self-Test Exercise 27
What is the output produced by the following ? x= 10 do { cout<< x<< endl; while (x>0) { cout<<x<<endl; x=x-3; }
10 7 4 1
What is the output produced by the following? x= 10 while (x>0) { cout<<x<<endl; x=x-3; }
No output because Boolean expression (x<0) is not satisfied and so while statements ends without executing the loop body
What output would be produced in the previous exercise if the > sign were replaced with <?
cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(3);
What statements should you include in your program to ensure that, when a number of type double is output, it will be output in ordinary notation with three digits after the decimal point?
#include <iostream> using namespace std; int main() { int n1, n2, sum; cout << "Enter two whole numbers\n"; cin >> n1 >> n2; sum = n1+ n2; cout << "The sum of " << n1 << "and" << n2 << " is " << sum << endl; return 0; }
Write a complete C++ program that read in two whole numbers and then outputs their sum. Be sure to prompt for input, echo input, and label all output.
Just remember to use % to get the remainder after dividing the two numbers.
Write a complete C++ program that reads two whole numbers into two variables of type int, and then outputs both the whole number part and the remainder when the first number is divided by the second. This can be done using the operators / and %.
#include <iostream> using namespace std; int main() { cout<<"Hello world\n"; return 0; }
Write a complete C++ program that writes the phrase Hello world to the screen. The program does nothing else.
if (score > 100) cout << "High"; else cout<<"Low";
Write an if-else statement that outputs the word High if the value of the variable score is greater than 100 and Low is the value of score is at most 100. The variable score is of type int.
if ((exam >=60)&&(programs_done>=10)) cout<< "Passed"; else cout<<"Failed";
Write an if-else statement that outputs the word Passed provided the value of the variable exam is greater than or equal to 60 and the value of the variable programs_done is greater than or equal to 10. Otherwise, the if-else statement outputs the word Failed. The variables exam and programs_done are both of type int.
if((temperature >=100) || (pressure) >=200)) cout<<"Warning"; else cout"OK";
Write an if-else statement that outputs the word Warning provided that either the value of the variable temperature is greater than or equal to 100, or the value of the variable pressure is greater than or equal to 200, or both. Otherwise, the if-else statement outputs the word OK. The variables temperature and pressure are both of type int.
a. 0 is false b. 1 is true c. -1 is true
a. if(0) cout<<"0 is true"; else cout<<"0 is false"; cout<<endl; b. if(1) cout<<"1 is true"; else cout<<"1 is false"; cout<<endl; c. if(-1) cout<<"-1 is true"; else cout<<"-1 is false"; cout<<endl;