• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NUCLEAR RABBIT

Array Problem in C

12 posts in this topic

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

Edited by Josh Petrie
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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. Edited by incertia
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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);
}
Edited by NUCLEAR RABBIT
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

 

  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

0

Share this post


Link to post
Share on other sites
#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
0

Share this post


Link to post
Share on other sites
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. smile.png
0

Share this post


Link to post
Share on other sites

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. smile.png

 

 

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

0

Share this post


Link to post
Share on other sites

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).
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0