如何打印5个数字的总和?平均? [英] How do I print the sum of the 5 numbers? And the average?

查看:131
本文介绍了如何打印5个数字的总和?平均?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开平!我正在用Cion编写C语言,我是新手。



问题是:



编写一个程序,询问用户数字,直到给出0,然后:



打印出已给出的数字(不包括0) ):

数:5

打印出所有数字的总和:

总和:78

打印平均值:

平均值:15.6



但是我似乎无法打印这笔款项?平均我不知道如何。



我尝试过:



Hei! I'm programming in C right now with CLion, and I'm new to this.

the question is:

"Write a program that asks the user for numbers until 0 is given, then:

Prints out how many numbers have been given (excluding the 0):
Count: 5
Prints out the sum of all the numbers:
Sum: 78
Prints out the average:
Average: 15.6"

but I can't seem to print the sum? and the average I don't know how to.

What I have tried:

int main()
{
    int input = 5;

    while(input != 0)
    {
        printf("Enter 5 number (0 to stop): ");
        scanf("%i", &input);
    }

    int sum = input;
    printf("The sum is: %i", sum);

    return 0;
}

推荐答案

总和均值是简单的数学计算。记下执行此操作所需的操作,然后您可以将已编写的内容转换为代码。
Sum and average are simple mathematical calculations. Write down the operations necessary to do it, and you can then convert what you have written into code.


您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的cpde应该做什么,它不是找到错误,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


学习使用Patrice编写的调试器,你会发现你的错误和缺失的代码。



提示:

1.算上循环中的输入数量

2.你必须在循环中总结(因为该值已被下一个输入覆盖)

3.输出后的输入数量循环
Learn to use the debugger as Patrice wrote and you find your mistakes and missing code.

Tips:
1. count the numbers of input in the loop
2. you must sum up in the loop (because the value got overriden with the next input)
3. output of numbers of input after the loop


这篇关于如何打印5个数字的总和?平均?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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