-1

Write a dice rolling game. The game should allow a user to toss up to six dice at a time. Each toss of a die will be stored in a six‐element integer array. The array will be created in the main() function, but passed to a new function called tossDie(). The tossDie() function will take care of generating random numbers from one to six and assigning them to the appropriate array element number. After the dice are tossed, the main() function should display the generated values of the dice.

The above is my problem.

I use an array in the tossDie function but when the array in tossDie function is printed. The elements in that array are more than six and I cannot control and the elements are not random numbers between 1 and 6. This is my approach:

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

int tossDie(int [], int);
int main() {
  srand(time(0));
  int a[6]= {'\0'};
  int size = 6;
  tossDie(a, size);
  // printf("The dice results:  \n%d", a);
}

int tossDie(int dice[6], int size){
  srand(time(0));
  int i =0;
  for (i =0; i<=6 ; i++){
    dice[i] = 1 + rand()%(6);
    printf(" %d", dice);
  }
  
  return 0;
}
Woody
  • 23
  • 2
  • 3
    O.T: Call `srand` only once. Also, why are you passing the `size` but hardcoding 6 in the function? Finally, you are accessing outside of the bounds of the array here `dice[i] = 1 + rand()%(6);` instead of `for (i =0; i<=6 ; i++){`, you want `i<6`, not `i<=6` (remember that arrays are 0 based in C). – David Ranieri Nov 26 '22 at 17:48
  • 1
    Is this C or C++? C does not have an `iostream` header. – Retired Ninja Nov 26 '22 at 17:52
  • 1
    `printf(" %d", dice);` does not print the value of a die. `dice` is the entire array, not an element of the array. Use `printf(" %d", dice[i]);`. – Eric Postpischil Nov 26 '22 at 17:55
  • 1
    Do not use `for (i =0; i<=6 ; i++)` for an array with six elements. Use `for (i = 0; i < 6 ; i++)`. – Eric Postpischil Nov 26 '22 at 17:56
  • The loop inside the function should be `for (int i = 0; i < size; ++i)`, otherwise there was no point of passing in the size. – Retired Ninja Nov 26 '22 at 17:58
  • I change the condition to i – Woody Nov 26 '22 at 18:07
  • The code has the number of dice `6` hard-coded four times. With a little rearrangement you can use the single definition `int size = 6;` for the arrays and the loops. There is *another* hard-coded `6` too, the numbers of faces, but it isn't the same 'magic number' as the other `6`s. – Weather Vane Nov 26 '22 at 18:07
  • yeah i know since i just try to figure out how to print the array in the main function instead in the called one – Woody Nov 26 '22 at 18:13
  • 1
    After you call the function in `main` add `for (int i = 0; i < size; ++i) { printf("%d ", a[i]); }` – Retired Ninja Nov 26 '22 at 18:15
  • 1
    i appreciate for the help – Woody Nov 26 '22 at 18:17

1 Answers1

2

MAIN DECLARATION:

Your code could benefit from an overall critique, so let's start with the main declaration. The standard clearly states that:

It [main] shall be defined with a return type of int and with no parameters.

There is a difference between

int main()

and

int main(void)

The first one takes an arbitrary amount of arguments, the second one doesn't.

MAGIC NUMBERS:

You have used the magic number 6, 5 times in the code. Consider using a define preprocessor instead:

#define MAX 6

It's easier to change and maintain. You wouldn't like changing 6 at a dozen places instead of just one.

NULL BYTE:

Replace the null byte '\0' with 0.

SIZE:

A common idiom in C to determine the size of an array is to use:

size_t size = sizeof (a) / sizeof (a[0]);

where sizeof(a) is the total number of bytes, and sizeof(a[0]) is the size of one element.

Though, you won't need to compute the size when you already have defined the size as a define statement?

RANDOM-NUMBER-GENERATOR:

The seed should only be set once. The answers to this question srand() — why call it only once? explain it in detail.

ACCESSING ILLEGAL MEMORY

for (i = 0; i <= 6; i++)

You're accessing memory outside of the bounds of the array, which is illegal. The memory not allocated should not be read.

Array-indexing starts at 0 instead of 1. So the sixth element of the array is at index position 5.

ORIGINAL PROBLEM:

You can use a for loop in the main function to print all the values of the array when you're done with all errors.

Consider changing the return type of the function to void as you do not need to return the array back to main when modifying it. The array decays to a pointer in the function parameter, so it's more of an alias. They both are pointing to the same memory, if you modify the memory through one or the other, the memory would be modified for both.

P.S: You may ask for clarifications if you found something unclear.

Chris
  • 26,361
  • 5
  • 21
  • 42
Harith
  • 4,663
  • 1
  • 5
  • 20