Ç分配,平均整数 [英] c assignment, averaging integers
问题描述
我工作这么昨天约5小时,得到了code。使用帮助,从这个网站的工作,但我觉得我做这是一个骗子的方式方法,我用scanf函数命令。不管怎样,我想这是否解决了正确的方法。多谢你们!哦code编译但被吐出平均是错误的。我想从概念上理解我在做什么错误以及完成这项任务。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
双get_number(INT NUM);
主(){
双N1,N2,N3;
双平均水平; 的printf(\\ nCompute的平均3整数的\\ n);
的printf(-------------------------------- \\ n);
N1 = get_number(1);
N2 = get_number(2);
N3 = get_number(3);
平均=(N1 + N2 + N3)/ 3;
的printf(平均为%0.2F \\ n,平均);
}双get_number(INT NUM){
双值= 0;
INT℃;
的printf(请输入编号%我:NUM);
而((C =的getchar())!='\\ n'){
如果((℃下'0')||(c取代;'9')){
的printf(不正确输入的字符为数字 - %I \\ N,C);
出口(-1);
}
其他{
C =价值;
}
}
返回(值);
}
您 get_number
函数总是返回0,因为你不指定任何的值
变量。我猜你想要的:
=值值* 10 +(C - '0');
而不是
的 C =价值;
要澄清:
您正在阅读由数字一个数字的数字(与的getchar
你读单个字符)。所以我们可以说你读了数字 1 2 3
:
当你读 1
,您值=值* 10 +('1' - '0')
含义值= 0 + 1 = 1
。
当你读 2
,你做的一样,让值= 1×10 +('2' - '0')
意值= 12
。
同为 3
。
你所需要了解的是,乘以10的东西在的东西年底增加了0。添加一个数字的东西,以0结尾,替换0由该数字。
您还必须明白,你正在处理的字符,这实际上是整数。字符 0
重新$ P $通过假设 X
的ASCII code psented。 1
重新由psented $ P $ X + 1
如果你要打印的价值等。 0'+'3'
它不会是 3
如你所期望。这就是为什么我们减去 0
(这是 X
)的字符,以获得实际的数字。
希望扫清一些事情了。
I worked on this yesterday about 5 hours and got the code to work using help from this site, but I think the way I did it was a cheater way, I used a scanf command. Anyways I want to fix this the correct way. Thanks guys! Oh the code compiles but the average that gets spit out is wrong. I would like to conceptually understand what I am doing wrong as well as finish the assignment.
#include <stdio.h>
#include <stdlib.h>
double get_number(int num);
main () {
double n1,n2,n3;
double average;
printf("\nCompute the average of 3 integers\n");
printf("--------------------------------\n");
n1 = get_number(1);
n2 = get_number(2);
n3 = get_number(3);
average = (n1 + n2 + n3)/3;
printf("The average is %0.2f\n",average);
}
double get_number(int num) {
double value = 0;
int c;
printf("Please input number %i: ", num);
while ((c = getchar()) != '\n') {
if ( (c<'0') || (c>'9') ) {
printf("Incorrect character entered as a number - %i\n",c);
exit(-1);
}
else {
c =value;
}
}
return(value);
}
Your get_number
function always returns 0, because you never assign anything to the value
variable. I'm guessing you want:
value = value*10 + (c - '0');
Instead of
c = value;
To clarify:
You are reading a number digit by digit (with getchar
you read a single character). So let's say you read the digits 1 2 3
:
When you read 1
, you do value = value*10 + ('1' - '0')
meaning value = 0 + 1 = 1
.
When you read 2
, you do the same and get value = 1*10 + ('2' - '0')
meaning value = 12
.
Same for 3
.
What you need to understand is that multiplying something by 10 adds a 0 at the end of that something. Adding a digit to something that ends with a zero replaces that 0 by that digit.
You must also understand that you are dealing with characters, which are actually integers. The character '0'
is represented by an ASCII code of let's say x
. '1'
is represented by x + 1
etc. If you were to print the value of '0'+'3'
it would not be 3
as you'd expect. This is why we subtract '0'
(which is x
) from the characters, to get the actual digits.
Hope that clears some things up.
这篇关于Ç分配,平均整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!