I have some problems understanding how to do this kind of stuffs. I have already made a function that does the logical part like declaring an Integer that get the value of the array[i] and this number is random picked from the array(i think). Anyway here is the code that I have done:
我在理解如何做这种事情时遇到了一些问题。我已经创建了一个函数来完成逻辑部分,就像声明一个得到数组[i]的值的Integer一样,这个数字是从数组中随机选取的(我认为)。无论如何这里是我做的代码:
void array_test( int arr[] , int size)
{
int i;
int random_number;
for(i = 0; i<size; i++){
random_number = (rand() %15 + 1);
arr[i] = random_number;
}
}
Now my question is how do I print the result(the code works and it does print out 15 different numbers between 1 and 15. I have defined the size to 15 and it is on the function prototype parameter) using another function? The function takes a char output[]
as one parameter, an array of type int
, and a size of type int
. Looks like this output_result(char output[], int arr[], int size)
.
现在我的问题是如何打印结果(代码工作,它确实在1到15之间打印出15个不同的数字。我已经将大小定义为15并且它在函数原型参数上)使用另一个函数?该函数将char output []作为一个参数,int类型的数组和int类型的大小。看起来像这个output_result(char output [],int arr [],int size)。
The thing is that I don't understand how I'm supposed to call the stuff from the previous function to this and then show it to the user. I admit that this is an assignment but I have tried many things and nothing works. The thing is that I have to printf all the arrays(?) value by using a for-loop. It's here it just get messy. I've tried to create the wheel again ,so to say, by copy pasting the code from the function array_test but then the function array_test would be unnecessary to implement. No pointers shall be used until next assignment.
问题是,我不明白我应该如何调用前一个函数中的东西,然后将其显示给用户。我承认这是一项任务,但我尝试了许多事情,没有任何作用。问题是我必须使用for循环打印所有数组(?)值。它在这里变得凌乱。我试图再次创建*,所以说,通过复制粘贴函数array_test中的代码,但是函数array_test将不必实现。在下次分配之前不得使用指针。
Here is the main function:
这是主要功能:
int main() {
int arr[SIZE];
srand(time(0));
array_test(arr, SIZE);
print_array("This is an array\0", arr, SIZE);
return 0;
}
2 个解决方案
#1
4
If you want to print the entire array, you can do the following:
如果要打印整个阵列,可以执行以下操作:
EDIT: Based on your main function that you gave us, your print_values_ver1
function could look something like this:
编辑:根据您给我们的主要功能,您的print_values_ver1函数可能如下所示:
In your main function, to find array size you would use this:
在您的main函数中,要查找数组大小,您可以使用:
int size = sizeof(array) / sizeof(array[0]);
int size = sizeof(array)/ sizeof(array [0]);
Then pass that you your print function like:
然后通过你的打印功能,如:
print_values_ver1("Some text\0", arr, size);
print_values_ver1(“Some text \ 0”,arr,size);
and your function definition would look something like:
你的函数定义看起来像:
print_values_ver1(char[] output, int arr[], int SIZE) {
for (int z = 0; z < SIZE; z ++) {
printf("%d\n", arr[i]);
}
}
Note that %d
is a format specifier of the printf
function (see documentation). And \n
is a newline
character.
请注意,%d是printf函数的格式说明符(请参阅文档)。而\ n是一个换行符。
This code iterates through each element of the array and prints the value (one value per line).
此代码遍历数组的每个元素并打印该值(每行一个值)。
#2
1
Like this: (comments in-line explain whys-)
像这样:(评论在线解释为什么 - )
[EDIT] adds output_results()
[编辑]添加output_results()
#include <ansi_c.h>
void array_test( int arr[] , int size);
void output_result(int a[], int size);
int main(void)
{
int arr[15];
int size = sizeof(arr)/sizeof(arr[0]);
array_test(arr, size);
output_result(arr, size);
getchar();
return 0;
}
void output_result(int a[], int size)
{
int i=0;
for(i=0;i<size;i++) printf("a[%d] is:%d\n", i, a[i]);
}
void array_test( int arr[] , int size)
{
int i;
int randomly_picked_number;
for(i = 0; i<size; i++){
randomly_picked_number = (rand() %15 + 1);
arr[i] = randomly_picked_number;
}
}
#1
4
If you want to print the entire array, you can do the following:
如果要打印整个阵列,可以执行以下操作:
EDIT: Based on your main function that you gave us, your print_values_ver1
function could look something like this:
编辑:根据您给我们的主要功能,您的print_values_ver1函数可能如下所示:
In your main function, to find array size you would use this:
在您的main函数中,要查找数组大小,您可以使用:
int size = sizeof(array) / sizeof(array[0]);
int size = sizeof(array)/ sizeof(array [0]);
Then pass that you your print function like:
然后通过你的打印功能,如:
print_values_ver1("Some text\0", arr, size);
print_values_ver1(“Some text \ 0”,arr,size);
and your function definition would look something like:
你的函数定义看起来像:
print_values_ver1(char[] output, int arr[], int SIZE) {
for (int z = 0; z < SIZE; z ++) {
printf("%d\n", arr[i]);
}
}
Note that %d
is a format specifier of the printf
function (see documentation). And \n
is a newline
character.
请注意,%d是printf函数的格式说明符(请参阅文档)。而\ n是一个换行符。
This code iterates through each element of the array and prints the value (one value per line).
此代码遍历数组的每个元素并打印该值(每行一个值)。
#2
1
Like this: (comments in-line explain whys-)
像这样:(评论在线解释为什么 - )
[EDIT] adds output_results()
[编辑]添加output_results()
#include <ansi_c.h>
void array_test( int arr[] , int size);
void output_result(int a[], int size);
int main(void)
{
int arr[15];
int size = sizeof(arr)/sizeof(arr[0]);
array_test(arr, size);
output_result(arr, size);
getchar();
return 0;
}
void output_result(int a[], int size)
{
int i=0;
for(i=0;i<size;i++) printf("a[%d] is:%d\n", i, a[i]);
}
void array_test( int arr[] , int size)
{
int i;
int randomly_picked_number;
for(i = 0; i<size; i++){
randomly_picked_number = (rand() %15 + 1);
arr[i] = randomly_picked_number;
}
}