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. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Unhappy 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. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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 subscribe to a feed

Similar Threads

  1. Largest Number
    By lukesowersby in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 06:53 AM
  2. 2 largest number,3 variables only
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2008, 08:13 AM
  3. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 08:56 AM
  4. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  5. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21