Thank you both for your help! It's correctly storing the input in the array. However, I have finished the rest of my code and I am having some issues with it. If you don't mind, would you take a quick look to see if anything stands out? Thanks! And no, dhayden, I haven't learned about vectors yet, so I pretty sure I'm supposed to use an array.
So I have three files for this program (that are all compiled together in unix): 1) stats.h, which contains the prototypes 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. And then my two main issues are at the bottom.
1) stats.h (I was given these prototypes to work with)
1 2 3 4
|
//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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#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;
{
//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.cpp (which is the same as above with these calls added at the bottom)
1 2 3 4 5 6 7 8
|
std::cout << "Mean: " << float Mean (array[SIZE], NumberOfEntries);
std::cout << "Median: " << float Median (array[SIZE], NumberOfEntries);
std::cout << "Data after sort: ";
return 0;
}
|
Okay, so there are my three files and there are two main problems 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.
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).