Thread: how to get largest number from the set

  1. #1
    Registered User
    Join Date
    Sep 2012

    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
    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?" );
        printf("Enter %d number(s) and <Enter> after each> ", N);
        while (count < N)
            sum = sum + num;
            average = sum / (double) N;
        small = num;
        while (++count < N)
            if (small < num)
            small = num;
        /* print the result */
         printf("\nSmallest=%.2f", small);
    	 printf("\nlargest=%.2f", large);
    	printf("\nAverage=%.2f", average);
        return (0);

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Nice, France
    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

    #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!!*/
    			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, 05:53 AM
  2. 2 largest number,3 variables only
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2008, 07:13 AM
  3. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07: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