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
    23,616
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,616
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,616
    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++;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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++;

  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
    23,616
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    23,616
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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