Array Problem in C

Started by
11 comments, last by alvaro 10 years ago

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! ohmy.png 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);
}

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

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 :/

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 :/


Perhaps you could give your program's output and your expected output.
what

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

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 :/


Perhaps you could give your program's output and your expected output.

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);
}
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.]

  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.


  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


#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;
}

This topic is closed to new replies.

Advertisement