Arrays 在C中作为参数传递数组

Arrays 在C中作为参数传递数组,arrays,c,implicit-conversion,function-declaration,function-parameter,Arrays,C,Implicit Conversion,Function Declaration,Function Parameter,我试图创建一个函数来标识数组中的最大值,并计算每次出现的值的总和。这很好,但问题是我需要让函数args表示数组的大小和数组本身 这就是我到目前为止得出的结论: int sum(int a, int size) { int i, max, output=0; //checking every index for max value for(i=0;i<=tam;i++){ if(i==1){ //setting max on

我试图创建一个函数来标识数组中的最大值,并计算每次出现的值的总和。这很好,但问题是我需要让函数args表示数组的大小和数组本身

这就是我到目前为止得出的结论:

int sum(int a, int size)
{
    int i, max, output=0;

    //checking every index for max value
    for(i=0;i<=tam;i++){
        if(i==1){
            //setting max on the first index
            max=a[i];
        }else{
            if(a[i]>max){
                a[i]=max;
            }
        }
    }
    //making the sum
    for(i=0;i<size;i++){
        if(a[i]==max);
        output=output+max;
    }
    printf("%d", output);
}
int和(int a,int大小)
{
int i,最大值,输出=0;
//检查每个索引的最大值
对于(i=0;imax){
a[i]=max;
}
}
}
//计算总数

对于(i=0;i替换
int sum(int a,int size)
int sum(int*a,int size)
int sum(int a[],int size)
替换
int sum(int a,int size)
int sum(int*a,int size)
int sum(int a[],int size)
此函数声明

int sum(int a, int size);
声明一个具有两个类型为
int
的参数的函数。如果您的意思是第一个参数应指定一维数组,则函数声明如下所示

int sum(int a[], int size);

因为编译器会将具有数组类型的参数调整为指向数组元素类型的指针

此外,尽管函数的返回类型不是
void
,但函数不返回任何内容

此外,该函数使用未声明的变量,例如变量
tam

如果数组具有
大小
元素,则索引的有效范围为
[0,大小)

此外,函数不应更改传递的数组。为避免整数溢出,返回类型应为
long-long-int

此外,函数不应输出任何消息。由函数的调用方决定是否输出消息

该函数可以如下所示,如下面的演示程序所示

#include <stdio.h>

long long int sum( const int a[], size_t n )
{
    long long int total = n == 0 ? 0 : a[0];
    
    size_t max = 0;
    
    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[max] < a[i] )
        {
            total = a[i];
            max = i;
        }
        else if ( !( a[i] < a[max] ) )
        {
            total += a[max];
        }
    }
    
    return total;
}

int main(void) 
{
    int a[] = { 2, 8, 8, 9, 7, 3, 8, 1, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );
    
    printf( "The sum of elements with the maximum value is %lld\n", sum( a, N ) );
    
    return 0;
}

此函数声明

int sum(int a, int size);
声明一个具有两个类型为
int
的参数的函数。如果您的意思是第一个参数应指定一维数组,则函数声明如下所示

int sum(int a[], int size);

因为编译器会将具有数组类型的参数调整为指向数组元素类型的指针

此外,尽管函数的返回类型不是
void
,但函数不返回任何内容

此外,该函数使用未声明的变量,例如变量
tam

如果数组具有
大小
元素,则索引的有效范围为
[0,大小)

此外,函数不应更改传递的数组。为避免整数溢出,返回类型应为
long-long-int

此外,函数不应输出任何消息。由函数的调用方决定是否输出消息

该函数可以如下所示,如下面的演示程序所示

#include <stdio.h>

long long int sum( const int a[], size_t n )
{
    long long int total = n == 0 ? 0 : a[0];
    
    size_t max = 0;
    
    for ( size_t i = 1; i < n; i++ )
    {
        if ( a[max] < a[i] )
        {
            total = a[i];
            max = i;
        }
        else if ( !( a[i] < a[max] ) )
        {
            total += a[max];
        }
    }
    
    return total;
}

int main(void) 
{
    int a[] = { 2, 8, 8, 9, 7, 3, 8, 1, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );
    
    printf( "The sum of elements with the maximum value is %lld\n", sum( a, N ) );
    
    return 0;
}
#包括
#包括
整数和(整数*a,整数大小);
int main()
{
int*a=(int*)malloc(10*sizeof(int));
对于(int i=0;i
#包括
#包括
整数和(整数*a,整数大小);
int main()
{
int*a=(int*)malloc(10*sizeof(int));

对于(int i=0;i好的,你可以在一次过程中完成,因为当你确定一个新的最大值时,最后一个的累积和不再有效(它指的不是最大的数字,而是一个较小的数字)

您的代码中有一些奇怪的东西……您在
0
处开始循环,然后比较if
(i==1)
,我猜这是一个错误(不应该是
0
?),因为您应该检查您是否在第一个单元格(而不是第二个单元格)要初始化
max
。无论如何,有一个聪明的方法是将
max
初始化到您可以拥有的最小值(对于
int
,您在
中将该值作为常量
int\u MIN
)。现在,我将向您展示解决问题的一个可能的源代码(摘自您的,但更改了一些变量并添加了其他变量,以向您展示在一次过程中您可以完成大量工作:

#include <stdio.h>
#include <limits.h>

/* pretty format of output with location of trace in source file
 */
#define F(_fmt) __FILE__":%d:%s: "_fmt,__LINE__,__func__

/* to pass an array, just declare it as below.  The array size is
 * unspecified because your don't know it before calling sum,
 * and this is the reason to pass the array size. */
int sum(int a[], int size)
{
    int i, pos0 = -1, pos1 = -1, max = INT_MIN, output=0;

    /* checking every index for max value, and output, you made
     * an error here and used tam instead of size. */
    for(i = 0; i <= size; i++){
        if (a[i] > max) {  /* if greater than max */
            pos0 = i;      /* save position as first */
            max = a[i];    /* save max value */
            output = max;  /* initialize output to max */
        } else if (a[i] == max) {  /* if equal to max */
            pos1 = i;      /* save position as last */
            output += max; /* add to output */
        } /* else nothing */
    }
    /* print all the values we got */
    printf(F("pos0 = %d, pos1 = %d, max = %d, output = %d\n"),
        pos0, pos1, max, output);
    return output;         /* return the requested sum */
}

int list[] = { 3, 2, 5, 6, 5, 4, 7, 3, 7, 4, 7, 2, 1, 6, 2 };
            /* max is located    ^     ^     ^  in these positions
             *                   6     8    10   */

/* the following macro gives us the size of the array list by
 * dividing the size of the complete array by the size of the
 * first element. */
#define n_list (sizeof list / sizeof list[0])

int main()
{
    printf(F("result = %d\n"), sum(list, n_list));
}

好的,你只需一次就可以做到,因为当你确定一个新的最大值时,最后一个的累计和不再有效(它不是指最大的数字,而是指一个较小的数字)

您的代码中有一些奇怪的东西……您在
0
处开始循环,然后比较if
(i==1)
,我猜这是一个错误(不应该是
0
?),因为您应该检查您是否在第一个单元格(而不是第二个单元格)要初始化
max
。无论如何,有一个聪明的方法是将
max
初始化到您可以拥有的最小值(对于
int
,您在
中将该值作为常量
int\u MIN
)。现在,我将向您展示解决问题的一个可能的源代码(摘自您的,但更改了一些变量并添加了其他变量,以向您展示在一次过程中您可以完成大量工作:

#include <stdio.h>
#include <limits.h>

/* pretty format of output with location of trace in source file
 */
#define F(_fmt) __FILE__":%d:%s: "_fmt,__LINE__,__func__

/* to pass an array, just declare it as below.  The array size is
 * unspecified because your don't know it before calling sum,
 * and this is the reason to pass the array size. */
int sum(int a[], int size)
{
    int i, pos0 = -1, pos1 = -1, max = INT_MIN, output=0;

    /* checking every index for max value, and output, you made
     * an error here and used tam instead of size. */
    for(i = 0; i <= size; i++){
        if (a[i] > max) {  /* if greater than max */
            pos0 = i;      /* save position as first */
            max = a[i];    /* save max value */
            output = max;  /* initialize output to max */
        } else if (a[i] == max) {  /* if equal to max */
            pos1 = i;      /* save position as last */
            output += max; /* add to output */
        } /* else nothing */
    }
    /* print all the values we got */
    printf(F("pos0 = %d, pos1 = %d, max = %d, output = %d\n"),
        pos0, pos1, max, output);
    return output;         /* return the requested sum */
}

int list[] = { 3, 2, 5, 6, 5, 4, 7, 3, 7, 4, 7, 2, 1, 6, 2 };
            /* max is located    ^     ^     ^  in these positions
             *                   6     8    10   */

/* the following macro gives us the size of the array list by
 * dividing the size of the complete array by the size of the
 * first element. */
#define n_list (sizeof list / sizeof list[0])

int main()
{
    printf(F("result = %d\n"), sum(list, n_list));
}

int a
->
int*a
“我得到的错误是“a”既不是数组,也不是指针,也不是向量。”这是因为
a
确实不是数组。说真的,如果你不能找到这个编译器错误的原因,你需要后退一步,再看一遍你的C语言书中的数组章节。
inta
->
int*a
“我得到的错误是“a”既不是数组,也不是指针,也不是向量。”这可能是因为
a
确实不是数组。说真的,如果您无法找到此编译器错误的原因,您需要后退一步,再次阅读C语言书中的数组章节。
int sum(int size,int a[size]);
也是有效的,函数中的sizeof将在运行时计算为正确的值。我对该符号唯一的不满是size参数必须出现在array参数之前,从而破坏了array,array_sizeconvention@andynint a*[size]是无效的声明。输入错误,已修复。@andyn此声明int a[size])也不允许获取ele的编号
$ test
test.c:23:sum: pos0 = 6, pos1 = 10, max = 7, output = 21
test.c:34:main: result = 21
$ _