使用数组作为参数的Caling函数

时间:2022-02-06 21:43:58

I'm having some difficulty with my syntax. The purpose of the below program is to print an array of 20 random numbers, find the max of the array and then print the array and the max. I've got the sections to initialize and create the array without any problem, but my issue is with the find_max function. In the main function, I'm trying to work out the syntax to call my find_max function there so I can print the results. If anyone can help to fix my syntax, I'd really appreciate it.

我的语法有些困难。以下程序的目的是打印一个包含20个随机数的数组,找到数组的最大值,然后打印数组和最大值。我已经有了初始化和创建数组的部分没有任何问题,但我的问题是find_max函数。在main函数中,我正在尝试编写语法来调用我的find_max函数,以便我可以打印结果。如果有人可以帮助修复我的语法,我真的很感激。

#include <cstdlib>
#include <iostream>
using
namespace std;

void
init_array(int array[], int size, int range)
{
   for (int index = 0; index < size; index++)
      array[index] = rand() % range;
}

void print_array(int array[], int size)
{
   for (int index = 0; index < size; index++)
      cout << array[index] << " ";
   cout << endl;
}

int find_max (int array[], int size)
{
   int max = array[0];
   for (int index = 0; index < size; index++)
   {
      if (array[index] > max)
         max = array[index];
   }
   return max;
}


int main()
{
   // Declare array of integers
    const int size = 20;
    int array[size] = {0};
    int max = find_max(array,size);


   // Initialize and print array
   init_array(array, size, 100);
   print_array(array, size);
   cout << "The max is:" << max << endl;



   return 0 ;
}

4 个解决方案

#1


1  

You try to find max before the array is initialized. That is not what you want. The following works:

您尝试在阵列初始化之前找到max。那不是你想要的。以下作品:

   const int DATA_SIZE = 20;
   int data[DATA_SIZE] = {0};

   // Initialize and print array
   init_array(data, DATA_SIZE, 100);
   print_array(data, DATA_SIZE);

   // Find maximum value
   int max = find_max(data, DATA_SIZE);

   cout << "max = " << max << endl;

   return 0;

#2


2  

Your call of find_max needs to use your declared variables and that after you call init_array. Otherwise your array is empty.

您对find_max的调用需要使用您声明的变量,并且在调用init_array之后。否则你的数组是空的。

The call to find_max should look like this:

对find_max的调用应该如下所示:

int max = find_max(data, DATA_SIZE);

#3


0  

Well, when declaring the array this way:

那么,当以这种方式声明数组时:

int data[DATA_SIZE] = {0};

so all 20 elements in it equals zero. your main function should look like this:

所以它中的所有20个元素都等于零。你的主要功能应如下所示:

int main()
{
   // Declare array of integers
   const int DATA_SIZE = 20;
   int data[DATA_SIZE] = {0};

   // Initialize and print array
   init_array(data, DATA_SIZE, 100);
   cout<< find_max(data, DATA_SIZE)<< endl;
   print_array(data, DATA_SIZE);

   return 0 ;
}

you give the 20 elements random values first by calling init_array and then you try finding the Max of them.

首先通过调用init_array给出20个元素随机值,然后尝试找到它们的最大值。

#4


0  

You can give your function the start address of your array and the size of array, then you can use pointers to traverse the array. :)

您可以为函数指定数组的起始地址和数组的大小,然后可以使用指针遍历数组。 :)

#1


1  

You try to find max before the array is initialized. That is not what you want. The following works:

您尝试在阵列初始化之前找到max。那不是你想要的。以下作品:

   const int DATA_SIZE = 20;
   int data[DATA_SIZE] = {0};

   // Initialize and print array
   init_array(data, DATA_SIZE, 100);
   print_array(data, DATA_SIZE);

   // Find maximum value
   int max = find_max(data, DATA_SIZE);

   cout << "max = " << max << endl;

   return 0;

#2


2  

Your call of find_max needs to use your declared variables and that after you call init_array. Otherwise your array is empty.

您对find_max的调用需要使用您声明的变量,并且在调用init_array之后。否则你的数组是空的。

The call to find_max should look like this:

对find_max的调用应该如下所示:

int max = find_max(data, DATA_SIZE);

#3


0  

Well, when declaring the array this way:

那么,当以这种方式声明数组时:

int data[DATA_SIZE] = {0};

so all 20 elements in it equals zero. your main function should look like this:

所以它中的所有20个元素都等于零。你的主要功能应如下所示:

int main()
{
   // Declare array of integers
   const int DATA_SIZE = 20;
   int data[DATA_SIZE] = {0};

   // Initialize and print array
   init_array(data, DATA_SIZE, 100);
   cout<< find_max(data, DATA_SIZE)<< endl;
   print_array(data, DATA_SIZE);

   return 0 ;
}

you give the 20 elements random values first by calling init_array and then you try finding the Max of them.

首先通过调用init_array给出20个元素随机值,然后尝试找到它们的最大值。

#4


0  

You can give your function the start address of your array and the size of array, then you can use pointers to traverse the array. :)

您可以为函数指定数组的起始地址和数组的大小,然后可以使用指针遍历数组。 :)