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
Note : Inorder to exit from program smoothly ,please give me three 0\'s sequentially at end of you input.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int set = 1;
int number;
int countall=0;
int sumall=0;
int averageall=0;
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);
int zcount=0;
while (number != 0)
{
int count = 0;
int sum = 0;
int fcount=0;
int setcount = 0;
double average;
int max=number;
while (number != 0)
{
if(number>max)
{
max=number;
}
count++;
sum = sum + number;
average = double(sum)/count;
cin >> number;
}
for(int i = 1; i <= max; ++i)
{
if(max % i == 0)
fcount++;
}
cout << left << setw(11) << set << left << setw(11) << count << setw(11)<< sum << setw(11) << average << setw(15) << max << fcount << endl;
set=set+1;;
countall=countall+count;
sumall = sumall + sum;
cin >> number;
if (number == 0)
{
if(number>max)
{
max=number;
}
cout << left << setw(11) << set << left << setw(11) << 0 << setw(11)<< 0 << setw(11) << 0 << setw(15) << 0 << 0 << endl;
set=set+1;;
cin >> number;
}
}
averageall = double(sumall)/countall;
cout << \"Count of all non-zero #s: \"<<countall <<endl;
cout << \"Sum of all non-zero #s: \"<< sumall <<endl;
cout << \"Average of all non-zero #s:\"<< averageall <<endl;
return 0;
}
Input : 4 12 -32 0 715 45 47 -3 21 19 0 0 5 33 172 222 14 -15 123 0 0 0
output :
et# Count Sum Average Largest # Factors
1 3 -16 -5.333 12 6
2 6 844 140.667 715 8
3 0 0 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




