错误太少的参数调用even_odd [英] error too few parameters to call even_odd
问题描述
我想显示从1到给定数字的奇数和偶数整数,并显示奇数和和的总和,但总是出现错误,说的参数太少。 (无效功能是必需的)最后我应该把公式用于获得奇数和总和的公式?
Hi, I would like to display the odd and even integers from 1 upto given number and display the sum of odd and sum of even but there's always an error saying too few parameters. (void function is required) And lastly where should i put the formula for getting the sum of odd and sum of even?
# include <stdio.h>
# include <conio.h>
void even_odd(int n);
int main()
{
int n, i ;
clrscr() ;
even_odd();
getch() ;
return 0;
}
void even_odd(int n){
printf("Enter Integer : ") ;
scanf("%d", &n) ;
printf("\nThe odd numbers are :\n\n") ;
for(i = 1 ; i <= n ; i = i + 2)
printf("%d\t", i) ;
printf("\n\nThe even numbers are :\n\n") ;
for(i = 2 ; i <= n ; i = i + 2)
printf("%d\t", i) ;
}
</conio.h></stdio.h>
推荐答案
你的问题是n
不是参数,而是局部变量
Your problem is thatn
is not a parameter but a local variable
void even_odd(int n){
必须替换为
must be replaced by
void even_odd()
int n;{
哎呀,您还需要在 main
之前将行更改为
Ooops, you also need to change the line before main
to
void even_odd();
如果您按原样编译,Turbo C会告诉您还有另一个问题,因为 i
和 n
用于 even_odd
,但在 main
中声明。
两次更正的源代码看起来像
If you compile as is, Turbo C will tell you that there is another problem because i
and n
are used in even_odd
but declared in main
.
the source code with both corrections will look like
# include <stdio.h>
# include <conio.h>
void even_odd();
int main()
{
clrscr() ;
even_odd();
getch() ;
return 0;
}
void even_odd(){
int n, i ;
printf("Enter Integer : ") ;
scanf("%d", &n) ;
printf("\nThe odd numbers are :\n\n") ;
for(i = 1 ; i <= n ; i = i + 2)
printf("%d\t", i) ;
printf("\n\nThe even numbers are :\n\n") ;
for(i = 2 ; i <= n ; i = i + 2)
printf("%d\t", i) ;
}
</conio.h></stdio.h>
错误说参数太少。 (需要空函数)
an error saying too few parameters. (void function is required)
你的函数
Your function
void even_odd(int n)
需要一个参数,但你不用任何参数调用它:
expects a parameter, but you call it without any:
even_odd();
试试这样:
Try it like this:
# include <stdio.h>
# include <conio.h>
void even_odd();
int main()
{
int i ;
clrscr();
even_odd();
getch() ;
return 0;
}
void even_odd()
{
int i, n;
printf("Enter Integer : ") ;
scanf("%d", &n) ;
printf("\nThe odd numbers are :\n\n") ;
for(i = 1 ; i <= n ; i = i + 2)
printf("%d\t", i) ;
printf("\n\nThe even numbers are :\n\n") ;
for(i = 2 ; i <= n ; i = i + 2)
printf("%d\t", i) ;
}
其他人已经描述了调用的问题。那么计算总和又是什么呢?你已经有两个循环,一个用于奇数,另一个用于偶数,为什么不计算这些循环中的总和。
类似于:
Others have already described the problem with the call. So what comes to calculating the sum; You already have two loops, one for odd and another for even numbers, why not calculate the sum inside those loops.
Something like:
...
for(i = 1 ; i <= n ; i = i + 2) {
printf("%d\t", i) ;
odd_sum = odd_sum + i;
}
...
这篇关于错误太少的参数调用even_odd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!