如何计算总和的总和和平均值,用户输入

问题描述 投票:0回答:3
循环?

#include <iostream> using namespace std; #include <iomanip> int main(int argc, const char * argv[]) { int x; double avg = 0.0; int count = 0; int sum = 0; // ask user for input cout << ("Welcome to the greatest calculator!\n"); cout << ("Please enter 10 integers seperated by spaces \n"); do { std::cin >> x; sum = sum + x; count = count + 1; } while (count < 10); // calculate average avg = sum/10.0; // output average cout << fixed; cout << "For all 10 numbers the sum is " << sum << "." "The average is " << setprecision (2) << sum/10.0 << ".\n"; return 0; }

输出应该看起来像这样。

Please enter 10 integers separated by spaces:
1 -1 45 17 28 -2 0 9 -14 11

Upon our intelligent calculations, here is the result:
+ There are 7 positive numbers, sum = 111.00 and average = 15.86
+ There are 3 negative numbers, sum = -17.00 and average = -5.67
+ For all 10 numbers, sum = 94.00 and average = 9.40

您可以这样做(注意评论):

#include <iostream> int main(void) { // Declaration and initialization of the required variables float cPositive = 0.0f; float cNegative = 0.0f; int it = 0; std::cout << "Enter 10 numbers (floating point assignable): \n"; // Looping till 10 iterations do { float temp; std::cin >> temp; // If the number is greater than zero, i.e. (+ve) then cPositive sums up // otherwise, cNegative if (temp > 0) cPositive += temp; else if (temp <= 0.0f) cNegative -= temp; } while (++it < 10); // Increment and comparison together // Final results std::cout \ << "Sum of positive: " << cPositive << std::endl << "Sum of negative: -" << cNegative << std::endl; return 0; }

简单的测试案例:
c++
3个回答
0
投票

Enter 10 numbers (floating point assignable): 10.5 -1.5 2.2 5.5 -3.8 -99.3 10 4.5 -1.0 0 Sum of positive: 32.7 Sum of negative: -105.6

,如果您想查看平均值,请声明两个变量,即两者最初为零的变量。之后,当发生正数或负数时,只需增加
pos

neg,然后分别与

pos
neg

分别分开。


下面的代码产生以下输出:
cPositive
请阅读评论,因为它们实际上是详细的答案。
cNegative
    
使用两个变量

$ ./main The (sum, avg) of negative integers = (-15, -5) The (sum, avg) of positive integers = (6, 2) The (sum, avg) of all numbers = (-9, -1.5)

0
投票
#include <array> #include <iostream> int main() { // For convenience, keep the numbers in an std::array. std::vector is // equally convenient. std::array<int, 6> integers { 1, -4, 2, -5, 3, -6 }; // Define variables that store the sums and the counts. int positiveSum = 0; int positiveCnt = 0; int negativeSum = 0; int negativeCnt = 0; // Iterate over the numbers taking one of them at a time. int i = 0; while (i < integers.size()) { int number = integers[i]; // Is the number positive?... if (number >= 0) { // ... it is - add it to the positive sum and increment the count. positiveSum += number; ++positiveCnt; } // The number is not positive, so it must be negative... else { // ... add it to the negative sum and increment the count. negativeSum += number; ++negativeCnt; } // Get ready for the next number. ++i; } // Time to print out the results. // Note that before we calculate the average, we have to cast at least one // of the terms of the division to floating point type. Otherwise the // division will be done with integers where the result is also an integer // (e.g. 3 / 2 -> 1). // Only affter the casting you will be getting expected answers // (e.g. double(3) / 2 -> 1.5). std::cout << "The (sum, avg) of negative integers = (" << negativeSum << ", " << double(negativeSum) / negativeCnt << ")" << std::endl; std::cout << "The (sum, avg) of positive integers = (" << positiveSum << ", " << double(positiveSum) / positiveCnt << ")" << std::endl; std::cout << "The (sum, avg) of all numbers = (" << negativeSum + positiveSum << ", " << double(negativeSum + positiveSum) / (negativeCnt + positiveCnt) << ")" << std::endl; }

在循环中尝试一个条件
int negativeVar=0
检测给定数字为负或正值。

然后分别添加所有正值和负值并使平均值。

PositiveVar=0

0
投票

© www.soinside.com 2019 - 2025. All rights reserved.