# 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; Originally Posted by slingerland3g Was interested in looking at this, but I do not see the point in this code: ...

1. Originally Posted by 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;```
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.

2. 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.

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.

3. 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. 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. <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.

6. 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.

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.

8. 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>.

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.

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.

11. 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;
}```

Page 2 of 2 First 12