for loop help

This is a discussion on for loop help within the C Programming forums, part of the General Programming Boards category; I am having trouble doing this problem. It is: "write a program that asks the user to enter a list ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    25

    for loop help

    I am having trouble doing this problem. It is:
    "write a program that asks the user to enter a list of integers. The program will then determine the largest value and the number of times it was entered.
    -the user will first enter how many numbers the list contains.
    -use a for loop to find the maximum value and the number of times it was entered"


    This is what I have done. I cant seem to figure out how to count how many times the maximum value has been entered.

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    
    
    int main()
    {
    	
    	int num, num2, max;
    	int counter;
    	int counter2=0;
    
    	max = INT_MIN;  
    
    	printf ("Enter the number of elements in this list: ");
    	scanf("%d",&num);
    	printf ("Enter the elements in this list: \n");
    
    	for(counter=1;counter<=num;counter++)
    	{
    		
    		scanf ("%d",&num2);
    
    		if(num2=num2) 
    		{
    			counter2++;
    			
    		}
    		
    		if(num2>max) 
    		{
    			max = num2;
    		}
    		
    	
    	}
    
    
    	printf ("\nThe maximum number is %d and it was entered %d times\n",max, counter2);
    
    	
    	return 0;
    }

  2. #2
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Alright, so basically:
    If our inputted number (num2) is equal to max (remember, conditional equality is == not =), we increase counter2 by 1. If we have a new maximum, then we set counter2 to 1 (Because the previous value wasn't the real maximum, and we now have a new maximum, with one occurance).
    There's no real reason to initialize counter2 to 0 initially using this method, by the way.
    Last edited by Phenax; 03-04-2011 at 08:17 PM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    So from what you just said, I should have three if statements, something like this?

    if(num2>max)
    {
    max = num2;
    }
    if(max>num2)
    {
    counter2=1;
    }
    if(num2=max)
    {
    counter2++;
    }




    but in what order?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're over-thinking the problem (which is common, btw). Think of it this way:

    I hand you melons, one at a time. You need to pick out the largest melon size, and how many of them there are in that same size.

    How would you do that?
    Code:
    if(I give you a bigger size melon than the previous maximum size)
      You have a new maximum size.
      You have one of them
    end if.
    
    If(I give you another melon which is the maximum size)
      You have one more maximum size melon
    end if
    It's just that simple.

  5. #5
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by zfite View Post
    So from what you just said, I should have three if statements, something like this?

    but in what order?
    First of all,
    if(num2=max) is not the same as if(num2==max)!! The first one will set num2 to max, and then return max which evaluates to true (0 = false, not 0 = true in if statements). Completely different behavior.
    Also, you have too many if statements. You only need the if(num2==max) and your original. Why would you reset the counter when you find a number *smaller* than the max? That doesn't make sense. You reset the counter to 1 when you find a number *bigger* than the max.
    And logically, your if(num2==max) should be before the if(num2>max), because if it wasn't, you would get a new max, set the counter to 1, and then increase it by 1 (when we only have 1 occurance!), thus you'd go one over your actual answer.

    Adak's psuedocode is what I'm trying to express, although in this case, the if statement's order matters, and your order would be the opposite of his.
    Last edited by Phenax; 03-04-2011 at 09:01 PM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Thanks, I got it.

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