0

My code:

// DICE ROLL PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>


int main()
{
    // defining variables until "till here" comment
    int i;
    int rollDice;
    int firInp;
    int secInp;
    int flag = 1; // flag variable set to 1 which will later be set to zero
    srand (time(NULL)); // seeding rand so that we get different values every time

    // till here

    while(flag) // while loop runs as long as flag variable has a value
    {

    printf("Enter the amount of faces you want your dice to have (MAX=24, MIN=2): "); // prints the message
    scanf("%d", &firInp); // user input stored into firInp
    printf("Enter the amount of throws you want(MAX=499, MIN=2): "); // this message is printed after the users first input
    scanf("%d", &secInp); // user input stored into secInp



    if (((firInp < 25)&&(firInp > 1))&&((secInp < 500)&&(secInp > 1))){ // if statement to check parameters met
    for(i = 0; i < secInp; i++){
        rollDice = (rand()%firInp) + 1; // added 1 to rand because if the value is ever zero, you will get an error
        printf("%d \n", rollDice);

    }
    flag = 0; // now the flag variable is set to zero, exiting the while loop
    }
    else{
        printf("Sorry, these numbers don't meet the parameters\nPlease enter a number in the right parameters.\n");
    }
    }

   return 0;
}

I want to input the values I obtain from "rollDice" into an array. for example: If the user enters firInp and secInp as 6, and they get the following values: 1 2 2 3 1 6 I want these numbers to be stored in an array like so: arrayA = [1,2,2,3,1,6]

4
  • 2
    Create an array of int and insert rollDice values in it? Commented Dec 16, 2020 at 3:32
  • but how? I know it might be simple, but I'm fairly new to coding so any help will be greatly appreciated Commented Dec 16, 2020 at 3:39
  • 4
    For that I would suggest reading a good book on C Commented Dec 16, 2020 at 3:39
  • You can create an array in C using calloc (I don't recommend using malloc).You'll read a lot of beginner-level tutorials using statically allocated arrays like int foo[6] however those have a fixed-size that cannot be resized at runtime or be sized based on program input. Don't forget to always call free for every time you use calloc/malloc. I suppose you could also use alloca but please don't. Commented Dec 16, 2020 at 3:41

1 Answer 1

1

Well it really depends on the particular codebase - my example uses stack frame allocation but it could easily be done using the malloc/calloc method suggested in the comments

(The following is pseudocode, you wil need to integrate it into your application)

int main() {
    int dice_rolls[6];
    for(int i = 0; i < 6 i++) {
        roll = rollTheDice(); // In your program you have your own method for getting this number - the point still stands
        dice_rolls[i] = roll;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.