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

For this project you will write a program to compute the arithmetic mean (averag

ID: 3762663 • Letter: F

Question

For this project you will write a program to compute the arithmetic mean (average), median, and mode for the values read in from TopicFin.txt. The program results will be written to TopicFout.txt.

The mode of a list of values is the score that occurs most often. The median is the value in the middle of the list. If there are an odd number of values in the list, then the median value can be determined by selecting the value at last / 2 where last is the last index that has a value in the array. If the list has an even number of values, then the median is the average of the value at last / 2 and last / 2 + 1. Using the list at the bottom of page 469 in the text, the median is the average of the values at index 2 (5 / 2) and index 3 (5 / 2 + 1) 5 + 7 / 2 = 6. So 6 is the median value of the list. Notice that 6 is not actually a value in the list.

The program will have a vector and an array. The vector will contain the values read in from the file. The array will contain those values in sorted order. You can assume that there will be no more than 1300 values. However, your program must handle files values less than 1300. You can assume that a file will have values and that these will be valid values.

At a minimum your program must have the following functions:

readInput This function reads the values in the file into the vector. It returns the sum of the values in the file. This is used by main to calculate the average. If the file cannot be found, the function should return a value of 0 for the total. This will prompt main to end the program. main will display an error message in this case.

copy - A function to copy the vector data into the array.

sort This function is a void function which sorts the values. The sorted values are stored in the array. When the function ends, main should have the array with the sorted values and the vector with the values in their original order. Feel free to base your function on one of the text's sort functions.

calculateMode This is a void function which determines the mode of the array and how many times that value is in the list. This function requires that the list be sorted, therefore it uses the array not the vector.

writeToFile This void function writes the values in the list to the output file. There should be two of these functions, one for the array and one for the vector. That is, this function is overloaded.

Explanation / Answer


/**C++ program that reads a text file caled "TopicFin.txt"
and performs all the operations as specified in the post.*/
//include header files
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
//set size of vector
#define max 1300
//function prototypes
void print(int arr[], int count);
int readInput(vector<int > &list, ifstream &ifs, int &count);
void copy(vector<int>&list,int arr[], int count);
void calculateMode(int array[], int size,int &mode);
void writeToFile(ofstream &fout,int arr[],int count,int mode);
void sort(int arr[], int count);
//void writeToFile(ofstream &fout,vector<int> arr ,int count,int mode);
int main()
{


   //create a vector of size max
   vector<int> list(max);

   //open a input stream of input file
   ifstream fin;
   fin.open("TopicFin.txt");

   int count=0;

   //read from file to vector and finds the number of elements inthe file in count variable
   int total=readInput(list,fin ,count);


   //if count is zero then exit the program
   if(total==0)
   {
       system("pause");
       exit(0);
   }

   int arr[max];


   //copy from vector to array upt count
   copy(list,arr,count);
   //call sort to sort the numers in array
   sort(arr,count);
   int mode=0;
  
   //call method to find the mode value
   calculateMode(arr,count,mode);

   ofstream fout;
   fout.open("TopicFout.txt");

   //write the array values along with mode
   writeToFile(fout, arr, count,mode);

   system("pause");
   return 0;
}


//Read input file to vector
int readInput(vector<int > &list, ifstream &fin, int &count)
{

   int total=0;
   int number=0;

   //check if file exist
   if(!fin)
   {
       cout<<"File doest not exist ."<<endl;
       //return 0 if file not exist
       return 0;
   }

   //otherwise total the numbers in the file
   while(fin>>number)
   {
       list.insert(list.begin()+count,number);
       count++;
       total+=number;
   }

   fin.close();

   //return total vector elements
   return total;
}


//Copy element of vector to array
void copy(vector<int>&list,int arr[], int count)
{

   for(int index=0;index<count;index++)
   {
       arr[index]=list.at(index);
       list.pop_back();
   }
}


//Call sort method to sort the elements of array
void sort(int arr[], int count)
{

   for(int index1=0;index1<count;index1++)
   {
       for(int index2=0;index2<count-index1-1;index2++)
       {
          
           if(arr[index2]>arr[index2+1])
           {
               int temp=arr[index2];
               arr[index2]=arr[index2+1];
               arr[index2+1]=temp;
           }
       }
   }
}

//Method to find the mode of the array values into the mode reference value
void calculateMode(int arr[], int size,int &mode)
{
  
        int counter = 1;
        int maximum = 0;
        mode = arr[0];
        for (int pass = 0; pass < size - 1; pass++)
        {
           if ( arr[pass] == arr[pass+1] )
           {
              counter++;
              if ( counter > maximum )
              {
                  maximum = counter;
                  mode = arr[pass];
              }

           }
           else
              counter = 1;
        }
   
}


//Method that writes the array to output file TopicFout.txt and mode value
void writeToFile(ofstream &fout,int arr[],int count,int mode)
{
   int index=0;
   while(index<count)
   {
       fout<<arr[index]<<endl;
       index++;
   }


   //write mode and median values to text file
   fout<<"Mode is "<<mode<<endl;
  
   int median=0;

   if((count-1)%2==1)
       median=arr[(count-1)/2];
   else
       median=arr[(count-1)/2]+arr[(count-1)/2+1];

   fout<<"Median is "<<median<<endl;


   fout.close();
}

-------------------------

input text file

TopicFin.txt

1
2
2
2
7
9
12
10

--------------------------

output file

TopicFout.txt

1
2
2
2
7
9
10
12
Mode is 2
Median is 2

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