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

The instructions are as follows, however I want to remove the pointers in the co

ID: 3555327 • Letter: T

Question

The instructions are as follows, however I want to remove the pointers in the code to a more basic way of doing it. The instructions for the assignment and the code is included at the bottom.

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<fstream>
#include<vector>
using namespace std;

int readInput(vector<int>&);
void sort(int[], int); // removed pointer here.
void copy(vector<int>, int[]);// removed pointer here.

void writeToFile(vector<int>, int);
void writeToFile(int[], int);
void calculateMode(int[], int);

int main()
{
   vector<int> data;
   int arr[1300];
   int sum = readInput(data);
   copy(data, arr);
   sort(arr, data.size());
   writeToFile(data, data.size());
   writeToFile(arr, data.size());
   calculateMode(arr, data.size());

   cout << "All Normal Output will be written to the output file. " << "Program Over" << endl << "Press Enter to end -->";

   return 0;
}

int readInput(vector<int> &data)
{
   ifstream input("TopicFin.txt");
   int returnVal = 0;
   if (input.is_open())
   {
       int x;
       while (input >> x)
           data.push_back(x), returnVal += x;

   }

   return returnVal;
}

void copy(vector<int> data, int arr[]) // removed pointer here.
{
   for (int i = 0; i < data.size(); i++)
       arr[i] = data[i];

}

void sort(int arr[], int N) // removed pointer here.
{
   for (int i = 0; i < N; i++)
   for (int j = 0; j + 1 < N; j++)
   if (arr[j] > arr[j + 1])
   {
       int tmp = arr[j];
       arr[j] = arr[j + 1];
       arr[j + 1] = tmp;
   }

}

void calculateMode(int arr[1300], int N)
{
   int val = 1;
   int mode = arr[0];
   int occ_mode = 1;

   ofstream out;
   out.open("TopicFout.txt");
   for (int i = 1; i < N; i++)
   {
       if (arr[i] != arr[i - 1])
           val = 1;
       else
       {
           val++;
           if (val > occ_mode)
               occ_mode = val, mode = arr[i];
       }
   }
   out << " The median of the values is: " << arr[N / 2] << " ";
   out << "The mode of the values is " << mode << " which occurs " << occ_mode << " times. " << "Program Over";
   out.close();
}

void writeToFile(vector<int> v, int N)
{

   ofstream out("TopicFout.txt");

   if (out.is_open())
   {

       out << "The values read are: ";

       int sum = 0;
       for (int i = 0; i < N; i++)
           out << v[i] << " ", sum += v[i];
       out << " ";

       out << " Average of values is " << sum / v.size() << endl;
       out.close();
   }

}

void writeToFile(int v[], int N)
{
   ofstream out;
   out.open("TopicFout.txt");
   out << " The sorted result are: ";

   for (int i = 0; i < N; i++)
       out << v[i] << " ";

   out << " ";
   out.close();
}

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