Question

In C++ This program will read a group of positive numbers from three files ( not...

In C++

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 scores in the order that they were input, showing each number and what percentage it is above or below the average for each file. Note: Finding the average doesn’t require an array. Finding the median and the percentages does require an array.

  • The user should be prompted for the three file names which you should store in sring variables for file processing.
  • The program will read values until the user encounters an EOF for that file to quit.
  • The program must provide an error message for zero or other negative numbers.
  • Do not presume that all the numbers will be integers..
  • The program must provide reasonable prompts and error messages to the users.
  • The program’s output must be properly labelled.
  • The program must handle the case of an empty array properly by producing a reasonable error message. It must not attempt to do any calculations on the empty array.
  • The program’s output does not need to be lined up perfectly.
  • You must display the deviations from the average as positive numbers with some wording (e.g., 50.253% below and 25.22% above) rather than as a simple number (e.g., -50.253% and 25.22%) for each file with a heading for each file corresponding to the lable as given below..
  • Finally It should have embedded in each file a lable for that file ( name of the scores) such as class A, class B , class C etc.
  • Print out the lable for the file and the average for the file with the highest average and its value. Do the same for the file with the highest median, and f inally the file with the highest average of the percentage below the average for that file and its value.

Finding the Median

To find the median of a group of n numbers, sort them into order. If n is odd, the median is the middle entry. In an array named @data with elements starting at index zero, this will be element $data[(n-1)/2]. (Of course, you will have to put a scalar variable in place of n)

If n is even, the median is the average of the numbers “surrounding” the middle. For an array named @data with elements starting at index zero, this is ($data[(n/2)]+$data[(n/2)-1])/2.

Here is sample output from three separate runs of the program. User input in bold and italics.

Scores Report

Enter fie name for first file : class_A.txt

Enter fie name for first file : class_B.txt

Enter fie name for first file : class _C.txt

For class A
 
The average is 8
The median value is 6
Value   % above/below average
4       50% below
6       25% below
14      75% above

For class B

The average is 9
The median value is 10
Value   % above/below average
3       50% below
6       25% below
12      75% above

For class C

The average is 7
The median value is 6
Value   % above/below average
4       50% below
8       25% below
9       75% above

File A had the highest average of 35.5 
File B had the highest median of 34
File C had the highest average  percent of the scores below the average with a value of 47.25.
 
 

In this example, the percentages all happened to come out to be integers. For an arbitrary set of numbers, this won’t be the case. Do not truncate your results to integers use two decima places for values that our output.

Sorting an Array Numerically

When you sort an array, the default is to sort in alphabetical (ASCII/Unicode) order. This isn’t what you want for an array containing numbers. Otherwise, the number "47" (as a string) will sort before the number "5" (as a string).

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

olution(s):

Code :

// averageAndMedian.cpp : Defines the entry point for the console application.
//

#include<iostream>
#include<string>
#include <fstream>
#include <stdlib.h>

using namespace std;

//reading class from filename
double* readClass(string fileName,double& avg,double& countArray){
//defining local variables
   double *arrayName = NULL;
   double temp=0,sum=0;
   int count=0;
   string line;
   size_t sz = 0;
   ifstream f (fileName);
   //opening file
   if (!f.is_open())
   {
       //printing error if file not found
       perror("Error while opening file");
   }else{
   while(getline(f, line)) {
       //converting string to double  
       temp=stod(line);
       //checking if value is less than equal to zero
           if(temp<=0){
               //printing message and ignoring file
           cout<<"Value cannot be :"<<temp<<" in:"<<fileName<<endl;
           }else{
               //adding sum and in this loop only counting valid entries
           sum+=temp;
           count++;
           }
           //cout<<stod(line)<<endl;
       }
   //closing file
   f.close();
   ifstream f (fileName);
   int index=0;
   if(count>0)
   arrayName = new double [count]; //creating dynamic array

   //adding file data to new array
   while(getline(f, line)) {
           temp=stod(line);
           if(temp>0){  
               arrayName[index]=temp;
               index++;
           }
       }
   }
   //checking if file has some valid data
   if(sum!=0)
   {
       avg=sum/count;
       countArray=count;
   }else{//returning default
   avg=0;
   countArray=0;
   }
  
   return arrayName;
}

//function to swap two numbers
void swap(double *p,double *q) {
int t;

t=*p;
*p=*q;
*q=t;
}

//sort function
void sort(double a[],int n) {
int i,j,temp;

for(i = 0;i < n-1;i++) {
for(j = 0;j < n-i-1;j++) {
if(a[j] > a[j+1])
swap(&a[j],&a[j+1]);
}
}
}
//median function
double getMedian(double arrayName[],int n){
   sort(arrayName,n);
   if (n % 2 != 0)
return (double)arrayName[n/2];
  
return (double)(arrayName[(n-1)/2] + arrayName[n/2])/2.0;
}

//printing results and getting average below value
void printResults(double average,int count,double *className,double &averageBelow){
   int sumBelow=0;
   if(count>0)
   {
   cout<<"The Average Value Is : "<<average<<endl;
   cout<<"The Median Value Is : "<<getMedian(className,count)<<endl;
   cout<<"Value % above/below average ";
   for(int i=0;i<count;i++)
   {
       double percentBelowAbove=0;
       percentBelowAbove=((className[i]-average)/average) * 100;
       if(percentBelowAbove<0)
       {
           cout<<className[i]<<" "<<percentBelowAbove*-1<<"% below ";
           sumBelow-=percentBelowAbove;
       }else if(percentBelowAbove>0)
       {
           cout<<className[i]<<" "<<percentBelowAbove<<"% above ";
       }else{
           cout<<className[i]<<" % is same ";
       }
   }
   averageBelow=sumBelow/count;
   }else{
   cout<<"No valid data found";
   averageBelow=100;
   }
  
}
//Main class to call functions
void main()
{
   double *classA = NULL;
   double *classB = NULL;
   double *classC = NULL;
   double classAAvergage,countA,classBAvergage,countB,classCAvergage,countC;
   double classAAvergageBelow,classBAvergageBelow,classCAvergageBelow;
   double classAMedian,classBMedian,classCMedian;
   string classAFileName,classCFileName,classBFileName;

   cout<<"Enter Name for first file: ";
   //classAFileName="c:\users\faraz\desktop\a.txt";
   //classBFileName="c:\users\faraz\desktop\b.txt";
   //classCFileName="c:\users\faraz\desktop\c.txt";
   cin>>classAFileName;
   cout<<"Enter Name for second file: ";
   cin>>classBFileName;
   cout<<"Enter Name for third file: ";
   cin>>classCFileName;

   classA=readClass(classAFileName,classAAvergage,countA);
   classB=readClass(classBFileName,classBAvergage,countB);
   classC=readClass(classCFileName,classCAvergage,countC);
  
   cout<<" For class A ";
   printResults(classAAvergage,countA,classA,classAAvergageBelow);
   cout<<" For class B ";
   printResults(classBAvergage,countB,classB,classBAvergageBelow);
   cout<<" For class C ";
   printResults(classCAvergage,countC,classC,classCAvergageBelow);
   classAMedian=getMedian(classA,countA);
   classBMedian=getMedian(classB,countB);
   classCMedian=getMedian(classC,countC);
  
   if(classAAvergage>classBAvergage && classAAvergage>classCAvergage)
   {
       cout<<"File A had the highest average of "<<classAAvergage<<endl;
   }
   else if(classBAvergage>classAAvergage && classBAvergage>classCAvergage)
   {
       cout<<"File B had the highest average of "<<classBAvergage<<endl;
   }
   else if(classCAvergage>=classAAvergage && classCAvergage>classBAvergage)
   {
       cout<<"File C had the highest average of "<<classCAvergage<<endl;  
   }else
   {
   cout<<"All average are same having value of "<<classAAvergage<<endl;
   }

   if(classAMedian>classBMedian && classAMedian>classCMedian)
   {
       cout<<"File A had the highest median of "<<classAMedian<<endl;
   }
   else if(classBMedian>classAMedian && classBMedian>classCMedian)
   {
       cout<<"File B had the highest median of "<<classBMedian<<endl;
   }
   else if(classCMedian>=classAMedian && classCMedian>classBMedian)
   {
       cout<<"File C had the highest median of "<<classCMedian<<endl;  
   }else
   {
   cout<<"All median are same having value of "<<classAMedian<<endl;
   }

   if(classAAvergageBelow>classBAvergageBelow && classAAvergageBelow>classCAvergageBelow)
   {
       cout<<"File A had the highest average percent of the scores below the average with a value of "<<classAAvergageBelow<<endl;
   }
   else if(classBAvergageBelow>classAAvergageBelow && classBAvergageBelow>classCAvergageBelow)
   {
       cout<<"File B had the highest average percent of the scores below the average with a value of "<<classBAvergageBelow<<endl;
   }
   else if(classCAvergageBelow>=classAAvergageBelow && classCMedian>classBAvergageBelow)
   {
       cout<<"File C had the highest average percent of the scores below the average with a value of "<<classCAvergageBelow<<endl;  
   }else
   {
   cout<<"All median are same having value of "<<classAAvergageBelow<<endl;
   }  


   system("Pause");
}

output:

Add a comment
Know the answer?
Add Answer to:
In C++ This program will read a group of positive numbers from three files ( not...
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
  • 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...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes...

    CIS 22A C++ Project Exam Statistics Here is what your program will do: first it welcomes the user and displays the purpose of the program. It then prompts the user to enter the name of an input file (such as scores.txt). Assume the file contains the scores of the final exams; each score is preceded by a 5 characters student id. Create the input file: copy and paste the following data into a new text file named scores.txt DH232 89...

  • C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that...

    C langauge Please. Complete all of this please. Thanks :) Statistical Analysis Write a program that creates an array of 10 integers. The program should then use the following functions: getData() Used to ask the user for the numbers and store them into an array displayData() Used to display the data in the array displayLargest() Used to find and display the largest number in the array (prior to sort). displaySmallest() Used to find and display the smallest number in the...

  • Write a program that will take input from a file of numbers of type double and...

    Write a program that will take input from a file of numbers of type double and output the average of the numbers in the file to the screen. Output the file name and average. Allow the user to process multiple files in one run. Part A use an array to hold the values read from the file modify your average function so that it receives an array as input parameter, averages values in an array and returns the average Part...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • C++ programming please Write a program that will display random numbers in order from low to...

    C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: 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