1

I'm trying to create a program which randomly decides how many cards you have, then randomly allocates a value to each of those cards. I have managed to randomise the amount of cards, and I know how to randomise their values using an array and a for loop, but the problem is that this method only works when I manually choose a value for the number of elements in the array, but I want the number of elements to be the random amount of cards. How do I go about this? Here's my code so far to show what I mean. And yes, I'm aware the code probably could be done better but this is my first C assignment and I'm a complete beginner. Thanks :)

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

int main(void)
{
    system("cls"); /* Clears output to start */

    srand(time(NULL)); /* Sets seed for random number generator */

    int player1_amount = rand() %9 + 2; /*Generates random number for player 1's amount of cards */
    int player2_amount = rand() %9 + 2; /*Generates random number for player 2's amount of cards */

    int a = 1; /* For loop purposes */
    while(a <= 1) /* While loop to print out each player's amount of cards once */
    {
        printf("Player 1 you have %d cards! \n", player1_amount);  
        Sleep(500);
        printf("Player 2 you have %d cards! \n", player2_amount);  
        a++; 
    }    

    Sleep(1000); /* Delays for 1 second before printing card values */

    int values[3]; /* Creates an array with 3 elements, but I want the number of elements to be player1_amount from above */
    int b; /* Random variable for the loop */
    int size = sizeof(values) / sizeof(values[0]); /* Gets length of array */

    for (b = 0; b < size; b++) /* For loop randomises 3 values and then stops */
    {
        values[b] = rand() % 10 +1;
    }

    printf("Player 1 your cards are"); /* For loop to print out the values one after the other */
    for(b = 0; b < size; b++)
    {
        printf(" %d, ", values[b]);
    }

    getch();
    return 0;
}
12
  • 3
    Read and learn about dynamic memory allocation. Or just use a fixed array with maximum number of cards.
    – Eugene Sh.
    Commented Dec 22, 2017 at 15:57
  • 1
    int values[player1_amount]; will do the job. It is a variable length array.
    – mch
    Commented Dec 22, 2017 at 15:59
  • @mch that doesn't work it was the first thing i tried but it returns an error
    – MendelumS
    Commented Dec 22, 2017 at 16:01
  • 1
    If you weren't stuck on Windows, you'd have more options available. Since MSVS is a C90 compiler, you can't use VLA (variable length array) notation, which would be ideal for this. Commented Dec 22, 2017 at 16:27
  • 1
    @JonathanLeffler you've solved it lol :). I'm supposed to submit this on an online compiler my lecturer made, but I preferred VS which is why I was using it. I just tried using the VLA on the online compiler, and it does what I want :)
    – MendelumS
    Commented Dec 22, 2017 at 16:37

2 Answers 2

3

I believe you will want to use malloc or calloc for that with a pointer.

int *values = (int *)calloc(player1_amount, sizeof(int));

Just make sure you free your allocation when done:

free(values);
3
  • Can you provide some more info on this? I saw mention of malloc while researching but I couldn't figure out how to use it
    – MendelumS
    Commented Dec 22, 2017 at 16:08
  • 2
    @Sam: you need to get your text book out and read up on how to use malloc(). It is medium grade C; not the first thing you learn, but still a part of the basics of C. There are tutorials aplenty online too, I'm sure. Commented Dec 22, 2017 at 16:26
  • The links in there have some example code that you can look at. They are similar to the array declarations. int values[3] = {0,0,0}; works almost the same as int *values = (int*) calloc(3, sizeof(int)); In the end, you can still do values[0] = 5; if you like. Its "custom array allocation"...but since its custom, you are responsible for cleaning it up (de-allocating). Commented Dec 22, 2017 at 16:36
0

C allows you to declare variable sized array. If you are not interested in using functions like malloc or calloc you can simply use variable to declare array as I've done here :

#include <stdio.h>
void main()
{
      int x;

      printf("\nEnter the value of x : ");
      scanf("%d" , &x);

      int array[x];

      for(i = 0 ; i < x ; i++)
      {
              printf("Enter the element : ");
              scanf("%d" , &array[i]);
      }

      for(i = 0 ; i < x ; i++)
      {
             printf("%d  " , array[i]);
      }
}

This program runs correctly without any error. So your problem is solved here itself without using malloc or calloc. But just make sure you declare your array after scanning or giving value to your variable which will represent the size of your array(here : x is the variable) and in your case I guess : player1_amount.

But still if you want to use malloc then here it goes :

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

void main()
{
    int x , i;
    int * array;

    printf("\nEnter the value of x : ");
    scanf("%d" , &x);

    array = (int *) malloc(x * sizeof(int));

    for(i = 0 ; i < x ; i++)
    {
        printf("Enter the element : ");
        scanf("%d" , &array[i]);
    }

    for(i = 0 ; i < x ; i++)
    {
        printf("%d  " , array[i]);
    }
}

Both the codes will give you same output. A little explanation ...

Malloc will take input parameter as the amount of memory you wish to allocate to given variable(like 'array' in our case) in bytes and will output the pointer to that block of memory. Since here we are working with integer array the return type is cast as : (int *), had it been a character array we would type cast it as : (char *).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.