Array problem

Hello all, i'm constructing a program for a class in university within which i had to store some numbers to an array. Then find the average of the numbers entered, the minimum and also the maximum. I've done this no problem. however when I compile it and enter the numbers, the average and max is displayed correctly but the minimum continually comes up as zero, just curious if there are any kind people out there, who wouldnt mind spending 2 minutes maybe looking at my code to see if you can spot the problem. I'm sure its trivially easy but i cannot seem to see it. Thanks in advance if anyone helps me out :)
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
#include <stdio.h> 

void main()
{

int count, num;
float total, average, max, min;
int myarray[6];

total = 0;
max = 0;
min = 0;

for (count = 0; count < 5; count++)
{
printf("Please enter a number: ");
scanf("%d",&myarray[count]);
total = total + myarray[count];
}

average = total / 5;
printf("The average of the numbers entered is: %.2f",average);

for (count = 0; count < 5; count ++)
{
	if(myarray[count]>=max)
	{
		max=myarray[count];
	}
    if(myarray[count]<=min)
	{
		min=myarray[count];
	}
}
printf("The maximum number is: %.2f",max);
printf("The minimum number is: %.2f",min);

} 


heres my program anyway, its driving me loopy lol it has to be in tomorrow too, looks perfect to me...
Last edited on
1: When posting here use the code tags
2: It is extremely wise to use indentation

1
2
3
int main(){
     cout<<"Hello, World!\n";
}


Basically after every "{" tab and after every "}" untab
sorry, missed the code tags rule, should've known that. and my program is indented where necessary but it didnt come out in the format when i c+p'd it, i'll sort it now
First thing I notice is your array is 6 deep and your loops go 0 to 4 (count < 5). Don't know if that is the cause... I didn't dig much deeper but it is something you should look at.
there is no reason why min max should not be ints?
haha figured it out; min is never set to be bigger than

when declared code to follow;
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
#include <iostream> 

using namespace std;

void main()
{
	int count, num;
	float average;
	int myarray[6], max(0), min;
	long total(0);

	for (count = 0; count < 5; count++)
	{
		cout << "Please enter a number: ";
		cin >> myarray[count];
		total = total + myarray[count];
	}

	average = total / 5;
	cout << "The average of the numbers entered is: " << average << endl;
	
	min = myarray[1];    //this is the inportant bit

	for (count = 0; count < 5; count ++)
	{
		if(myarray[count]>=max)
		{
			max=myarray[count];
		}
		else if(myarray[count]<=min)
		{
			min=myarray[count];
		}
	}

	cout << "The maximum number is:" << max << endl;
	cout << "The minimum number is:" << min << endl;
	cin >> min;
} 


Please enter a number: 2
Please enter a number: 3
Please enter a number: 4
Please enter a number: 5
Please enter a number: 3
The average of the numbers entered is: 3
The maximum number is:5
The minimum number is:3


Last edited on
see my above post, negs will be less than zerro therefore be picked up.
just trying to figure out how it would be set out in the stdio.h library. unfortunately they've neglected to teach us how to use the iostream lib.
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
#include <iostream> 

using namespace std;

void main()
{
	int count, num;
	float average;
	int myarray[6], max(0), min(INT_MAX);
	long total(0);

	for (count = 0; count < 5; count++)
	{
		cout << "Please enter a number: ";
		cin >> myarray[count];
		total = total + myarray[count];
	}

	average = total / 5;
	cout << "The average of the numbers entered is: " << average << endl;

	for (count = 0; count < 5; count ++)
	{
		if(myarray[count]>=max)
		{
			max=myarray[count];
		}
		else if(myarray[count]<=min)
		{
			min=myarray[count];
		}
	}

	cout << "The maximum number is:" << max << endl;
	cout << "The minimum number is:" << min << endl;
	cin >> min;
} 


actually this is MUCH better
Last edited on
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
#include <stdio.h> 

void main()
{
int count, max(0), min;
float total, average;
int myarray[5];

total = 0;


for (count = 0; count < 5; count++)
{
	printf("Please enter a number: ");
	scanf("%d",&myarray[count]);
	total = total + myarray[count];
}

average = total / 5;
printf("\n\nThe average of the numbers entered is: %.2f\n\n",average);

min = myarray[1];

for (count = 0; count < 5; count ++)
{
	if(myarray[count]>=max)
	{
		max=myarray[count];
	}
	else if(myarray[count]<=min)
	{
		min=myarray[count];
	}
}
printf("\n\nThe maximum number is: %d\n\n",max);
printf("\n\nThe minimum number is: %d\n\n",min);

}


this is what i have now, slightly adapted from your fix. However now if i enter the first number as the lowest it doesnt pick it up and instead picks up the next lowest
Last edited on
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
#include <stdio.h> 

void main()
{
	int count, max(0), min(0x7fffffff); //0x7fffffff maximum number int can hold (hex)
	float total, average;
	int myarray[5];

	total = 0;


	for (count = 0; count < 5; count++)
	{
		printf("Please enter a number: ");
		scanf("%d",&myarray[count]);
		total = total + myarray[count];
	}

	average = total / 5;
	printf("The average of the numbers entered is: %.2f",average);

	for (count = 0; count < 5; count ++)
	{
		if(myarray[count]>=max)
		{
			max=myarray[count];
		}
		else if(myarray[count]<=min)
		{
			min=myarray[count];
		}
	}
	printf("The maximum number is: %.2d",max);
	printf("The minimum number is: %.2d",min);
	scanf("%d",&myarray[count]);
}



OK this works for me, removed INT_MAX as it is not present in stdio.h

hope this helps
The help is much appreciated :) but it still refuses to pick up that first integer and my code is EXACTLY like above
I think if you go back Lisky8 (5) post, the code is okay.

Just change min = myarray[1] to myarray[0] the code works.

Hope that helps.
heh, one number and it worked, i guess thats C programming for you! Many thanks to everyone that helped.
yeah, sorry bout that;

however I highly recommend instead of doing what i first showed you, to set int min to 0x7FFFFFFF (that is hex for the largest value an int could be).

good luck.

the code in my previous post is what work the best.
just one comment: generally speaking you should initialize both MIN and MAX either with the first element in container (array, vector, whatever...) or with MAXimal and MINimal values respectively of a corresponding type. However, often these values are not well-known. That's why initializing with the first (actually, any!) element is simpler to remember and use.
arhh yes there is another bug in the program if you only enter minus number then you will get 0 back for max.

so you could either go

int min, max;
....
min = myarray[0];
max = myarray[1];


or just

int min(0x7FFFFFFF), max(0x80000000); //maxium for min so all else is lower and visa versa for max
Topic archived. No new replies allowed.