











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Given an integer variable strawsOnCamel, write a statement that uses the auto-increment operator to increase the value of that variable by 1. - ANSWER strawsOnCamel++; Given an integer variable timer, write a statement that uses the auto-decrement operator to decrease the value of that variable by 1. - ANSWER timer--; Consider this code: "int v = 20; --v; cout << v++;". What value is printed, what value is v left with? - ANSWER 19 is printed, v ends up with 20 Consider this code: "int s = 20; int t = s++ + --s;". What are the values of s and t? - ANSWER s is 20 and t cannot be determined Given an int variable k that has already been declared, use a while loop to print a single line consisting of 97 asterisks. Use no variables other than k. - ANSWER 1. while (k<97)
five asterisks will be printed. Use no variables other than n and j. - ANSWER
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 for 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. - ANSWER
Assume the int variables i and result, have been declared but not initialized. Write a for loop header -- i.e. something of the form for (... ) for the following loop body: result = result * i; When the loop terminates, result should hold the product of the odd numbers between 10 and 20. NOTE: just write the for loop header; do not write the loop body itself. - ANSWER for(i=11,result=1;i<20;i+=2)
Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by spaces. - ANSWER 1. for(int
i=1;i<175;i++){
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. - ANSWER 1. for(count=50;count>0;count--){
Write a statement that increments the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total. - ANSWER total+=amount;
Given that two int variables, total and amount, have been declared, write a sequence of statements that: initializes total to 0 reads three values into amount, one at a time. After each value is read in to amount, it is added to the value in total (that is,
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. - ANSWER 1. for(int num=1;num<=500;num++)
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 11 + 22 + 33 +... + 4949 + 50*50 into total. Use no variables other than k and total. - ANSWER
Given int variables k and total that have already been declared, use a 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. - ANSWER
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 111 + 222 + 333 + 444 into total. Use no variables other than n, k, and total. - ANSWER 1. total=0;
Write a loop that reads positive integers from standard input, printing out
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. - ANSWER 1. string duck;
You need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use? - ANSWER for loop
You need to write a loop that will keep reading and adding integers to a sum, until the sum reaches or exceeds 21. The numbers are all less than 20 and the sum is initially 0. Which is the preferred loop construct to use? - ANSWER do while loop
You need to write a loop that reads integers and adds them to a sum as long as they are positive. Once 0 or a negative value is read in your loop terminates. Which is the preferred loop construct to use? - ANSWER while loop
You have a variable, n, with a non-negative value, and need to write a loop that will keep print n blank lines. What loop construct should you use? -
Assume the input data is structured as follows: first there is a non-negative integer specifying the number of employee timesheets to be read in. This is followed by data for each of the employees. The first number for each employee is an integer that specifies their pay per hour in cents. Following this are 5 integers, the number of hours they worked on each of the days of the workweek. Given this data, and given that an int variable total has been declared, write a loop and any necessary code that reads the data and stores the total payroll of all employees in total. Note that you will have to add up the numbers worked by each employee and multiply that by that particular employee's pay rate to get the employee's pay for the week-- and sum those values into total. - ANSWER 1. int employees;
Write the necessary preprocessor directive to enable the use of file streams. - ANSWER #include
Define an object named outfile that can be used to write data from program variables to a file. - ANSWER ofstream outfile;
Given an ifstream object named input1, associate it with a file named winterdata.txt by opening the file for reading. - ANSWER input1.open("winterdata.txt");
Given an ofstream object named output, associate it with a file named yearsummary.txt by opening the file for writing. - ANSWER output.open("yearsummary.txt");
Given that corpdata is an ifstream object that has been used for reading data and that there is no more data to be read, write the necessary code to
Given an int variable x write some statements that attempt to open a file named "table20" and read a value into x; if that turns out not to be possible your code should then read avalue from standard input into x. - ANSWER 1. ifstream filename;
Write a statement that terminates the current loop when the value of the int variables x. and y.are equal - ANSWER 1. if(x==y){
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. - ANSWER 1. cin >> x >> y;
5.24: Using Files—Numeric ProcessingIf you have downloaded this book's source code from the companion Web site, you will find a file named Random.txt in the Chapter 05 folder. (The companion Web site is at www.pearsonhighered.com/gaddis.) This file contains a long list of random numbers. Copy the file to your hard drive and then write a program that opens the file, reads all the numbers from the file, and calculates the following: A) The number of numbers in the file