so I am writing this program that reads user input into an array and then finds the mean and median. I have three different files that I am going to compile together in unix:
1) stats.h, which contains the prototypes that I was given for my mean, median, and sort functions
2) stats.cpp, where I am supposed to implement these three functions, and
3) main.cpp, with my main function.
So far, my main() is functioning correctly. There are two main issues that I am having:
1) I want to make sure that I am passing the array[] and its size correctly to each of the three functions (I'm a little rusty on passing arrays).
2) I was given the prototypes for my three functions (including the void Sort function) and I was told to call the Sort function in the Median function.
I don't really understand how to update the old array in the Median function if I can't return the sorted array in the Sort function (if that makes sense).
So here are my three files: 1) stats.h
//not sure if I need to include anything at top
float Mean (const int* array, size_t size);
float Median (int* array, size_t size);
void Sort (int* array, size_t size);
2) stats.cpp
#include <stats.h> //only include header file with prototypes?
//function to find the mean that should be called by
main()
float Mean (int NewArray[], size_t size)
{
float sum, mean = 0.0;
for (int i = 0; i < size; i++)
{
sum += NewArray[i];
}
mean = ((float)sum)/size;
return mean;
}
//function to find median that should be called by main()
float Median (int NewArray[], size_t size);
{//not sure how to call sort (void) function and return new sorted array to median function
if(size % 2 == 0)
{
median = (NewArray[size/2] + NewArray[size/2-1])/2.0f;
}
else
{
median = NewArray[size/2];
}
return median; //return median to main()
}
// void function to sort array in ascending order and called by median()
Sort(int NewArray[], size_t size)
{
for (i = 0; i < size; i++)
{
for(int j = 0; j < size-1; j++)
{
if(NewArray[j] > NewArray[j+1])
{
int temp = NewArray[j+1];
NewArray[j+1] = NewArray[j];
NewArray[j] = temp;
}
}
}
}
3) And main()
#include <iostream>
#include <stats.h>
#include <stats.cpp>
int main()
{
int input, sum, NumberOfEntries = 0;
const int SIZE = 100
int array[SIZE];
std::cout << "Please enter integers ('x' to quit): ";
for (int i = 0; i < SIZE; i++)
{
if (std::cin >> array[i])
{
}
else
{
NumberOfEntries = i;
break;
}
}
std::cout << "Data as entered: ";
for (int i = 0; i < NumberOfEntries; i++)
std::cout << array[i] << " ";
std::cout << "Mean: " << float Mean (array[SIZE], NumberOfEntries);
std::cout << "Median: " << float Median (array[SIZE], NumberOfEntries);
std::cout << "Data after sort: "; //would print out sorted array
return 0;
}