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??

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

#include <iostream>
#include <iomanip>
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<n; i++)
               total = total + v[i];
       double average = total/n;
       return average;
}

double calcMedian (int v[],int n)
{
       double median;

       if(n%2==0)
       {   int middle1 = n/2;
           int middle2 = (n/2)-1;
           median = (v[middle1] + v[middle2])/2;
       }
       else
       {
              int middle = n/2;
              median = v[middle];
       }
       return median;
}


void bubbleSort (int v[],int n)

{

	for (int i =0 ; i<n; i++)
    {
		for (int j = 0; j < n-i-1; j++)
        {
			if (v[j] > 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<n;i++)
        if (i<i+1)
            int min=i;
    cout << "The min value of the elements is: " << array [0];
}
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
You didn't reply to anything I asked.
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.