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: 3546261 • 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)

Explanation / Answer

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

void print( const vector<int>& a)
{
    for(int i=0; i<a.size(); ++i)
        cout << a[i] << " ";
}

int readInput(vector<int> &read)
{
string line;

int var=0;
int sum=0;
ifstream myfile ("TopicFin.txt");
if (myfile.is_open())
{
    while ( getline (myfile,line) )
    {
      //cout << line << endl;
      //istringstream (line) >> var;
      var=stoi(line.c_str());
      sum=sum+var;
      read.push_back(var);
    }
    myfile.close();
   
}
return sum;
}

void sort(vector<int> &read, int *sorted)
{
    std::vector<int> myvector (read);
    std::sort (myvector.begin(), myvector.end());  
    for(int i=0;i<myvector.size();i++)
    {
        sorted[i]=myvector[i];
    }
}

void calculateMode(int *array,int size)
{
    int number = array[0];
    int mode = number;
    int count = 1;
    int countMode = 1;
   
    for (int i=1; i<size; i++)
    {
          if (array[i] == number)
          { // count occurrences of the current number
             countMode++;
          }
          else
          { // now this is a different number
                if (count > countMode)
                {
                      countMode = count; // mode is the biggest ocurrences
                      mode = number;
                }
               count = 1; // reset count for the new number
               number = array[i];
      }
    }
   
    cout << "mode : " << mode << " no of times " << countMode << endl;
}

void write(int *array, int size)
{
ofstream myfile;
myfile.open ("TopicFour.txt");
for(int i=0; i<size; ++i)
        myfile << array[i] << " ";
myfile << " " ;
myfile.close();
}

void write(vector<int> &a, int size)
{
ofstream myfile;
myfile.open ("TopicFour.txt");
for(int i=0; i<size; ++i)
        myfile << a[i] << " ";
myfile << " " ;
myfile.close();
}

int main()
{
    vector<int> read;
    int sorted[1300];
    int sum;
   sum=readInput(read);
   if(sum==0) exit(1);
   //print(read);
   sort(read,sorted);
   //for(int i=0;i<read.size();i++)
   //cout<<sorted[i];
   calculateMode(sorted, read.size());
   write(read, read.size());
   write(sorted, read.size());
   return 0;
}

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