Coded in C Visual Studios 2013 Please help Read in float val

Coded in C++; Visual Studios 2013; Please help.

Read in float values from tin until I press -z. This is the some as the eof in a file. As you read in each number, you will need to track the minimum, the maximum, the count, the sum and the average. As each number is read in and processed, you will need output the statistics you are tracking to the screen neatly for malted with d decimal precision of three. YOU MAY NOT SAVE THE NUMBERS INTO AN ARRAY, use the eof function, break statements or continue statements, please see the document on reading until EOF. A sample run may look like this: My input is in red Enter -z to terminate input Count = 1 Minim = -34.000 Maxima = -34.000 Sum = -34.000 Average = -34.000 Count = 2 Minim = -34.000 Maxima = 45.600 Sum = 11.600 Average = 5.800 Count = 3 Minimum = -34.000 Maximum = 93.600 Sum = 110.200 Average = 36.733 Count = 4 Minimum = -34.000 Maximum = 93.600 Sum = 111.000 Average = 27.750 Count = 5 Minimum = -34.000 Maximum = 93.600 Sum = 111.230 Average = 22.246 Count = 6 Minimum = -34.000 Maximum = 93.600 Sum = 204.794 Average = 34.132 Count = 7 Minimum = -34.000 Maximum = 100.943 Sum = 305.742 Average = 43.677 Press any key to continue... Make sure and test your program thoroughly Try all negatives Try all positives Try no numbers (hit Ctrl 2 right away)

Solution

Logic: We just need to compute 4 values -- min, max, sum and average. Since all these values just depend upon the current number to update, we do not require an array.

sum = sum + number

average = sum/count

For count == 1, min = max = input

rest all follows after that.

Below code: IDE Used - Dev-Cpp (for coding and testing), should be fine with VisualStudio.

Tested for all the conditions mentioned in the question plus matches the sample input/output

Code:

#include <iostream>
#include <iomanip>

using namespace std;

int main(){
  
   float number, max = 0, min =0, avg =0, sum= 0;
   int count = 0;
   cout<<\"Enter <ctrl>-z to terminate input \ \";
   while(cin>>number){
       count++;
       if(count == 1){
           min = number;
           max = number;
       } else{
           if(min>number)
               min = number;
           if(max<number)
               max = number;
       }
       sum+=number;
       avg = sum/count;
      
       cout<<setprecision(3)<<fixed;
       cout<<\"\\tCount = \"<<count<<\"\ \";
       cout<<\"\\tMinimum = \"<<min<<\" Maximum = \"<<max<<\"\ \";
       cout<<\"\\tSum = \"<<sum<<\" Average = \"<<avg<<\"\ \";
   }      
}

Coded in C++; Visual Studios 2013; Please help. Read in float values from tin until I press -z. This is the some as the eof in a file. As you read in each numbe
Coded in C++; Visual Studios 2013; Please help. Read in float values from tin until I press -z. This is the some as the eof in a file. As you read in each numbe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site