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

Description: randomA.txt: randomB.txt: Output: Description main a main function

ID: 3599074 • Letter: D

Question

Description: randomA.txt:
randomB.txt:
Output:
Description main a main function has been given. You are to only add the function calls. The comments will tell you where the function calls should be done. Do NOT change main. The following functions should be implemented. loadArray This function has an array and a variable to count the number of elements in the array passed into it. The function asks the user for the file name, opens the file with the filename and reads the numbers into the array. It also keeps track array. It makes sure the file opens correctly and if it doesn't, it exits the program. This method should also check to make sure that more than the MAXSIZE values are not put into the array of how many numbers are in the printArray this function is passed an array and the number of values in the array. It then prints averageArray this function is passed an array and the number of values in the array. It returns sortArray sorts the numbers in the array. It is passed the array and the number of values in the countMultiplesOf this function is passed an array, the number of values in the array, and the totalOfNumbers this function is passed an array and the number of values in the array. It findMedian this function is passed an array and the number of values in the array. It returns the the values in the array, eight per line as shown in the output the average of the numbers in the array array number that the function is counting the multiples of. For example, if a five is passed in, this function will count all the multiples of five in the array returns the total of the numbers in the array median of the numbers in the median is the middle number (ex: the median of 1, 2, 3, 4, 5 is 3). If there is an even number of elements in the array, the median is the average of the middle 2 numbers. (ex: the median of 1 2, 3, 4 is 2.5) There is NO loop in this method array. If there are an odd number of elements in the array, the Sample output is on the next page Requirements 1. 2. 3. Code should run correctly for all files of the format given. Code that doesn't compile will receive a zero Comments should be at the top of the program which have your name, class, etc. (Check Assignment one for format) 4. Submit the C+ file to moodle by the due date

Explanation / Answer

Here are the functions for you:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#define MAXSIZE 1000
using namespace std;

/*
   This function has an array and a variable to count the number of elements in the array
   passed into it. The function asks the user for the file name, opens the file with the filename
   and reads the numbers into the array. It also keeps track of how many numbers are in the
   array. It makes sure the file opens correctly and if it doesn't it exits the program. This
   method should also check to make sure that more than the MAXSIZE values are not put into
   the array.
*/
void load(int array[], int &count)
{
    string fileName;
    cout << "Enter the file that you would like to read data from: ";
    cin >>fileName;
    ifstream fin;
    fin.open(fileName);
   
    if(!fin.is_open())
    {
       cout << "Unable to open the input file. Exiting the program..." << endl;
       exit(1);
    }
    count = 0;
    while(!fin.eof() && count < MAXSIZE)
        fin >> array[count++];
}

/*
   This function is passed an array and the number of values in the array. It then prints
   the values in the array, eight per line as shown in the output.
*/
void printArray(int array[], int count)
{
        int numsInLine = 0;
        for(int i = 0; i < count; i++)
        {
           cout << setw(8) << array[i];
           numsInLine++;
           if(numsInLine == 8)
           {
              cout << endl;
              numsInLine = 0;
           }
        }
        cout << endl;
}

/*
   This function is passed an array and the number of values in the array. It returns the
   average of the numbers in the array.
*/
double averageArray(int array[], int count)
{
    double avg = 0.0;
    for(int i = 0; i < count; i++)
        avg += array[i];
    return avg / count;  
}

/*
   Sorts the numbers in the array. It is passed the array and the number of values in the array.
*/
void sortArray(int array[], int count)
{
    for(int i = 0; i < count-1; i++)
        for(int j = i; j < count-i-1; j++)
            if(array[j] > array[j+1])
            {
               int temp = array[j];
               array[j] = array[j+1];
               array[j+1] = temp;
            }
}

/*
   This function is passed an array, the number of values in the array, and the number that
   the function is counting the multiples of. For example, if a five is passed in, this
   function will count all the multiples of five in the array.
*/
int countMultiplesOf(int array[], int count, int value)
{
    int multiples = 0;
    for(int i = 0; i < count; i++)
        if(array[i] % value == 0)
            multiples++;
    return multiples;      
}

/*
   This function is passed an array and the number of values in the array. It returns
   the total of the numbers in the array.
*/
int totalOfNumbers(int array[], int count)
{
    int total = 0;
    for(int i = 0; i < count; i++)
        total += array[i];
    return total;  
}

/*
   This function is passed an array and the number of values in the array. It returns the
   median of the numbers in the array. If there are an odd number of elements in the array,
   the median is the middle number. If there are even number of elements in the array, the
   median is the average of the middle 2 numbers. There is NO loop in this method.
*/
double findMedian(int array[], int count)
{
    sortArray(array, count);
    if(count % 2 == 1)
        return array[count/2];
    else
        return (array[count/2] + array[count/2-1]) / 2.0;
}