Thread: I don't know how many times a number is repeated in an array!

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by slingerland3g View Post
    Was interested in looking at this, but I do not see the point in this code:

    Code:
    do {
    		min = rand() % MAX_VALUE;
    		max = rand() % MAX_VALUE;
    	} while (min >= max);
    	
    	array[0] = min;
    	array[1] = max;
    	printf("array[0] is %d\n", array[0]);
    	printf("array[1] is %d\n", array[1]);
    I can only predict strange results with that seen...

    Simply do:

    Code:
    #define NEW_MAX 1
    #define NEW_MIN 1
    ...
    int min = MAX_VALUE;
    int max = 0;
    int i;
    ...
    for (i = 0; i < ARR_LEN; i++) {
    ...
    
    /*For easier code documentation */
    
    ...
    cnt_max = NEW_MAX;
    ...
    cnt_min = NEW_MIN;
    This part:
    int min = MAX_VALUE;
    int max = 0;
    is correct and cleaner, but mine didn't have any problem. (the probability that it goes on forever would be ~0 for true random numbers and I suspect that for pseudo-random numbers it's even lower).(but yes, it seems useless, I don't remember why I thought it was needed)


    This part:
    cnt_max = NEW_MAX;
    ...
    cnt_min = NEW_MIN;
    is either semantically incorrect(that is, you didn't understand what I did) or it's useless and you chose some very bad names for the defines.
    Last edited by MisterIO; 10-30-2009 at 01:34 PM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by slingerland3g
    I still think sorting the array would be ideal here, though you can accomplish this task 100's of ways.
    In terms of complexity, sorting is not ideal. A linear time solution is more efficient than the application of some O(n log n) comparison based sorting algorithm in the long run.

    Quote Originally Posted by MisterIO
    This part:
    int min = MAX_VALUE;
    int max = 0;
    is correct and cleaner, but mine didn't have any problem. (the probability that it goes on forever would be ~0 for true random numbers and I suspect that for pseudo-random numbers it's even lower).
    I would prefer just setting min to INT_MAX and max to INT_MIN. Alternatively, if the array is populated beforehand, we assign the value of the first element of the array to min and max, then begin looping from the second element.
    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. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    With 1 suggestion accepted and 1 rejected, the code is now this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX_VALUE 1000
    #define ARR_LEN 99
    
    int main(void)
    {
    	int array[ARR_LEN];
    	int min = MAX_VALUE, max = 0;
    	int cnt_min = 0, cnt_max = 0;
    
    	srand (time(NULL)); 
    
    	for (int i = 0; i < ARR_LEN; ++i) {
    		array[i]=rand() % MAX_VALUE;
    		printf("array[%d] is %d\n",i,array[i]);
    		if (array[i] == min) {
    			++cnt_min;
    		} else if (array[i] == max) {
    			++cnt_max;
    		} else if (array[i] > max) {
    			max = array[i];
    			cnt_max = 1;
    		} else if (array[i] < min) {
    			min = array[i];
    			cnt_min = 1;
    		}
    	}
    	printf("\n\nMaximum is %d\n", max);
    	printf("Minimum is %d\n", min);
    	printf("cnt_min = %d\n", cnt_min);
    	printf("cnt_max = %d\n", cnt_max);
    
    	return 0;
    }

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Ah, by the way, I just remembered why I thought that the do {} while () loop was needed, it's because at that moment I was thinking more in general, not thinking about the %1000.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    <limits.h> has what you both are looking for; definitions of the minimum and maximum int values. Set the minimum to INT_MAX and the maximum to INT_MIN and be done with it.

    There's a reason we all didn't just whip up the code for them in the first reply. It's not because we couldn't. It's because we wanted them to work it out for themselves. Now that you've gone and ruined it, we all may as well throw our hat into the ring.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #include<limits.h>
    
    #define SIZE 100
    #define RANDCAP 100
    
    int main( void )
    {
        int array[ SIZE ] = {0},
            min = INT_MAX,
            max = INT_MIN,
            mincount = 0,
            maxcount = 0;
        size_t x = 0;
    
        srand( time( NULL ) );
    
        for( x = 0; x < SIZE; x++ )
        {
            array[ x ] = rand( ) % RANDCAP;
            if( array[ x ] < min )
            {
                min = array[ x ];
                mincount = 0;
            }
            if( array[ x ] > max )
            {
                max = array[ x ];
                maxcount = 0;
            }
            if( array[ x ] == min )
            {
                mincount++;
            }
            if( array[ x ] == max )
            {
                maxcount++;
            }
        }
        printf( "min: %d, count: %d, max: %d, count: %d\n", min, mincount, max, maxcount );
    
        return 0;
    }
    There you go, all in a single loop.

    Edit: Looks like you replied before I did. :P


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't need to use INT_MAX. Just set min and max to the first value you have in the array. Then adjust max and min as needed, as you go scan through the array, and reset their respective counter, to 1 when a new max or min is found.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    You don't need to use INT_MAX. Just set min and max to the first value you have in the array. Then adjust max and min as needed, as you go scan through the array, and reset their respective counter, to 1 when a new max or min is found.
    That's more involved than simply initializing your variables. You either change the initializer on your loop, or you add another if check. Either way, it seems easier to just initialize the variables in the first place.

    But yes, there are lots of ways to do pretty much everything. That's what makes it fun.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    With my idea, you have one line of code to add

    Code:
    if (x == 0)  min= max = array[x]; //or simply if(!x)...
    That shrinks the compilable lines of code in the program, from 589, to 542, by eliminating <limits.h>.

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Adak View Post
    With my idea, you have one line of code to add

    Code:
    if (x == 0)  min= max = array[x]; //or simply if(!x)...
    That shrinks the compilable lines of code in the program, from 589, to 542, by eliminating <limits.h>.
    You've added a check that will be true only once for the whole loop. I don't like it.

    @quizah: I knew about limits, but I don't like it either. IMO that should be used if there isn't any other way to do it. But that's just me, I'm not saying it's not correct.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    With my idea, you have one line of code to add

    Code:
    if (x == 0)  min= max = array[x]; //or simply if(!x)...
    That shrinks the compilable lines of code in the program, from 589, to 542, by eliminating <limits.h>.
    It may shrink the code, but it adds another check every time you run through the loop. Also, "shrinking" the code doesn't make it look more elegant in my opinion. I can understand if you're doing it for the sake of making it look slick, but I don't feel it does anything nifty, and it just clutters the loop even further.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    The internal loop could also be decomposed like this, for the very remote possibility of an array full of equal values:
    Code:
    if (array[i] == min) {
    	++cnt_min;
    } else if (array[i] < min) {
    	min = array[i];
    	cnt_min = 1;
    }
    		
    if (array[i] == max) {
    	++cnt_max;
    } else if (array[i] > max) {
    	max = array[i];
    	cnt_max = 1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. printing number from array question..
    By transgalactic2 in forum C Programming
    Replies: 41
    Last Post: 12-25-2008, 04:04 PM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM