Finding maximum and the number to times entered without using arrays

This is a discussion on Finding maximum and the number to times entered without using arrays within the C Programming forums, part of the General Programming Boards category; I am trying to find the maximum number using a loop and count the number to times the max was ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    40

    Finding maximum and the number to times entered without using arrays

    I am trying to find the maximum number using a loop and count the number to times the max was entered. I am having trouble with finding and checking for the maximum and counting the number of times it has been entered. Can someone please give me some advice.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int max;
        int i;
        int current;
        int numbers;
        int count = 0;
        
        printf("\nHow many numbers?: ");
        scanf("%d", &numbers);
        
        for (i = 0; i < numbers; i++)
        {
              scanf("%d", current);
              if (max > current)
              {
                 max = current;
                 count++;
              }
        }
         
        printf("Max: %d     Number of times: %d\n", max, count);  
        system("PAUSE");	
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Your counting seems to be wrong. Every time you discover a new current maximum, you should reset the count, not increment it. But every time the current number is equal to the current maximum, you should increment the count.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    I did what you said but I still cant seem to get the program to work. Can you please tell me what is wrong with my program.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int max;
        int current_max;
        int current;
        int i;
        int numbers;
        int count = 0;
        
        printf("\nHow many numbers?: ");
        scanf("%d", &numbers);
        
        for (i = 0; i < numbers; i++)
        {
              scanf("%d", &current);
              if (current_max > current)
              {
                 max = current_max;
                 count = 1;
                 if (current == current_max)
                    count++;
              }
        }
         
        printf("Max: %d     Number of times: %d\n", max, count);  
        system("PAUSE");	
        return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    By current maximum I meant the max variable in your first code.

    Note that current_max > current and current == current_max is a contradiction, so you should be moving the comparison of equality to its own branch.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    I still cant get the program to work. Can you please show me what you mean.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    I mean something like this:
    Code:
    for (i = 0; i < numbers; i++)
    {
        scanf("%d", &current);
        if (max > current)
        {
            max = current
            count = 1;
        }
        else if (max == current)
        {
            count++;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You should check if current > current_max, not current_max > current rfight?

    Code:
    		  if (current > current_max)
    		  {
    			  current_max = current;
    			  count = 1;
    		  }
    		  else if (current == current_max)
    			  count++;

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    thank you for your advice the program is starting to make sense now but i still cannot get it to work. Could you please take a closer look at it.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int max;
        int current;
        int i;
        int numbers;
        int count = 0;
        
        printf("\nHow many numbers?: ");
        scanf("%d", &numbers);
        
        for (i = 0; i < numbers; i++)
        {
              scanf("%d", &current);
              if (max > current)
              {
                 max = current;
                 count = 1;
              }
              else if (max == current)
              {
                   count++;
              }     
        }
         
        printf("Max: %d     Number of times: %d\n", max, count);  
        system("PAUSE");	
        return 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Oh, and one more thing: I notice that max was never initialised. You should either initialise it to the first input entered by the user, or to the maximum value for int (i.e., #include <limits.h> and use INT_MAX).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    40
    never mind i got it to work. thank you for everything i appreciate everything

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    No problem, but if you ever do decide to change and use INT_MAX, note that I mixed it up: it should be INT_MIN instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

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