### Problem with array in method

I simply cannot figure out how to get my program to incorporate the methods for min and max. i know that the min is array [0] and max is array [n]how do i programthat properly??

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` `````` #include #include using namespace std; void bubbleSort (int v[],int n); double calcMean (int v[],int n); double calcMedian (int v[],int n); double calcMax (int v[], int n); double calcMin (int v[], int n); int main () { int size; cout << "Enter Data Count" << endl; cin >> size; int array[size]; int i; for (i = 0; i < size; i++) { cout << "\nPlease Enter grade " << i+1 << " : "; cin >> array[i]; } double mean = calcMean (array, size); cout << std::fixed << "\n\nThe mean of all the elements is : " << setprecision(2) << mean << endl; bubbleSort(array, size); cout << "\nThe sorted array is : " << endl; for (i = 0; i < size; i++) { if(i%5==0) cout << "\n"; cout << array[i] << '\t'; } double median = calcMedian (array, size); cout << "\n\nThe median of all the elements is: " << setprecision(2) << median << endl; calcMin(array,size); return 0; } double calcMean (int v[],int n) { int total =0; for(int i =0 ; i v[j+1]) { int temp = v[j]; v[j] = v[j + 1]; v[j + 1] = temp; } } } } double calcMax (int v[],int n) { int array [n]; } double calcMin (int v[],int n) { for (int i=0;i
Your question is vague - what is your program supposed to do exactly?

And I don't see int v[] and int n actually declared anywhere. I doubt your code even compiles. And if (i<i+1) will always be true, why bother with an if statement? And if you know what min and max are why do you need to calculate them?
Last edited on
it compiles without the last 2 methods calcMax and calcMin. I don't know what to put in the 2 methods in order to output the max and min values the user inputs. i know they are simply the 1st and last numbers in the method for sorting them
my program needs to output mean,median,max,min through separate methods. therefore, i don't know what to put into the max and min methods so i can output the min and max in the main as i have the mean and median working. the i<i+1 i clearly am confused at that point and don't know what to do in the two methodsfor the max and min
Where are int v[] and int n declared/initialized? Is this all your code or only part? I can't follow the logic of your code unless I can see all of it.
agnophilo look at his code..they are the parameters of his functions calcMax and calcMin.
and as far as max and min..there is this really neat function in the algorithm header called max_element , min_element.
for your calcMax do something like this.
`return( std::max_element( v.begin() , v.end() ); );`
instead of what you have and the same thing but with min_element for your calcMin.

http://www.cplusplus.com/reference/algorithm/max_element/
http://www.cplusplus.com/reference/algorithm/min_element/

You can also even use this
http://www.cplusplus.com/reference/algorithm/minmax_element/

ps agnophilo when you call a function you do not have to have same parameter names as the prototype only the same type of objects.
Last edited on
"agnophilo look at his code..they are the parameters of his functions calcMax and calcMin."

I know, I was asking where they are initialized and where the rest of the code is.

"and as far as max and min..there is this really neat function in the algorithm header called max_element , min_element."

I see no header file, nor do I see min_element anywhere. I may be being stupid here, I know nothing about header files (aren't they name.h files that were a C thing, not C++?).

"ps agnophilo when you call a function you do not have to have same parameter names as the prototype only the same type of objects. "

I know. Not sure what your point is.
Last edited on
they are under the algorithm library part of the stl container
I sent the links to them. And you can use .h headers in c++ but most people use .hpp I just meant it as a precompiled library I forgot what they are called.
Last edited on
Has anyone noticed that array sizes must constant and known at compile time?

If this is not what you want then use a vector or one of the other STL containers.

For beginners - If you compilation errors - then post them in full - why do I have to repeat my self about this to various beginners all the time?

The last 2 functions don't return anything.
Topic archived. No new replies allowed.