Min,max and average of array

Hi, I tried to make a program that print out a min , max and average value

but the average value doesn't work !!!

What should I do to fix this problem.


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
#include <iostream>
using namespace std;


int main ()
{




    int array [5],max=0,low=0,mid=0;

    for (int w=0 ; w<5; w++)
    {
        cout << "Enter value "<<endl;

        cin >>    array[w];

    }

    max=array[0];
    low=array[0];
    mid=array[0];

    for (int i=0 ; i<5; i++)
    {







        if ( max<array[i])
        {

            max=array[i];
        }


        else if ( low>array[i])
        {
            low=array[i];

        }

        else if (mid<=array[i]&&mid>=array[i])
        {

            mid= max+low/2;

        }


    }

    cout << "the minimum value "<<"  "<< low<< "  " << "the average value "<<mid<< "  "<<"the highest value"<<"  "<<max<<endl;


}



Last edited on
Average is calculated like so:
1
2
3
4
5
int average=0;
for(int i=0; i<5; i++) {
	average+=A[i];
}
average/=5;
What kind of average are you trying to calculate?
http://en.wikipedia.org/wiki/Average

This else if (mid<=array[i]&&mid>=array[i]) is only true if mid == array[i]. I don't think this is what you wanted.
still doesn't work

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
#include <iostream>
using namespace std;


int main ()
{




    int array [5],max=0,low=0,mid=0;

    for (int w=0 ; w<5; w++)
    {
        cout << "Enter value "<<endl;

        cin >>    array[w];

    }

    max=array[0];
    low=array[0];
    mid=array[0];

    for (int i=0 ; i<5; i++)
    {







        if ( max<array[i])
        {

            max=array[i];
        }


        else if ( low>array[i])
        {
            low=array[i];

        }

        mid+=array[i];



    }
    mid/=5;

    cout << "the minimum value "<<"  "<< low<< "  " << "the average value "<<mid<< "  "<<"the highest value"<<"  "<<max<<endl;


}






if the input were 1 ,2 ,5,8 and 6 the ave value will be 4



----

I want the average value of the input numbers ( the middle number ^^ )


like

1

11

5

10

100

the middle number will be 10 !


that what I want .
Last edited on
closed account (28poGNh0)
You can do it at this way

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
# include <iostream>
# include <limits.h>// To define INT_MAX : the highest nbr int takes
using namespace std;

int main ()
{
    int array [5],maxNbr=0,minNbr=INT_MAX,sumNbr=0,tmpNbr;
    float aveNbr=0;

    for (int w=0 ; w<5; w++)
    {
        cout << "Enter value -> " ;
        cin >> array[w];
    }

    for (int i=0 ; i<5; i++)
    {
        maxNbr = max(maxNbr,array[i]);
        minNbr = min(minNbr,array[i]);
        sumNbr += array[i];
    }

    aveNbr = (float(sumNbr)/5);

    for(int i=0;i<4;i++)
    {
        for(int j=i;j<5;j++)
        if(array[i]>array[j])
        {
            tmpNbr = array[i];
            array[i] = array[j];
            array[j] = tmpNbr;
        }
    }

    for(int i=0;i<5;i++)
    cout << array[i] << endl;
    cout << endl;
    midNbr = sumNbr/5;//midNbr = sumNbr/strlen(array);

    cout << "The minimum value is : " << minNbr << endl;
    cout << "The maximum value is : " << maxNbr << endl;
    cout << "The average       is : " << aveNbr << endl;
    cout << "The middle number is : " << array[2] << endl;

    return 0;
}
Topic archived. No new replies allowed.