简体   繁体   English

将数组传递给 function 然后在 main function 中打印该数组

[英]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.每次掷骰子都会存储在一个六元素数组 integer 中。 The array will be created in the main() function, but passed to a new function called tossDie().该数组将在 main() function 中创建,但传递给一个名为 tossDie() 的新 function。 The tossDie() function will take care of generating random numbers from one to six and assigning them to the appropriate array element number. tossDie() function 将负责生成从 1 到 6 的随机数,并将它们分配给适当的数组元素编号。 After the dice are tossed, the main() function should display the generated values of the dice.掷骰子后,main() function 应该显示骰子的生成值。

The above is my problem.以上是我的问题。

I use an array in the tossDie function but when the array in tossDie function is printed.我在 tossDie function 中使用了一个数组,但是当打印了 tossDie function 中的数组时。 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:该数组中的元素超过六个,我无法控制,元素不是 1 到 6 之间的随机数。这是我的方法:

#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.它 [main] 应定义为返回类型 int 且不带参数。

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.第一个占用任意数量的 arguments,第二个不占用。

MAGIC NUMBERS:神奇数字:

You have used the magic number 6, 5 times in the code.您在代码中使用了幻数 6、5 次。 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.您不希望在十几个地方而不是一个地方更改 6。

NULL BYTE: NULL 字节:

Replace the null byte '\0' with 0 .将 null 字节'\0'替换为0

SIZE:尺寸:

A common idiom in C to determine the size of an array is to use: C 中确定数组大小的常用习惯用法是:

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.其中 sizeof(a) 是字节总数,sizeof(a[0]) 是一个元素的大小。

Though, you won't need to compute the size when you already have defined the size as a define statement?但是,当您已经将大小定义为 define 语句时,您不需要计算大小吗?

RANDOM-NUMBER-GENERATOR:随机数生成器:

The seed should only be set once.种子只能设置一次。 The answers to this question srand() — why call it only once?这个问题的答案srand() — 为什么只调用一次? explain it in detail.详细解释一下。

ACCESSING ILLEGAL MEMORY非法访问 MEMORY

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

You're accessing memory outside of the bounds of the array, which is illegal.您在数组边界之外访问 memory,这是非法的。 The memory not allocated should not be read.没有分配的memory不要读取。

Array-indexing starts at 0 instead of 1. So the sixth element of the array is at index position 5.数组索引从 0 而不是 1 开始。因此数组的第六个元素位于索引 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.处理完所有错误后,您可以在主 function 中使用 for 循环打印数组的所有值。

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.考虑将 function 的返回类型更改为 void,因为在修改它时不需要将数组返回给 main。 The array decays to a pointer in the function parameter, so it's more of an alias.该数组衰减为 function 参数中的指针,因此它更像是一个别名。 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.它们都指向相同的 memory,如果您通过一个或另一个修改 memory,则两个 memory 都会被修改。

PS: You may ask for clarifications if you found something unclear. PS:如有不明之处,可追问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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