Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program. In order to get full credit, your programs MUST compile. Al

ID: 639570 • Letter: W

Question

Write a C++ program. In order to get full credit, your programs MUST compile. Also, do not forget properdocumentation of your code. USE functions at all times for a modular program.

Write a program to read a set of numbers representing the salary of employees of alocal company. Assume this data comes from the attached data file (emp.txt). Yourprogram must read the name of the file entered by the user. Use a one-dimensional arrayto store the salary values. You need a function to read the data (make sure to read to theend of the file and do not assume a constant number of entries), a function to calculate theaverage of salary values, a function to sort the data in ascending order, and a function tooutput the average of salary values. Make sure to call the functions to solve this problem.

Here is the data file.

2500.07

1500.82

3200.00

1100.92

Explanation / Answer

#include <iostream>
#include <string>
#include<fstream>

using namespace std;
void readData(double data[],int *length);
void sort(double data[],int length);
double average(double data[],int length);
void display(double data[],int length,double average);
int main()
{

int length;
double avg;
double data[1000];
readData(data,&length);
sort(data,length);
avg = average(data,length);
display(data,length,avg);

return 0;
}

void readData(double data[],int *length)
{

   int i=0;
   char fileName[50];
   cout<<"Enter File Name: ";
   cin>>fileName;

   ifstream infile;
   infile.open(fileName);
   if(infile.is_open())
   {
       while (!infile.eof())
       {

           infile >> data[i];
           i++;
       }
       *length = i-1;
       infile.close();
   }
   else
   {

       cout<<"File Not found ";
   }
}

void sort(double a[],int length)
{

   double temp;
   for(int i=0;i<length-1;i++)
   {
       for(int j=i+1;j<length;j++)
       {
           if(a[i]>a[j])
           {
               temp=a[i];
               a[i]=a[j];
               a[j]=temp;
           }
       }
   }
}
double average(double data[],int length)
{

   double sum=0;
   double average;
   for(int i=0;i<length;i++)
   {
       sum = sum +data[i];
   }
   average = sum/length;
   return average;
  
}
void display(double data[],int length,double average)
{  

   cout<<"------------------ ";
   cout<<"Sorted Data is ";
   cout<<"------------------ ";
   for(int i=0;i<length;i++)
   {
       cout<<data[i]<<endl;
   }
   cout<<"------------------ ";
   cout<<"Average is = "<<average<<endl;
   cout<<"------------------ ";

}

=============================================

Input and Output

==============================================

Enter File Name:
emp.txt
------------------
Sorted Data is
------------------
1100.92
1500.82
2500.07
3200
------------------
Average is = 2075.45
------------------
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote