功能问题 [英] Problem with Function

查看:53
本文介绍了功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我...我无法得到正确的输出......我的教授很糟糕'因为她没有彻底讨论C的'和退出'编程..请帮助......这就是问题:我必须使用一个函数编写一个程序,当你输入一个数字时,它会将数字从1加到输入的数字,你也将展示如何平方的数字。


所需输出:

输入数字:5

1 + 4 + 9 + 16 + 25

总和是:55


我的产量:

输入一个数字:5

1 + 4 + 9 + 16 + 25

总和是:5869


非常感谢您提前...这是我的代码,这肯定是错的这个

展开 | 选择 | Wrap | 行号

解决方案


请帮助我...我无法得到正确的输出......我的教授很糟糕她没有彻底讨论C编程中的'和退出'。请帮助......这就是问题:我必须使用一个函数编写一个程序,当你输入一个数字,它会将数字从1添加到输入的数字,你也会显示数字的平方。


所需输出:

输入数字:5

1 + 4 + 9 + 16 + 25

总和是:55


我的产量:

输入一个数字:5

1 + 4 + 9 + 16 + 25

总和是:5869


非常感谢您提前...这是我的代码,这肯定是错的这个


#include< stdio.h>

int funct(int x);

int functi(int y) ;

int main()

{

int c,d,num;

clrscr();

printf(" \\\
输入一个数字:");

scanf("%d",& num);

for(c = 1; c< = num c ++){

printf("%d +",funct(c));

}

printf(\ n总和是:%d,functi(d));

getch();

}

int funct(int x)

{

int a;

a = x * x;

返回a;

}

int functi(int y)

{

int b;

b = y + b;

返回b;

}



你永远不会初始化d。当你声明它时,编译器会给它内存空间 - 而d变成x04a3d(内存的某个任意插槽)。但是,它不会使用值初始化它。你在使用那个内存点x04a3d之前使用的一些程序,并且有一个随机的垃圾。它现在具有的价值。这就是你传递给functi()的价值(顺便说一下,将两个函数命名为两个函数并且它们只有一个字符不同被认为是不好的做法......当你进入更长的程序时,它也会让你感到困惑 - 我会为他们推荐mult(int x)和addr(int y)...有点表明他们做了什么......但你需要设置'd'的值,你要添加的数字。


既然你已经编译了,我猜你的代码不是直接从你的程序中复制的 - 编译器会在那里大吼大叫 - 你呢'使用getch()(这个函数让我的语言最让我烦恼,应该永远被禁止),但是不要声明conio,并且你将main声明为int,但最后不要返回任何内容如果你试图复制那段代码,那么只是抬头......


或许,在OP看来,编译器错误不是正确的输出?;)

please help me...I can''t get the correct output...My professor suck''s because she''s didn''t discuss thoroughly the in''s and out''s of C programming..please help...Here''s the problem: I have to write a program using a function that when you input a number, it will add the number from 1 to the number that was input, also you will show how the numbers where squared.

Desired output:
Enter a number: 5
1+4+9+16+25
The sum is: 55

My ouput:
Enter a number: 5
1+4+9+16+25
The sum is: 5869

Thank you very much in advance..here''s my code, there''s definitely wrong with this

Expand|Select|Wrap|Line Numbers

解决方案

please help me...I can''t get the correct output...My professor suck''s because she''s didn''t discuss thoroughly the in''s and out''s of C programming..please help...Here''s the problem: I have to write a program using a function that when you input a number, it will add the number from 1 to the number that was input, also you will show how the numbers where squared.

Desired output:
Enter a number: 5
1+4+9+16+25
The sum is: 55

My ouput:
Enter a number: 5
1+4+9+16+25
The sum is: 5869

Thank you very much in advance..here''s my code, there''s definitely wrong with this

#include <stdio.h>
int funct(int x);
int functi(int y);
int main()
{
int c, d, num;
clrscr();
printf("\nEnter a number: ");
scanf("%d",&num);
for(c=1;c<=num;c++){
printf("%d+",funct(c));
}
printf("\nThe sum is: %d",functi(d));
getch();
}
int funct(int x)
{
int a;
a=x*x;
return a;
}
int functi(int y)
{
int b;
b=y+b;
return b;
}

You never initialize d. When you declare it, the compiler gives it memory space - and d becomes x04a3d (some arbitrary slot of memory). However, it does not initialize this with a value. Some program you were using before used that memory spot x04a3d, and there is a random "garbage" value in it that d now has. That is the value you are passing to functi() (by the way, it is considered bad practice to name two functions and only have them differ by one character... it will also get very confusing for you when you get into longer programs - I would recommend mult(int x) and addr(int y) for them... somewhat indicative of what they do... But you need to set values for ''d'', the numbers you want to add.


And since you got it to compile, I''m guessing that''s not your code copied directly from your program - the compiler would have yelled about a few things in there - you''re using getch() (the function that annoys me the most out of any language and should be banned forever), but don''t declare conio, and you declare main as int, but don''t return anything at the end. Just a heads up if you try to copy that code...


Perhaps, in the OP''s opinion, a compiler error is not the right output? ;)


这篇关于功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆