COSC 1436 C++

¡Supera tus tareas y exámenes ahora con Quizwiz!

Write the necessary preprocessor directive to enable the use of file streams.

#include<fstream>

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.

1. cin >> x >> y; 2. if(y==0)continue; 3. cout << x / y;

Write a loop that displays all possible combinations of two letters where the letters are 'a', or 'b', or 'c', or 'd', or 'e'. The combinations should be displayed in ascending alphabetical order: aa ab ac ad ae ba bb ... ee

1. for(char outerChar='a';outerChar<='e'; 2. outerChar++) 3. { 4. for(char innerChar='a';innerChar<='e'; 5. innerChar++) 6. { 7. cout<<outerChar<<innerChar<<"\n"; 8. } 9. }

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.

1. for(count=50;count>0;count--){ 2. cout<<count<<" "; 3. }

Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by spaces.

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

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 B) The sum of all the numbers in the file (a running total) C) The average of all the numbers in the fileThe program should display the number of numbers found in the file, the sum of the numbers, and the average of the numbers. Prompts And Output Labels. Print each of the above quantities on a line by itself, preceding by the following (respective) labels: "Number of numbers: ", "Sum the numbers: ", and "Average of the numbers: ".

1. #include<iostream> 2. #include<fstream> 3. using namespace std; 4. int main() 5. { 6. int aNumber = 0; 7. int numbers = 0; 8. double sum = 0.0; 9. double average = 0.0; 10. ifstream randomFile; 11. randomFile.open("Random.txt"); 12. 13. if(randomFile.fail()) 14. cout<<"failed to read file."; 15. else 16. { 17. while(randomFile>>aNumber) 18. { 19. numbers++; 20. sum+=aNumber; 21. } 22. 23. if(numbers>0) 24. average = sum/numbers; 25. else 26. average = 0.0; 27. cout<<"Number of numbers: "<<numbers<<"\n"; 28. cout<<"Sum of the numbers: "<<sum<<"\n"; 29. cout<<"Average of the numbers: "<<average<<"\n"; 30. } 31. randomFile.close(); 32. return 0; 33. }

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.

1. for(int num=1;num<=500;num++) 2. { 3. if((num%5)==0) 4. { 5. sum+=num; 6. } 7. }

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.

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

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.

1. for(k=1;k<=97;k++){ 2. cout<<"*"; 3. }

Write a statement that terminates the current loop when the value of the int variables x. and y.are equal

1. if(x==y){ 2. break; 3. }

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.

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

Given a bool variable isReadable write some statements that assign true to isReadable if the file "topsecret" exists and can be read by the program and assigns false to isReadable otherwise.

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

Given the availability of an ifstream object named input, write the other statements necessary to read an integer into a variable datum that has already been declared from a file called rawdata. Assume that reading that one integer is the only operation you will carry out with this file. (Note: write just the statements, do not define a main function.)

1. input.open("rawdata"); 2. input>>datum; 3. input.close();

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.

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

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.

1. int num=1; 2. while(num>0){ 3. cin>>num; 4. if((num%2)==0&&num>0){ 5. cout<<num<<" "; 6. } 7. }

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.

1. int sumodd=0; 2. int sumeven=0; 3. int num=1; 4. while(num>0) 5. { 6. cin>>num; 7. if((num%2)==0&&(num>0)) 8. { 9. sumeven+=num; 10. } 11. if((num%2)!=0&&(num>0)) 12. { 13. sumodd+=num; 14. } 15. } 16. cout<<sumeven<<" "<<sumodd;

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.

1. j=1; 2. do{ 3. cout<<"*"; 4. j++; 5. } 6. while(j<=n);

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.

1. j=n; 2. while(j>0){ 3. cout<<"*"; 4. j--; 5. }

Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata (which you must declare) to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do. Declare any variables that you need.

1. ofstream outdata;string name; int age; 2. cin >> name >> age; outdata.open("outdata"); 3. outdata << name << " " << age; outdata.close();

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.

1. string duck; 2. string goose; 3. string x; 4. int count=0; 5. do 6. { 7. cin>>x; 8. if(x<"goose") 9. { 10. count++; 11. } 12. } 13. while (x<"goose"); 14. cout<<count;

Given that two int variables, total and amount, have been declared, write a loop that reads integers into amount and adds all the non-negative values into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0.

1. total=0; 2. do 3. { 4. cin>>amount; 5. if(amount>0) 6. total+=amount; 7. } 8. while(amount>=0);

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, total is incremented by the value in amount).

1. total=0; 2. for(int i=1;i<=3;i++) 3. { 4. cin>>amount; 5. total+=amount; 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 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.

1. total=0; 2. k=0; 3. do 4. { 5. total+=k*k*k; 6. k++; 7. }while(k<=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.

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

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 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

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

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.

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

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.

1. while ((s!="Y"&&s!="y"&&s!="N"&&s!="n")) 2. { 3. cin>>s; 4. }

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.

1. while (k<97) 2. { 3. cout<<"*"; 4. k++;}

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.

1. while(n>10||n<1){ 2. cin>>n; 3. }

Consider this code: "int v = 20; --v; cout << v++;". What value is printed, what value is v left with?

19 is printed, v ends up with 20

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?

do while loop

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 complete your use of this object.

corpdata.close();

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?

for loop

You need to write a loop that will repeat exactly 125 times. Which is the preferred loop construct to use?

for loop

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.

for(i=11,result=1;i<20;i+=2)

Given an ifstream object named input1, associate it with a file named winterdata.txt by opening the file for reading.

input1.open("winterdata.txt");

Consider this code: "int s = 20; int t = s++ + --s;". What are the values of s and t?

s is 20 and t cannot be determined

Define an object named outfile that can be used to write data from program variables to a file.

ofstream outfile;

Given an ofstream object named output, associate it with a file named yearsummary.txt by opening the file for writing.

output.open("yearsummary.txt");

Given an integer variable strawsOnCamel, write a statement that uses the auto-increment operator to increase the value of that variable by 1.

strawsOnCamel++;

Given an integer variable timer, write a statement that uses the auto-decrement operator to decrease the value of that variable by 1.

timer--;

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.

total+=amount;

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?

while loop


Conjuntos de estudio relacionados

algebra 1a - unit 4: polynomials and factoring quadratic expressions

View Set

How Is Genetic Information in DNA Used to Express Proteins?

View Set

CSC 591 Quantum Computing Midterm

View Set

Chapter 8 Abnormal Psychology Treatments for Depressive and Bipolar disorders

View Set

sociology chapter 9 constructing gender and sexuality

View Set

James Chapter 29: Psychosocial Problems in Children and Families

View Set