c++ ch. 2-5
Assume that c is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if c is NOT what is called a whitespace character (that is a space or a tab or a newline-- none of which result in ink being printed on paper).
!(c == ' ' || c== '\n' || c == '\t')
Assume that x is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if x is NOT a upper-case letter.
!(x >= 'A' && x <='Z')
Define a macro RIGHT to represent the true value
#define RIGHT-true
Define a macro WRONG to represent the false value
#define WRONG-false
Write the necessary preprocessor directive to enable the use of the time function
#include <ctime>
Write the necessary preprocessor directive to enable the use of the stream manipulators like setw and setprecision
#include <iomanip>
Assume that credits is an int variable whose value is 0 or positive. Write an expression whose value is "freshman" or "sophomore" or "junior" or "senior" based on the value of credits. In particular: if the value of credits is less than 30 the expression's value is "freshman"; 30-59 would be a "sophomore", 60-89 would be "junior" and 90 or more would be a "senior".
((credits < 30)?"freshman": ((credits < 60 && credits > 29)?"sophomore": ((credits < 90 && credits > 59)?"junior": ((credits > 89)?"senior": "invalid"))))
Assume that day is an int variable whose value is 1 or 2 or ... or 7. Write an expression whose value is "sun" or "mon" or ... or "sat" based on the value of day. (So, if the value of day were 4 then the value of the expression would be "wed".).
((day==1)?"sun": ((day==2)?"mon": ((day==3)?"tue": ((day==4)?"wed": ((day==5)?"thu": ((day==6)?"fri": ((day==7)?"sat": "invalid")))))))
Assume that month is an int variable whose value is 1 or 2 or 3 or 5 ... or 11 or 12. Write an expression whose value is "jan" or "feb or "mar" or "apr" or "may" or "jun" or "jul" or "aug" or "sep" or "oct" or "nov" or "dec" based on the value of month. (So, if the value of month were 4 then the value of the expression would be "apr".).
((month==1)?"jan": ((month==2)?"feb": ((month==3)?"mar": ((month==4)?"apr": ((month==5)?"may": ((month==6)?"jun": ((month==7)?"jul": ((month==8)?"aug": ((month==9)?"sep": ((month==10)?"oct": ((month==11)?"nov": ((month==12)?"dec": "invalid"))))))))))))
Assume that x is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if x is a decimal digit (0-9).
(x>='0' && x<='9')
Assume that x is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if x is a upper-case letter.
(x>='A' && x<='Z')
Write a hexadecimal integer literal representing the value fifteen
0xf
Write a literal representing the long integer value twelve million
12000000L
Write the declaration of a char array named smallWord suitable for storing 4-letter words such as "love", "hope" and "care"
char smallWord[5]
Assume that an int variable age has been declared and already given a value and assume that a char variable choice has been declared as well. Assume further that the user has just been presented with the following menu: S: hangar steak, red potatoes, asparagus T: whole trout, long rice, brussel sprouts B: cheddar cheeseburger, steak fries, cole slaw (Yes, this menu really IS a menu!) Write some code that reads a single character (S or T or B) into choice. Then the code prints out a recommended accompanying drink as follows: If the value of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, and "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value of age, your code should print "invalid menu selection" if the character read into choice was not S or T or B.
cin >> choice; if(age < 22){ switch(choice){ case 'S': cout << "vegetable juice"; break; case 'T': cout << "cranberry juice"; break; case 'B': cout << "soda"; break; default: cout << "invalid menu selection"; } }else{ switch(choice){ case 'S': cout << "cabernet"; break; case 'T': cout << "chardonnay"; break; case 'B': cout << "IPA"; break; default: cout << "invalid menu selection"; } }
Write a statement that reads a word from standard input into firstWord. Assume that firstWord. has already been declared as an char array large enough to hold a 50-letter word
cin >> firstWord
Given a int variable named yesCount and another int variable named noCount and a char variable named response, write the necessary code to read a value into response and then carry out the following: if the character typed in is a y or a Y then increment yesCount and print out "YES WAS RECORDED" if the character typed in is an n or an N then increment noCount and print out "NO WAS RECORDED" If the input is invalid just print the message "INVALID" and do nothing else.
cin >> response; switch (response) { case 'Y': case 'y': cout << "YES WAS RECORDED"; yesCount++; break; case 'N': case 'n': cout << "NO WAS RECORDED"; noCount++; break; default: cout << "INVALID"; break; }
Write a statement that reads a floating point (real) value from standard input into temperature. Assume that temperature. has already been declared as an double variable
cin >> temperarture
Write an expression that attempts to read a double value from standard input and store it in an double variable, x, that has already been declared
cin >> x
Suppose that the code below is the body of some loop. Use a continue statement to make sure that the nothing is written to cout when y is 0.
cin >> x >> y; if (y == 0) continue; cout << x / y;
Write a statement that reads 5 successive integers into these variables that have already been declared: x1 x2x3x4 x5. Then write a statement that prints each out on its own line so that they form a right-justified column with a 5-digit width. If any of the integers are 5-digits in size, then they will start at the very beginning of their lines. For example: |54213 | 8713 | 23 | 147 | 15
cin >> x1 >> x2 >> x3 >> x4 >> x5; cout.setf (ios::right, ios::adjustfield); cout << setw(5) << x1 << "\n" << setw(5) << x2 << "\n" << setw(5) << x3 << "\n" << setw(5) << x4 << "\n" << setw(5) << x5 << "\n";
A variable c of type char has been declared. Write the necessary code to read in the next character from standard input and store it in c, regardless of whether is a whitespace character
cin.get(c);
Assume that a,b and c are char variables have been declared. Write some code that reads in the first character of the next line into a, the first character of the line after that into b and the first character of the line after that into c. Assume that the lines of input are under 100 characters long
cin.ignore(100, '\n') >> a; cin.ignore(100, '\n') >> b; cin.ignore(100, '\n') >> c;
Assume that c is a char variable has been declared. Write some code that reads in the first character of the next line into c. Assume that the lines of input are under 100 characters long
cin.ignore(100, '\n') >> c;
Assume that x is a double variable that has been given a value. Write a statement that prints it out with exactly three digits to the right of the decimal point no matter what how big or miniscule its value is
cout << fixed << setprecision(3) << x;
Two variables, num and cost have been declared and given values: num is an integer and cost is a double. Write a single statement that outputs num and cost to standard output. Print both values (num first, then cost), separated by a space on a single line that is terminated with a newline character. Do not output any thing else.
cout << num << " " << cost<< endl
Assume that m is an int variable that has been given a value. Write a statement that prints it out in a print field of 10 positions
cout << setw(10) << m;
Assume that x is a double variable that has been initialized. Write a statement that prints it out, guaranteed to have a decimal point, but without forcing scientific (also known as exponential or e-notation)
cout << showpoint << x;
Write a statement that declares a double variable named dosage
double dosage
Declare a numerical variable precise and initialize it to the value 1.09388641
double precise = 1.09388641
Given an int variable count that has already been declared, write a for loop that prints the integers 50 through 1, separated by spaces. Use no variables other than count
for (count=50; count>0; count--) { cout << count << " "; }
Given an int variable i that has already been declared, write a for loop that prints the integers 0 through 39, separated by spaces. Use no variables other than i.
for (i=0; i<=39; i++) { cout << i << '\n'; }
Assume the int variables i, lo, hi, and result have been declared and that lo and hi have been initialized. Write a for loop that adds the integers between lo and hi (inclusive), and stores the result in result. Your code should not change the values of lo and hi. Also, do not declare any additional variables -- use only i, lo, hi, and result.
for (i=lo; i<=hi; i++) { result += i; }
Write a for loop that prints the odd integers 11 through 121 inclusive, separated by spaces.
for (int i=11; i<=121; i++) { if ((i%2)!=0){ cout << i << " "; } }
Given an int variable k that has already been declared, use a for loop to print a single line consisting of 97 asterisks. Use no variables other than k.
for (k=1;k<=97;k++) { cout << "*"; }
Write a for loop that computes the following sum: 5+10+15+20+...+485+490+495+500. The sum should be placed in a variable sum that has already been declared and initialized to 0. In addition, there is another variable, num that has also been declared. You must not use any other variables.
for(int num=1; num<=500; num++) { if ((num % 5)==0){ sum+=num; } }
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Assume that n is an integer variable whose value is some positive integer N. Assume also that hn is a double variable whose value is the Nth harmonic number. Write an expression whose value is the (N+1)th harmonic number
hn + 1.0/(n+1)
In mathematics, the Nth harmonic number is defined to be 1 + 1/2 + 1/3 + 1/4 + ... + 1/N. So, the first harmonic number is 1, the second is 1.5, the third is 1.83333... and so on. Assume that n is an integer variable whose value is some integer N greater than 1. Assume also that hn is a double variable whose value is the Nth harmonic number. Write an expression whose value is the (N-1)th harmonic number..
hn-1.0/(n)
Write a conditional that assigns true to the variable fever if the variable temperature is greater than 98.6
if ( temperature > 98.6 ) fever = true ;
Write an if/else statement that compares the double variable pH with 7.0 and makes the following assignments to the bool variables neutral, base, and acid: false, false, true if pH is less than 7 false, true, false if pH is greater than 7 true, false, false if pH is equal to 7
if (pH < 7){ neutral = 0; base = 0; acid = 1; }else if (pH > 7){ neutral = 0; base = 1; acid = 0; }else { neutral = 1; base = 0; acid = 0; }
Write a conditional that multiplies the values of the variable pay by one-and-a-half if the value of the boolean variable workedOvertime is true
if (workedOvertime == true) pay = pay * 1.5;
Write a statement that terminates the current loop when the value of the int variables x. and y.are equal
if(x == y){ break; }
Write a loop that reads positive integers from standard input, printing out those values that are even, separating them with spaces, and that terminates when it reads an integer that is not positive. Declare any variables that are needed.
int num=1; while (num>0){ cin >> num; if ((num % 2) == 0 && num > 0 ) { cout << num << " "; } }
Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a do...while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.
j=1; do{ cout << "*"; j++; } while (j<=n);
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 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
k = 0; total = 0; do { total = total + (k*k); k++; } while (k <=50);
Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 97 asterisks. Use no variables other than k.
k=1; do { cout << "*"; k++; } while (k<=97);
Write a statement using a compound assignment operator to subtract 10 from minutes_left (an integer variable that has already been declared and initialized)
minutes_left-=10;
Write a statement using a compound assignment operator to multiply num_rabbits by 4, changing the value of num_rabbits (num_rabbits has already been declared and initialized)
num_rabbits *= 4
Write a statement using a compound assignment operator to cut the value of pay in half (pay is an integer variable that has already been declared and initialized)
pay /= 2
Write a loop that reads strings from standard input where the string is either "duck" or "goose". The loop terminates when "goose" is read in. After the loop, your code should print out the number of "duck" strings that were read.
string duck; string goose; string x; int count = 0; do { cin >> x; if (x < "goose") { count++; } } while (x < "goose"); cout << count;
In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early childhood 6-7, young reader 8-10, elementary 11 and 12, middle 13, impossible 14-16, high school 17-18, scholar greater than 18, ineligible Given an int variable age, write a switch statement that prints out the appropriate label from the above list based on age.
switch (age) { case 0: case 1: cout << "ineligible"; break; case 2: cout << "toddler"; break; case 3: case 4: case 5: cout << "early childhood"; break; case 6: case 7: cout << "young reader"; break; case 8: case 9: case 10: cout << "elementary"; break; case 11: case 12: cout << "middle"; break; case 13: cout << "impossible"; break; case 14: case 15: case 16: cout << "high school"; break; case 17: case 18: cout << "scholar"; break; default: cout << "ineligible"; break; }
Assume that grade is a variable whose value is a letter grade-- any one of the following letters: 'A', 'B', 'C', 'D', 'E', 'F', 'W', 'I'. Assume further that there are the following int variables, declared and already initialized: acount, bcount, ccount, dcount, ecount, fcount, wcount, icount. Write a switch statement that increments the appropriate variable (acount, bcount, ccount, etc.) depending on the value of grade. So if grade is 'A' then acount is incremented; if grade is'B' then bcount is incremented, and so on.
switch (grade) { case 'A': acount++; break; case 'B': bcount++; break; case 'C': ccount++; break; case 'D': dcount++; break; case 'E': ecount++; break; case 'F': fcount++; break; case 'W': wcount++; break; case 'I': icount++; break; }
HTTP is the protocol that governs communications between web servers and web clients (i.e. browsers). Part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. Some of the codes and their meanings are listed below: 200, OK (fulfilled) 403, forbidden 404, not found 500, server error Given an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.
switch( status ){ case 200: cout << "OK (fulfilled)"; break; case 403: cout << "forbidden"; break; case 404: cout << "not found"; break; case 500: cout << "server error"; break; }
Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y, the message Your request is being processed is printed if response is n, the message Thank you anyway for your consideration is printed if response is h, the message Sorry, no help is currently available is printed for any other value of response, the message Invalid entry; please try again is printed
switch(response){ case 'y': cout << "Your request is being processed"; break; case 'n': cout << "Thank you anyway for your consideration"; break; case 'h': cout << "Sorry, no help is currently available"; break; default: cout << "Invalid entry; please try again"; break; }
Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". There is a CONSECUTIVE REPETITION of length 3 (the three consecutive birds) and a CONSECUTIVE REPETITION of length 2 (the two reptiles). There are also several SINGLETONs of length 1 (a singleton fish, a singleton bird, a singleton mammal, and another singleton fish). Write some code that uses a loop to read in a sequence of words, terminated by the "xxxxx". The code assigns to t the number of CONSECUTIVE REPETITIONS that were read. (For example, in the above data sequence that value would be 2.) Assume that t has already been declared but not initialized. Assume that there will be at least one word before the terminating "xxxxx".
t = 0; string prev = "xxxxx", current = "", next, last; cin >> current; while (current != "xxxxx") { cin >> next; if ((prev == current || current == next) && current != last) { t++; last = current; } prev = current; current = next; } cout << t;
Given int variables k and total that have already been declared, use a for 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 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total=0; for (int k=1; k<=50; k++) { total += k*k; }
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.
total=0; for (int k=1; k<=n; k++) { total += k*k*k; }
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.
total=0; k=0; do { total+=k*k*k; k++; } while (k<=n);
Qualifying an integer declaration with "unsigned" essentially increases the maximum value of the variable by a factor of
two
Declare an unsigned integer variable named degreesKelvin
unsigned int degreesKelvin
Write a statement using a compound assignment operator to change val to the remainder when val is divided by 16 (val has already been declared and initialized)
val %= 16
Write a statement using a compound assignment operator to add 5 to val (an integer variable that has already been declared and initialized)
val +=5
Write an expression using the conditional operator (? :) that compares the values of the variables x and y and results in the larger of the two.
x > y ? x : y
Write an expression using the conditional operator (? :) that compares the value of the variable x to 5 and results in: x if x is greater than or equal to 5 -x if x is less than 5
x>=5 ? x : -x