:eek: Awesome, I had to define the count as static (since the value assigned to it would go away after the count occurances loop ended) and set it to 0 at the beginning of the outer loop.. but it works! /clapQuote:
Originally Posted by quzah
Printable View
:eek: Awesome, I had to define the count as static (since the value assigned to it would go away after the count occurances loop ended) and set it to 0 at the beginning of the outer loop.. but it works! /clapQuote:
Originally Posted by quzah
Thanks for everyone's help. Based on everyone's responses, I have some semi-working code.
This code gives me a new array with zeros in the unwanted slots.Code://Pass in 2 arrays of same size
//1 filled with random numbers other with zeros
int copies, new_size = 1;
*(array2 + 0) = *(array1 + 0);
for (int i = 1; i < size1; i++)
{
copies = 0; //Reset count to 0
for (int j = 0; j < size1; j++)
{
if (*(array1 + i) == *(array2 + j))
copies++;
}
if (copies < max_dups) //Max_dups is a constant
{
*(array2 + i) = *(array1 + i);
new_size++;
}
}
The thing I don't like about this code is that I would rather have the new array be dynamically allocated based on new_size.
Obviously this code wouldn't work if I wanted to include zeros in my set of random numbers.
I hope I am making sense to everyone and again thanks a bunch for the help.
kratz
This is what I was talking about with my last post. It doesn't matter if you have zero as a valid value in this array either.Code:#include <cstdio>
#define N 2
size_t countin( int n, int array[], size_t size )
{
size_t ret = 0;
for( size_t x = 0; x < size; x++ )
if( array[ x ] == n )
ret++;
return ret;
}
int main( void )
{
int array[] = { 1, 3, 4, 5, 2, 3, 4, 1, 3, 4, 4, 3, 4, 2 };
int newarray[ sizeof array / sizeof array[ 0 ] ] = {0};
size_t x, y;
for( y = x = 0; x < sizeof array / sizeof array[ 0 ]; x++ )
if( countin( array[ x ], array, y ) < N )
newarray[ y++ ] = array[ x ];
for( x = 0; x < y; x++ )
printf("newarray[ %d ] is %d\n", x, newarray[ x ] );
return 0;
}
/*
newarray[ 0 ] is 1
newarray[ 1 ] is 3
newarray[ 2 ] is 4
newarray[ 3 ] is 5
newarray[ 4 ] is 2
newarray[ 5 ] is 3
newarray[ 6 ] is 4
newarray[ 7 ] is 1
newarray[ 8 ] is 2
*/
Quzah.
I ended up getting sort of the same thing as kratz from quzah's comment:
I believe it will accept 0 as a valid value too...Code:int oldArray[] = { 19, 16, 17, 18, 17, 15, 16, 18, 17, 19, 20, 16, 15, 18, 15 };
int tempArray[sizeof oldArray / sizeof oldArray[0]];
int maxOfNum = 2;
int static countOfNum;
int i, j;
for (i = 0; i < 15; ++i) {
countOfNum = 0;
for (j = 0; j < 15; ++j) {
if (tempArray[j] == oldArray[i])
++countOfNum;
}
if (countOfNum < maxOfNum) {
tempArray[i] = oldArray[i];
cout << tempArray[i] << ", ";
}
}
If you want it the new array to be the exact size of how many are carried over, why not just make this array a temp array, then copy over the contents of the temp array to a fresh array after.
Using my code you could do this a few ways:
1) Use a for loop to set all the values of the tempArray to some value, ie. 666. Then after all of the code, add another for loop that counts how many of the values in tempArray != 666, that will be the size of your newArray. Then simply make a for loop (to loop amount: size of the newArray) to take the values from the tempArray and put them into the newArray.
2) You could add another static int count and that counts inside (countOfNum < maxOfNum), which will end up being the amount of valid values in your tempArray. Then create newArray using that value as the size, then use a for loop to copy from tempArray to newArray.
I would like to thank everyone that contributed. You guys have been a huge help.