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

This is a discussion on I don't know how many times a number is repeated in an array! within the C Programming forums, part of the General Programming Boards category; Please can somebody help me? I made a program that creates an array of 100 random numbers... I know the ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

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

    Please can somebody help me? I made a program that creates an array of 100 random numbers... I know the maximum and the minimum, but I need to know how many times maximum (minimum) is repeated in the array. I have tried everything, nothing has worked. This is my "program" :
    Code:
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int i,c;
    int array[99];
    int min=array[0], max=array[0];
    
    srand (time(NULL)); 
    for (i=0; i<=99;i++)
        {
        array[i]=rand()%1000;
        printf("array[%d] is %d\n",i,array[i]);
        if(array[i]>max) max=array[i];
        if(array[i]<min) min=array[i];
        }
    printf("\n\nMaximum is %d\n",max);
    printf("Minimum is %d\n",min);
    
    system("pause");
    }
    Thank You very much for your help!
    S.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for each element in the array
        add a number
    for each element in the array
        check to see if it's the highest or lowest, and keep track of it as such
    for each element in the array
        see if it is what you have listed as highest or lowest, and increment a counter

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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you are scanning through the array, you have a lownum, and a hinum, and those values change.

    Whenever one of them changes, set the xlow or xhi counter, to 1. Then increment one of these counters whenever you have that same low or hi number in the array.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    First off, you need to declare the array as array[100] since your range is 0 to 99.

    Then you need to properly initialize min and max... I would suggest min = huge number, and max = 0.

    You said you wanted to know how many times the minimum and maximum values occurred. You haven't yet counted them. Once you determine the min and max, rescan the array and count the ones that match these two limits.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    "I have tried everything, nothing has worked." Don't give us a gross exaggeration of what you have tried, show us what you have tried!

    Note that you can't easily generate the numbers in the same loop that is trying to count the mix and max values because the min and max will change duringf the loop, so your counts qould become invalid.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Anytime you are scanning through an array of numbers, especially huge arrays, please sort them first then scan through them. Then the rest of the logic should just fall in to place as far as counting your lows and highs.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by pomocnik89
    Code:
    int min=array[0], max=array[0];
    This is a great idea. Works every time (then you don't have to worry about coming up with the min/max values as you have _REAL_ values to work with). HOWEVER, what you did here is wrong. You set min/max to array[0] THEN you assign array[0]. This needs to be in your loop but only valid for the first loop.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you get the counters added to your program for min and max, yet?

    Sorting the array is fine, but it's not necessary for what you want to do.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main(void)
    {
    	int i,c;
    	int array[99];
    	int min=array[0], max=array[0]; (BUG)
    
    	srand (time(NULL)); 
    	for (i=0; i<=99;i++) { (BUG)
    		array[i]=rand()%1000;
    		printf("array[%d] is %d\n",i,array[i]);
    		if(array[i]>max) max=array[i];
    		if(array[i]<min) min=array[i];
    	} (You're not counting in the loop, so how are you supposed to find what you're searching for?)
    	printf("\n\nMaximum is %d\n",max);
    	printf("Minimum is %d\n",min);
    
    	system("pause");
    }
    Last edited by MisterIO; 10-29-2009 at 05:48 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int min=array[0], max=array[0]; (BUG)
    Bug, probably, but not invalid code. You just don't have any idea of what the value assigned to min and max is actually going to be.
    Code:
    } (You're not counting in the loop, so how are you supposed to find what you're searching for?)
    They aren't actually searching for anything. They are just keeping track of the minimum and maximum, which if they had set them to a known (correct) value at the start, would function fine. All they need to do is add in a counter there if it actually equals the existing value.


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

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by quzah View Post
    Code:
    int min=array[0], max=array[0]; (BUG)
    Bug, probably, but not invalid code. You just don't have any idea of what the value assigned to min and max is actually going to be.
    Code:
    } (You're not counting in the loop, so how are you supposed to find what you're searching for?)
    They aren't actually searching for anything. They are just keeping track of the minimum and maximum, which if they had set them to a known (correct) value at the start, would function fine. All they need to do is add in a counter there if it actually equals the existing value.


    Quzah.
    The instructions where he reads from an uninitialized memory cell is a bug, period. First of all, I'm sure that's not what he wanted to do. Second, it may happen that the min value is never decided by the loop, because in the loop the min variable can get only positive values, while in the buggy initialization it can be any value. To understand that though, you need to always remember about the buggy initialization. IMO it just helps you in adding more bugs. At the very least, you need to comment that particular behaviour very clearly and state why couldn't you do any better than that.

    About the second part, what do you mean he wasn't searching for anything? He said he wanted to find how many times the min and max occurred, so he was searching for them. Searching as in the general meaning for it, not as in the binary(or something)- search. If he doesn't count them, how is he supposed to find that?
    Last edited by MisterIO; 10-30-2009 at 10:01 AM.

  12. #12
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I am not sure this was made clear, but what I understand is that the OP needs to count how many of the highest (max) number there are in the randomized list. The same goes for the lowest number found. The OP is not needing to count how many max and min changes have occurred. There is a big difference there.

    I still think sorting the array would be ideal here, though you can accomplish this task 100's of ways.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Try 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;
    	int cnt_min = 1, cnt_max = 1;
    
    	srand(time(NULL)); 
    	
    	do {
    		min = rand() % MAX_VALUE;
    		max = rand() % MAX_VALUE;
    	} while (min >= max);
    
    	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;
    }
    I coded it in 5 minutes so I didn't check it throughly. It may be buggy and not have considered all the corner cases. I'll check it better when I've got some more time.
    Last edited by MisterIO; 10-30-2009 at 10:31 AM.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    First bug spotted: min and max could reflect outside of array values.

    Let's try with 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;
    	int cnt_min = 1, cnt_max = 1;
    
    	srand (time(NULL)); 
    	
    	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]);
    	for (int i = 2; 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;
    }
    Last edited by MisterIO; 10-30-2009 at 10:38 AM.

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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;
    Last edited by slingerland3g; 10-30-2009 at 11:42 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  2. printing number from array question..
    By transgalactic2 in forum C Programming
    Replies: 41
    Last Post: 12-25-2008, 03: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

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