This is a C assignment that Im trying to create and would li

This is a C++ assignment that I\'m trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what\'s giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program will NOT use fstream, sstream, or string, and data will be taken from cin.

Learning Goals:
-Problem solving and debugging.
-Use of selection structures (making decisions).
-Use of repetition structures (looping).
-Formatting output to meet specifications.

The data files for this assignment will contain several data sets. Each data set will consist of several positive and/or negative integers terminated by the sentinel value, 0. Each value in the file will be separated by whitespace. Here is a sample data file with 2 data sets:
4   12   -32 0
715   45
47 -3 21
19 0

Design and implement a C++ program that will do the following.

Read the data from a file (as described above) using Linux redirection and generate a table that displays

a data set #, the number of values in the data set, the sum of the values in the data set, the average of the values in the data set, the largest value in the data set and (if the largest value is greater than 0) the number of factors it has

When all data has been read and the table is complete, display the total number of non-zero values read, their sum, and average with appropriate labels.

NOTES:

Assumptions about input: (you do not have to test for these conditions)

the data file will exist, will not be empty, and all input values will be integers

each data set will be terminated by a 0

a data set can be empty (no values preceding the 0)

each input value will be separated by whitespace (blanks and/or linefeeds)

the last line in the data file will be terminated with a linefeed (\'\ \')

Program must be designed to run in batch mode (no prompts) via Linux redirection

Display averages as floating point numbers with 3 digits to the right of the decimal.

If there are no values in a data set, leave the average column blank.

Display data set #, counts, and sums as integers.

Right justify all numbers displayed in table. The maximum number of columns needed to display a number (including negative sign and decimal) is 11. Make sure numbers do not run together in the table.

Display final count and sum as integers with labels.

Display final average with 3 digits to the right of the decimal and a label.

Sample terminal session:

[@bobby keys]$ more data4three
4   12   -32 0
715   45
47 -3 21
19 0
0
5 33 172 222 14
-15 123 0
[@bobby keys]$ g++ assign03.cpp
[@bobby keys]$ ./a.out < data4three

Output:

Data                     Largest #                                  
Set#       Count         Sum     Average   Largest #     Factors
================================================================
   1           3         -16      -5.333          12 6
   2           6         844     140.667         715 8
   3           0           0 0
   4           7         554      79.143         222 8
================================================================
Count of all non-zero #s: 16
Sum of all non-zero #s: 1382
Average of all non-zero #s: 86.375

Here is part of my code (I am still working on it but this is what I have thus far. Some if it isn\'t right, and needs some correction.)
There will be a minimum of 3 loops I need in order to solve this problem, I am just not sure how to go about doing this. I do not need anymore header files
and I can\'t use fstream since data will be taken from cin.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

int number;

int count = 0;

int sum = 0;

int set = 1;

int setcount = 0;

double average;

cin >> number;

cout << endl;

cout << left << setw(11) << \"Set#\" << left << setw(11) << \"Count\" << setw(11)

<< \"Sum\" << setw(11) << \"Average\" << setw(15) << \"Largest #\" << \"Factors\" << endl;

cout << fixed << setprecision(3);

{

while (number != 0)

{

count++;

sum = sum + number;

average = double(sum)/count;

cin >> number;

}

cout << count << endl;

cout << sum << endl;

cout << average << endl;

}

return 0;

}

Solution

The code part that you have written seems to be correct for single iteration i.e single buffer of data.

In order to check for all other buffer I am writing the while loop again for you.

You can take help from this :


largest=INT_MIN;
while (number != 0)
{
count++;
if(number>largest)
largest=number;
sum = sum + number;
average = double(sum)/count;
cin >> number;
}
cout << count << endl;
cout << sum << endl;
cout << average << endl;
cout << largest << endl;

globalSum+=sum; //for overalll
globalCount+=count;
globalAvg= ((globalAvg*globalCount)+ (sum*count))/(count+globalCount);
count=0;//for next iteration
sum=0;
average=0;
}

This is a C++ assignment that I\'m trying to create and would like some help understanding while loops, with possible integration of for loops and if statements
This is a C++ assignment that I\'m trying to create and would like some help understanding while loops, with possible integration of for loops and if statements
This is a C++ assignment that I\'m trying to create and would like some help understanding while loops, with possible integration of for loops and if statements

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site