• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Array Problem in C

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

#1NUCLEAR RABBIT  Members

Posted 30 March 2014 - 07:43 PM

Hello, so I am trying to write a program that creates an array with random numbers and then making a new array with the same values but with no duplicates. When I run the program, the results are not what they should come out being and I'm not too sure why this his happening! If anyone could shed some light on my brain it would be greatly appreciated!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void binarySearch(int rand_nums[], int size);
void printArray(int nums[], int size);

int main(int argc, const char * argv[])
{
srand((unsigned)time(NULL));

int rand_nums[20];
int rand_num_size = sizeof(rand_nums)/sizeof(rand_nums[0]);

// fills array with random nums 0-10
for(int i = 0; i < rand_num_size; i++)
rand_nums[i] = rand() % 10;

binarySearch(rand_nums, rand_num_size);

return 0;
}

void printArray(int nums[], int size) {
printf("Array set: ");
for(int i = 0; i < size; i++)
printf("%d ", nums[i]);
}

void binarySearch(int rand_nums[], int size) {
int counter = 0;

// goes through array and makes duplicates NULL
for(int i = 0; i < size; i++) {
for(int j = i + 1; j < size; j++) {
if(rand_nums[i] == rand_nums[j] && rand_nums[j] != (int)NULL && rand_nums[i] != (int)NULL) {
rand_nums[j] = (int)NULL;
counter++;
}
}
}

// new array with custome size
int new_array[size-counter];
size_t new_array_size = sizeof(new_array)/sizeof(new_array[0]);

int index = 0;

// fills new array with non NULL values of rand_nums
for(int g = 0; g < new_array_size; g++) {
if(rand_nums[g] != (int)NULL) {
new_array[index] = rand_nums[g];
index++;
}
}

// victory
printf("OLD Array:\n");
printArray(rand_nums, size);
printf("\n\nNEW Array:\n");
printArray(new_array, new_array_size);
}



Edited by Josh Petrie, 01 April 2014 - 12:38 PM.

------------------------My band: RISE OVER ME!

#2Álvaro  Members

Posted 30 March 2014 - 07:56 PM

The first obvious problem with the code is that you can't set an int to NULL, or at least it probably won't do what you want. NULL is a fancy name for zero, and (int)NULL will just turn into 0, which is one of the normal values that your random numbers might have.

#3NUCLEAR RABBIT  Members

Posted 30 March 2014 - 08:04 PM

The first obvious problem with the code is that you can't set an int to NULL, or at least it probably won't do what you want. NULL is a fancy name for zero, and (int)NULL will just turn into 0, which is one of the normal values that your random numbers might have.

i changed all references to NULL and made them a random number outside the scope I made the random numbers (999), but the same problem is still occuring :/

------------------------My band: RISE OVER ME!

#4incertia  Members

Posted 30 March 2014 - 08:08 PM

You are quite ambiguous with the results don't really come out to what they should be.
This code will never put the value 0 into the new array because NULL evaluates to 0 and you only add non-zero values. C/C++ is not like Java in which null refers to nothing. In C/C++, NULL evaluates to 0 and you set pointers to NULL or nullptr because accessing memory at location 0 is guaranteed to crash your program.

Setting the ones you don't want to -1 would work better because rand() % 10 will always be positive because rand() is always positive.

i changed all references to NULL and made them a random number outside the scope I made the random numbers (999), but the same problem is still occuring :/

Edited by incertia, 30 March 2014 - 08:09 PM.

what

#5NUCLEAR RABBIT  Members

Posted 30 March 2014 - 08:08 PM

I'm 99.9% sure the problem is happening where the variable counter is increased, but can't think of what's causing it

------------------------My band: RISE OVER ME!

#6NUCLEAR RABBIT  Members

Posted 30 March 2014 - 08:11 PM

You are quite ambiguous with the results don't really come out to what they should be.
This code will never put the value 0 into the new array because NULL evaluates to 0 and you only add non-zero values. C/C++ is not like Java in which null refers to nothing. In C/C++, NULL evaluates to 0 and you set pointers to NULL or nullptr because accessing memory at location 0 is guaranteed to crash your program.

Setting the ones you don't want to -1 would work better because rand() % 10 will always be positive because rand() is always positive.

i changed all references to NULL and made them a random number outside the scope I made the random numbers (999), but the same problem is still occuring :/

Oh okay I see, I was not aware of that, thank you! Well what I mean is that, some of the values in the new array are the same as the original array, but some of the values are not even in the original array to begin with. The size of the new array is also not the correct size it should be, which I believe is on account of the counter variable be incremented when it shouldn't be

updated code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void binarySearch(int rand_nums[], int size);
void printArray(int nums[], int size);

int main(int argc, const char * argv[])
{
srand((unsigned)time(NULL));

int rand_nums[20];
int rand_num_size = sizeof(rand_nums)/sizeof(rand_nums[0]);

// fills array with random nums 0-10
for(int i = 0; i < rand_num_size; i++)
rand_nums[i] = rand() % 10;

binarySearch(rand_nums, rand_num_size);

return 0;
}

void printArray(int nums[], int size) {
printf("Array set: ");
for(int i = 0; i < size; i++)
printf("%d ", nums[i]);
}

void binarySearch(int rand_nums[], int size) {
int counter = 0;

printf("OLD Array:\n");
printArray(rand_nums, size);

// goes through array and makes duplicates NULL
for(int i = 0; i < size; i++) {
for(int j = i+1; j < size; j++) {
if(rand_nums[i] == rand_nums[j] && rand_nums[j] != 999 && rand_nums[i] != 999) {
rand_nums[j] = 999;
counter++;
printf("\ncounter = %d", counter);
}
}
}

// new array with custome size
int new_array[size-counter];
int new_array_size = (int)sizeof(new_array)/sizeof(new_array[0]);

int index = 0;

// fills new array with non NULL values of rand_nums
for(int g = 0; g < new_array_size; g++) {
if(rand_nums[g] != 999) {
new_array[index] = rand_nums[g];
index++;
}
}

// victory
printf("\n\nNEW Array:\n");
printArray(new_array, new_array_size);
}


Edited by NUCLEAR RABBIT, 30 March 2014 - 08:12 PM.

------------------------My band: RISE OVER ME!

#7Álvaro  Members

Posted 30 March 2014 - 08:13 PM

You need to ask questions better. At this point, we don't know exactly what code we are talking about, and you have never described what you mean when you say the results are not what they should be: What are they? What should they be?

[EDIT: OK, you addressed my concerns just as I was posting this.]

Edited by Álvaro, 30 March 2014 - 08:14 PM.

#8Álvaro  Members

Posted 30 March 2014 - 08:19 PM

  for(int g = 0; g < new_array_size; g++) {

Should be
  for(int g = 0; g < size; g++) {
EDIT: Why is that function called binarySearch'? I can only imagine that you are in the early stages of putting together some code and this is just some test, and not really what you intend to do. But giving a function a name unrelated to what it does is extremely confusing and I wouldn't even do it during tests. I certainly wouldn't post it to a forum.

Edited by Álvaro, 30 March 2014 - 08:21 PM.

#9NUCLEAR RABBIT  Members

Posted 30 March 2014 - 08:32 PM

  for(int g = 0; g < new_array_size; g++) {

Should be
  for(int g = 0; g < size; g++) {
EDIT: Why is that function called binarySearch'? I can only imagine that you are in the early stages of putting together some code and this is just some test, and not really what you intend to do. But giving a function a name unrelated to what it does is extremely confusing and I wouldn't even do it during tests. I certainly wouldn't post it to a forum.

Wow thank you!! Yes you're right it is pretty confusing, very sorry for any inconvinience

------------------------My band: RISE OVER ME!

#10georger.araujo  Members

Posted 30 March 2014 - 09:06 PM

#include <stdio.h>  /* printf */
#include <stdlib.h> /* rand */
#include <string.h> /* memset */
#include <time.h>   /* time */

#define SIZE 20

int main( int argc, const char * argv[] )
{
srand( ( unsigned )time( NULL ) );

int rand_nums[SIZE];

// fills array with random nums 0-10
for ( int i = 0; i != SIZE; i++ )
rand_nums[i] = rand() % 10;

int unique_rand_nums[SIZE];
memset( unique_rand_nums, -1, sizeof unique_rand_nums );

int counter, dupe;
counter = dupe = 0;
for ( int i = 0; i != SIZE; i++ )
{
for ( int j = 0; j != SIZE; j++ )
if ( rand_nums[i] == unique_rand_nums[j] )
dupe = 1;

if ( !dupe )
unique_rand_nums[counter++] = rand_nums[i];

dupe = 0;
}

for ( int i = 0; i != SIZE; ++i )
printf( "%d ", rand_nums[i] );

printf( "\n" );

for ( int i = 0; i != counter; ++i )
printf( "%d ", unique_rand_nums[i] );

return 0;
}


Edited by georger.araujo, 30 March 2014 - 09:10 PM.

#11Álvaro  Members

Posted 30 March 2014 - 09:26 PM

If you are OK with destroying the original array, you can even do this in place.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int const size = 20;

int main(void) {
srand((unsigned)time(NULL));

int rand_nums[size];

for (int i = 0; i < size; ++i)
rand_nums[i] = rand() % 10;

for (int i = 0; i < size; ++i)
printf("%d ", rand_nums[i]);
puts("");

int n_unique = 0;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < n_unique; ++j) {
if (rand_nums[i] == rand_nums[j])
goto NOT_UNIQUE;
}
rand_nums[n_unique++] = rand_nums[i];
NOT_UNIQUE:;
}

for (int i = 0; i < n_unique; ++i)
printf("%d ", rand_nums[i]);
puts("");

return 0;
}

I won't apologize for the GOTO.

#12georger.araujo  Members

Posted 30 March 2014 - 10:15 PM

If you are OK with destroying the original array, you can even do this in place.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int const size = 20;

int main(void) {
srand((unsigned)time(NULL));

int rand_nums[size];

for (int i = 0; i < size; ++i)
rand_nums[i] = rand() % 10;

for (int i = 0; i < size; ++i)
printf("%d ", rand_nums[i]);
puts("");

int n_unique = 0;
for (int i = 0; i < size; ++i) {
for (int j = 0; j < n_unique; ++j) {
if (rand_nums[i] == rand_nums[j])
goto NOT_UNIQUE;
}
rand_nums[n_unique++] = rand_nums[i];
NOT_UNIQUE:;
}

for (int i = 0; i < n_unique; ++i)
printf("%d ", rand_nums[i]);
puts("");

return 0;
}

I won't apologize for the GOTO.

Clever!

I understood that @op had a hard requirement of using C, so even though int const size = 20; was the right thing to do in C++, I went for #define. This exercise showed me my C has become rusty, and C++ has spoiled me...

#13Álvaro  Members

Posted 31 March 2014 - 06:06 AM

I understood that @op had a hard requirement of using C, so even though int const size = 20; was the right thing to do in C++, I went for #define. This exercise showed me my C has become rusty, and C++ has spoiled me...

Perhaps using the macro is more idiomatic in C, and using `const' is not as good a substitute as it is in C++. The reason this works in C99 is because a local array can have a size specified by a variable (a feature that the OP was already using). If I were to make the array global, it wouldn't have worked (but in C++ this would have been fine).

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.