Question

This is a C++ assignment that I'm trying to create and would like some help understanding...

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 ('\n')

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

#include

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int factorCount(int n)
{
   int count=0;
   for(int i = 1; i <= n; ++i)
   if(n % i == 0)
           count++;
   return count;
}

int main(int argc,char *argv[])
{
   int number;
   int count = 0;
   int sum = 0;
   int set = 1;
   int max = 0;
   int fact = 0;
   int totalSum = 0;
   double gAverage=0.0;
   int setcount = 0;
   double average=0.0;

   cout << left << setw(11) << "Set#" << left << setw(11) << "Count" << setw(11)<< "Sum" << setw(11) << "Average" << setw(15) << "Largest #" << "Factors" << endl;
   cout << fixed << setprecision(3);

   fstream myfile(argv[0], ios_base::in);
   while(myfile >> number)
   {
       if (number != 0)
       {
           setcount++;
           sum = sum + number;
           if(number>max)
               max = number;
       }
       else if(number == 0)
       {
       if(sum!=0)
           average = double(sum)/setcount;
           fact = factorCount(max);
           cout << left << setw(11) << set << left << setw(11) << setcount << setw(11)<< sum << setw(11) << average << setw(15) << max << fact << endl;
           count = count + setcount;
           totalSum = sum + totalSum;
            set++;
           setcount = 0;
           max = 0;
           sum = 0;
           average = 0.0;

       }

    }
   cout<<endl;
   cout<<"Count of all non-zero #s: "<<count<<endl;
   cout<<"Sum of all non-zero #s: "<<totalSum<<endl;
   if(totalSum!=0)
   gAverage = double(totalSum)/count;
   cout<<"Average of all non-zero #s: "<<gAverage<<endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
This is a C++ assignment that I'm trying to create and would like some help understanding...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    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...

  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • Topics c ++ Loops While Statement Description Write a program that will display a desired message...

    Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement.    Testing For submitting, use the data in the...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • This is my code for a final GPA calculator. For this assignment, I'm not supposed to...

    This is my code for a final GPA calculator. For this assignment, I'm not supposed to use global variables. My question is: does my code contain a global variable/ global function, and if so how can I re-write it to not contain one? /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * Title: Final GPA Calculator * Course Computational Problem Solving CPET-121 * Developer: Elliot Tindall * Date: Feb 3, 2020 * Description: This code takes grades that are input by the student and displays * their...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • I'm not getting out put what should I do I have 3 text files which is...

    I'm not getting out put what should I do I have 3 text files which is in same folder with main program. I have attached program with it too. please help me with this. ------------------------------------------------------------------------------------ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT