Thread: Calculating the lowest and highest values

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Calculating the lowest and highest values

    What can I inject to make this program calculate the youngest entry and the oldest entry. So far it can calculate average and number of entries.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXARR 100
    void main (void)
    {
     int
    	mode[MAXARR], i, entry;
     int counter = 0;
     int total = 0;
     int total2 = 0;
     int mostfreq = 0;
     float average, stdev;
     printf("Student Age Calculator \n");
     printf("======================\n\n");
     printf("Input Student's Age:\n");
     scanf("%d",&entry);
     while (entry >= 0)
    	{
    	 total = total + entry;
    	 total2 = total2 + entry * entry;
    	 mode[entry]++;
    	 counter = counter +1;
    	 printf("Input the Student's age ; Negative number for Calculations: \n");
    	 scanf("%d",&entry);
    	}
     average = (float)total / (float)counter;
     for(i=0;i<100;i++)
    	{
    	 if (mode[i] > mostfreq);
    		{
    		 mostfreq = mode[i];
    		}
    	}
     printf("There were %d ages recorded. \n", counter);
     printf("The average age is: %6.2f \n", average);
     getchar();
     
     system("PAUSE");
    }
    Last edited by Charak; 01-30-2011 at 07:40 PM.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    in a loop
    Code:
    for( entry = 0 ; entry < MAX ; ++entry) 
    {
        if( mode[entry] >= largest) 
              largest = mode[entry]
        else if(mode[entry] < lowest )
               lowest = mode[entry];
    }
    Last edited by nimitzhunter; 01-30-2011 at 07:44 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    cleaned it up a bit, but having some issues:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXARR 100
    
    void main (void)
    {
     int mode[MAXARR], i, entry, lowest, largest;
     int counter = 0;
     int total = 0;
     
     float average, stdev;
     printf("Student Age Calculator \n");
     printf("======================\n\n");
     printf("Input Student's Age: ");
     scanf("%d",&entry);
     while (entry >= 0)
     
         {
    	 total = total + entry;
    	 mode[entry]++;
    	 counter = counter +1;
    	 printf("Input Student's age ; Negative number for Calculations: ");
    	 scanf("%d",&entry);
         }
         
         average = (float)total / (float)counter;
    
    for( entry = 0 ; entry < MAX ; ++entry) 
    {
        if( mode[entry] >= largest) 
              largest = mode[entry]
        else if(mode[entry] < lowest )
               lowest = mode[entry];
    }	
    
      printf("\nThere were %d ages recorded. \n", counter);
      printf("The average age is: %6.2f \n", average);
      printf("The Youngest age is: %d \n", lowest);
      printf("The Oldest age is: %d \n", largest);
    
     system("PAUSE");
    }
    32. syntax error before "else"
    6. return type of 'main' is not `int'
    28 MAX' undeclared (first use in this function)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    32. Statements still need semicolons after them, even inside if statements.
    6. Yes, the return type of main is supposed to be int. Make it so.
    28. You've got nothing called MAX. How many times do you want the loop to run?

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    it compiles but the number it generates isnt it

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXARR 100
    
    int main (void)
    {
     int mode[MAXARR], entry, lowest, largest;
     int counter = 0;
     int total = 0;
     
     float average, stdev;
     printf("Student Age Calculator \n");
     printf("======================\n\n");
     printf("Input Student's Age: ");
     scanf("%d",&entry);
     while (entry >= 0)
    
         {
    	 total = total + entry;
    	 mode[entry]++;
    	 counter = counter +1;
    	 printf("Input Student's Age ; Negative number for Calculations: ");
    	 scanf("%d",&entry);
         }
         
         average = (float)total / (float)counter;
         
         for( entry = 0 ; entry < 100 ; ++entry) 
         {
        if( mode[entry] >= largest) 
              largest = mode[entry];
        else if(mode[entry] < lowest )
               lowest = mode[entry];       
         }	
    
      printf("\nThere were %d ages recorded. \n", counter);
      printf("The Average age is: %.2f \n", average);
      printf("The Youngest age is: %d \n", lowest);
      printf("The Oldest age is: %d \n", largest);
    
     system("PAUSE");
    }
    For example:

    Input Student Age: 5
    Input Student Age: 10
    Input Student Age: 19
    Input Student Age: -1

    There were 3 ages recorded.
    The Average age is 11.33.
    The Youngest age is: -1779614522
    The Oldest age is: 1997798193
    press any key to continue...

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    New changes, everything works BUT the lowest entry output doesnt work, it keeps saying 0 when no 0 was inputted:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
     int mode[100], entry;
     int counter = 0;
     int total = 0;
     int largest = 0;
     int lowest = 0;
     
     float average, stdev;
     printf("Student Age Calculator ; -1 for Calculations \n");
     printf("============================================\n\n");
     printf("Input Student's Age: ");
     scanf("%d", &entry);
     while (entry >= 0)
    
         {
    	 total = total + entry;
    	 mode[100] = entry;
    	 counter = counter +1;
    	 printf("Input Student's Age: ");
    	 scanf("%d", &entry);
         }
         
         average = (float)total / (float)counter;
         
         for( entry = 0 ; entry < 100 ; ++entry) 
         {
        if( mode[100] >= largest) 
              largest = mode[100];
        else if(mode[100] <= lowest )
               lowest = mode[100];       
         }	
    
      printf("\nThere were %d ages recorded. \n", counter);
      printf("The Average age is: %.2f \n", average);
      printf("The Oldest age is: %d \n", largest);
      printf("The Youngest age is: %d \n", lowest);
    
     system("PAUSE");
     return 0;
    }

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Because you initialized it to zero. Change the initialization of lowest to something large.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    still unable to get lowest/largest to work

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    {
     int mode[100], entry;
     int counter = 0;
     int total = 0;
     int largest = 0;
     int lowest = 100;
     
     float average, stdev;
     printf("Student Age Calculator ; -1 for Calculations \n");
     printf("============================================\n\n");
     printf("Input Student's Age: ");
     scanf("%d", &entry);
     while (entry >= 0)
    
         {
    	 total = total + entry;
    	 mode[counter] = entry;
    	 counter = counter +1;
    	 printf("Input Student's Age: ");
    	 scanf("%d", &entry);
         }
         
         average = (float)total / (float)counter;
         
         for( entry = 0 ; entry < 100 ; ++entry) 
              {
               if( mode[entry] >= largest)
               largest = mode[entry];
               
               else if(mode[entry] < lowest)
               lowest = mode[entry];
               }
    	
    
      printf("\nThere were %d ages recorded. \n", counter);
      printf("The Average age is: %.2f \n", average);
      printf("The Oldest age is: %d \n", largest);
      printf("The Youngest age is: %d \n", lowest);
    
     system("PAUSE");
     return 0;
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    entry < 100
    Psychic? How do you know they entered 100 numbers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  2. test scores(avg, highest, lowest...)
    By taj777 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2010, 09:23 PM
  3. Replies: 22
    Last Post: 11-13-2009, 05:51 PM
  4. HELP: Exclude the highest and lowest number...
    By Ocin101 in forum C Programming
    Replies: 12
    Last Post: 07-21-2009, 01:51 AM
  5. Displaying highest and lowest values
    By beastofhonor in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2006, 08:24 PM