gpt4 book ai didi

c - 在子函数中输入大量元素并在main()中相乘

转载 作者:行者123 更新时间:2023-11-30 19:43:38 24 4
gpt4 key购买 nike

我必须在子函数中输入大量元素,并在main()函数中输入小于max元素的多重元素。
为了输入元素,我编写了以下代码:

#include <stdio.h>
#define K 70

int input_ar (int* x){
int i;
printf("Inputing elements of massive \n");
for(i=0;i<K;i++){
printf("Please, enter element %d:", i);
scanf("%d", &x[i]);
}
}

乘法:

int main(){
int x, i, mult=1;
int max=input_ar(0);
for(i=0;i<K;i++){
if(input_ar(i)>max){
max=input_ar(i);
}
if(input_ar(i)<max){
mult*=input_ar(i);
}

}
printf("\n Sum of elements = %d ", mult);
}

但我收到错误:

[Error] void value not ignored as it ought to be
[Error] invalid conversion from 'int' to 'int*' [-fpermissive]

如何消除这些错误?

最佳答案

捕获!:)

#include <stdio.h>

#define N 70

int input_ar( int *a, int n )
{
int i = 0;

printf( "Inputing elements of massive\n" );

for ( ; i < n; i++ )
{
printf( "Please, enter element %d: ", i );
if ( scanf( "%d", &a[i] ) != 1 ) break;
}

return i;
}

int max_element( const int *a, int n )
{
int max = 0;
int i;

for ( i = 1; i < n; i++ ) if ( a[max] < a[i] ) max = i;

return max;
}

int main(void)
{
int a[N];
int n = input_ar( a, N );
int max = max_element( a, n );

int count = 0;
int long long product = 1;
int i;

for ( i = 0; i < n; i++ )
{
if ( a[i] != a[max] )
{
++count;
product *= a[i];
}
}

if ( count != 0 )
{
printf( "The product of elements "
"that are not equal to the maximum element "
"is %lld\n", product );
}
else
{
puts( "All elements of the array are equal each other" );
}

return 0;
}

至于您的代码,它包含许多错误,从函数 input_ar 开始,该函数不返回任何内容,并以表示总和而不是乘积的字符串文字结束

"\n Sum of elements = %d "

关于c - 在子函数中输入大量元素并在main()中相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com