0

This program gets 5 numbers only from the user then Store them in an array. Get the min, max, and the average of the numbers inputted. Here's the code I made:

#include <stdio.h>
#include <conio.h>

int main()
{
int num[5];
int min, max=0;
int counter;
float average, total;

max = num[0];
min = num[2];

for(counter=0; counter<=4; counter++)
{
    printf("Enter a number: ");
    scanf("%d", &num[counter]);

    if(num[counter]>max)
    {
        max = num[counter];
    }

    if (num[counter]<min)
    {
        min = num[counter];
    }
}

total = max+min;
average = total/2;

printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min); 
printf("The average is: %d", average);


getch();
return 0;
}

FInally fixed my error with the min and max and now I'm having trouble with the average. I should only get the average of the min and max numbers but it keeps on showing an average of zero. Can someone help? Thankkyouuu.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Barry
  • 59
  • 1
  • 5
  • possible duplicate of [Getting the min, max, and ave of the five numbers inputted](http://stackoverflow.com/questions/18668824/getting-the-min-max-and-ave-of-the-five-numbers-inputted) – ldav1s Sep 07 '13 at 03:15
  • Learn to use the idiomatic `for (counter = 0; counter < 5; counter++)` loop instead of using `counter <= 4` in the condition. – Jonathan Leffler Sep 07 '13 at 03:33
  • Also, you might note that you really don't need the array at all. You can simply read an indefinite number of values into a single variable, one at a time, and do the minimum, maximum and summing operations using that variable. Then you're not limited to a fixed size array. Of course, you'd have to handle errors and EOF from `scanf()` correctly, but you should be doing that anyway. – Jonathan Leffler Sep 07 '13 at 03:49
  • 2
    Stop destroying your questions after you're done with them. This is the third time you've done this today. – Blastfurnace Sep 07 '13 at 12:15

4 Answers4

1
  1. Your calculation of average is wrong; you need to use total/num (remember use float):

    total += num[counter];
    
  2. max and min were incorrectly initialized: num[0], num[2] may be anything when you initialize them.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
brooke
  • 11
  • 1
1
//get memory address and store value in it.
void getValue(int *ptr)
{
    printf("Enter a number: ");
    scanf("%d", ptr);
}
int main()
{
    //initialized n=5 as per your requirement. You can even get the value at run time.
    int min, max, counter,n=5;
    int num[n];
    float average,total;
    getValue(num); //get num[0]
    min=max=total=num[0]; //Initialize min,max,total with num[0]
    for(counter=1; counter<n; counter++)
    {
        getValue(num+counter); //get num[counter]
        num[counter]>max?max = num[counter]:max; //find max
        num[counter]<min?min = num[counter]:min; //find min
        total+=num[counter]; // total = total + num[counter]
    }
    average = total/n; 
    printf("The maximum number is: %d\n", max);
    printf("The minimum number is: %d\n", min); 
    printf("The total is: %f\n", total); 
    printf("The average is: %f\n", average);
return 0;
}
Amarnath Krishnan
  • 1,253
  • 1
  • 9
  • 12
  • @chux provide a test case for failure. The min and max are always initialized with the first element of the array. SO min and max cannot be zero unless the input value is zero. – Amarnath Krishnan Sep 16 '13 at 03:50
0

Aside from your calculation of average being wrong (it's not just total/2), you need to use the correct format specifier in the printf:

printf("The average is: %g", average);

You are using %d which tells printf to expect an integer, but you're giving it a float.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

1 Min and max should be initialized

int min = INT_MAX;
int max = INT_MIN;

2 You need to keep a running total of your numbers

int total = 0;
...
// inside loop
scanf("%d", &num[counter]);
total += num[counter];

3 At the end print the average, recommend going to floating point.

printf("The average is: %.1f", (double)total/counter);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256