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

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-27-2009
pomocnik89
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.
• 10-27-2009
quzah
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.
• 10-27-2009
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.
• 10-28-2009
nonoob
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.
• 10-29-2009
iMalc
"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.
• 10-29-2009
slingerland3g
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.
• 10-29-2009
Kennedy
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.
• 10-29-2009
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.
• 10-29-2009
MisterIO
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"); }```
• 10-29-2009
quzah
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.
• 10-30-2009
MisterIO
Quote:

Originally Posted by quzah
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?
• 10-30-2009
slingerland3g
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.
• 10-30-2009
MisterIO
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.
• 10-30-2009
MisterIO
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; }```
• 10-30-2009
slingerland3g
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;```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last