在子函数中输入大量元素并在 main() 中相乘
Inputting elements of massive in sub-function and multiplying in main()
我必须在子函数中输入大量的元素,在main()函数中输入小于最大元素的多元素。
为了输入元素,我写了这段代码:
#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]
如何消除这些错误?
您的函数 input_ar
有一个 int*
类型的参数。您将 int
值传递给函数。这就是编译器所抱怨的。您还应该 return 来自 input_ar
和 main
的 int
,尽管如果您使用 C99 或 C11 进行编译,它可能会在 main
的情况下自动提供在该标准中看到:
5.1.2.2.3 Program termination
- If the return type of the
main
function is a type compatible with int
, a return from the initial call to the main
function is equivalent to calling the exit function with the value returned by the main
function as its argument; 11 reaching the }
that terminates the main
function returns a value of 0. If the return type is not compatible with int
, the
termination status returned to the host environment is unspecified.
主要功能必须return。在 main.
末尾添加 return 语句
return 0;
你定义的函数将参数作为整数指针*x
,但你传递的是值i
。
就这样吧
input_ar(&i);
在所有地方。然后你放置这条线,
mult*=input_ar(i);
但是您正在return从那个函数中获取任何东西。您必须从该函数 return。
接住!:)
#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 开始,returns 什么都没有,并以表示总和而不是乘积的字符串文字结尾
"\n Sum of elements = %d "
我必须在子函数中输入大量的元素,在main()函数中输入小于最大元素的多元素。
为了输入元素,我写了这段代码:
#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]
如何消除这些错误?
您的函数 input_ar
有一个 int*
类型的参数。您将 int
值传递给函数。这就是编译器所抱怨的。您还应该 return 来自 input_ar
和 main
的 int
,尽管如果您使用 C99 或 C11 进行编译,它可能会在 main
的情况下自动提供在该标准中看到:
5.1.2.2.3 Program termination
- If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling the exit function with the value returned by themain
function as its argument; 11 reaching the}
that terminates themain
function returns a value of 0. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.
主要功能必须return。在 main.
末尾添加 return 语句return 0;
你定义的函数将参数作为整数指针*x
,但你传递的是值i
。
就这样吧
input_ar(&i);
在所有地方。然后你放置这条线,
mult*=input_ar(i);
但是您正在return从那个函数中获取任何东西。您必须从该函数 return。
接住!:)
#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 开始,returns 什么都没有,并以表示总和而不是乘积的字符串文字结尾
"\n Sum of elements = %d "