Docsity

C++ Programming Exercises with Solutions: Loops and Conditionals, Exams of Advanced Education

Toggle sidebar
bg1
COSC 1436 C++ TEST WITH
COMPLETE SOLUTION
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)
2. {
3. cout<<"*";
4. k++;}
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
while loop to print a single line consisting of n asterisks. Thus if n contains 5,
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download C++ Programming Exercises with Solutions: Loops and Conditionals and more Exams Advanced Education in PDF only on Docsity!

COSC 14 36 C++ TEST WITH

COMPLETE SOLUTION

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)

  1. {
  2. cout<<"*";
  3. k++;} 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 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. - ANSWER

  1. j=n;
  2. while(j>0){
  3. cout<<"*";
  4. j--;
  5. } Given a string variable s that has already been declared, write some code that repeatedly reads a value from standard input into s until at last a "Y" or "y"or "N" or "n" has been entered. - ANSWER 1. while ((s!="Y"&&s!="y"&&s!="N"&&s!="n"))
  6. {
  7. cin>>s;
  8. } Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered. - ANSWER 1. while(n>10||n<1){
  9. cin>>n;
  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. - ANSWER 1. total=0;

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

  1. for(j=0;j<n;j++){
  2. cout<<"*";
  3. }

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++){

  1. if((i%5)==0){
  2. cout<<i<<" ";
  3. }
  4. }

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--){

  1. cout<<count<<" ";
  2. }

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++)

  1. {
  2. if((num%5)==0)
  3. {
  4. sum+=num;
  5. }
  6. }

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

  1. total=0;f
  2. for(k=1;k<=50;k++)
  3. total+=k*k;

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

  1. total=0;
  1. k=1;
  2. while(k<=50)
  3. {
  4. total+=k*k;
  5. k++;
  6. }

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;

  1. k=0;
  2. do
  3. {
  4. total+=kkk;
  5. k++;
  6. }while(k<=n);

Write a loop that reads positive integers from standard input, printing out

  1. sumeven+=num;
  2. }
  3. if((num%2)!=0&&(num>0))
  4. {
  5. sumodd+=num;
  6. }
  7. }
  8. cout<<sumeven<<" "<<sumodd;

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;

  1. string goose;
  2. string x;
  3. int count=0;
  4. do
  5. {
  6. cin>>x;
  7. if(x<"goose")
  1. count++;
  2. }
  3. }
  4. while (x<"goose");
  5. cout<<count;

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;

  1. int a,b;
  2. int wage=0;
  3. int hours=0;
  4. total=0;
  5. cin>>employees;
  6. for(a=1;a<=employees;a++)
  7. {
  8. cin>>wage;
  9. for(b=1;b<=5;b++)
  1. cin>>hours;
  2. total+=hours*wage;
  3. }
  4. }

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

  1. filename.open("topsecret");
  2. if(filename.fail())
  3. {
  4. isReadable=false;
  5. }
  6. else
  7. {
  8. isReadable=true;
  9. }

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;

  1. filename.open("table20");
  2. if(filename.fail())
  3. {
  4. cin>>x;
  5. }
  6. else
  7. {
  1. filename>>x;
  2. }

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){

  1. break;
  2. }

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;

  1. if(y==0)continue;
  2. cout << 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

  1. else
  2. {
  3. while(randomFile>>aNumber)
  4. {
  5. numbers++;
  6. sum+=aNumber;
  7. }
  8. if(numbers>0)
  9. average = sum/numbers;
  10. else
  11. average = 0.0;
  12. cout<<"Number of numbers: "<<numbers<<"\n";
  13. cout<<"Sum of the numbers: "<<sum<<"\n";
  14. cout<<"Average of the numbers: "<<average<<"\n";
  15. }
  16. randomFile.close();
  17. return 0;
  18. }