# how to get largest number from the set

This is a discussion on how to get largest number from the set within the C Programming forums, part of the General Programming Boards category; I got the smallest number from the set the user inputs (i did this a couple days ago and basically ...

1. ## how to get largest number from the set

I got the smallest number from the set the user inputs (i did this a couple days ago and basically im lost) however i cannot figure out how to obtain the largest integer in the set any help would be appreciated
Code:
```#include<stdio.h>
#include<math.h>

int main()
{
int count, N;
double num, sum, average;
double small;
double large;

sum = 0.0;
average = 0.0;
count = 0;

printf("How many numbers are in your set?" );
scanf("%d",&N);
printf("Enter %d number(s) and <Enter> after each> ", N);

while (count < N)
{
scanf("%lf",&num);
sum = sum + num;
average = sum / (double) N;
count++;
}

small = num;

while (++count < N)
{
scanf("%lf",&num);
if (small < num)
small = num;
}

/* print the result */
printf("\nSmallest=%.2f", small);
printf("\nlargest=%.2f", large);
printf("\nAverage=%.2f", average);

system("pause");

return (0);

}```

2. Code:
`average = sum / (double) N;`
First i would have this out of the loop.I would have it at the next line that the loop ends,because the sum always changes,but you want the final value.

I will give you a simple example of how to find the max value of an array.Then you can modify your code inspired by this

Code:
```#include <stdio.h>

int main(void)
{
int array[5]={0,1,2,3,4};
int max;
int i;

max = array[0];
for( i = 1 ; i < 5 ; i++)/*start from second cell!!*/
{
if(max<array[i])
{
max = array[i];
}
}

printf("max = %d\n",max);
return 0;
}```
The idea is that the we store the first value of the array in variable max.Then we compare it with all the others values of the array.If we find a bigger value ,then we store this value in variable max.When the loop is terminated,variable max will hold the biggest value of the array

Popular pages Recent additions