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 :)
#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...
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.
#include <iostream>
usingnamespace 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];
}
elseif(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
#include <iostream>
usingnamespace 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];
}
elseif(myarray[count]<=min)
{
min=myarray[count];
}
}
cout << "The maximum number is:" << max << endl;
cout << "The minimum number is:" << min << endl;
cin >> min;
}
#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];
}
elseif(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
#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];
}
elseif(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
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.