简体   繁体   中英

Passing an array to a function then print that array in main function

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM