首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c++数组使用for循环输出雨量统计信息

c++数组使用for循环输出雨量统计信息
EN

Stack Overflow用户
提问于 2018-11-21 10:22:40
回答 3查看 2.4K关注 0票数 0

编写一个程序,读取12个月中每个月的降雨量,并输出该月的降雨量和该月的降雨量。然后,它应该输出降雨量最多的月份、降雨量最少的月份、一年的总降雨量和月平均降雨量。所有雨量应在小数点后2位有效数字输出。

程序应该包含两个并行数组:类型为string的month包含月份名称,类型为double的雨量包含相应月份的降雨量(英寸)。

提示:有关示例,请查看CS1336_Lect7c_Arrays_Compare_Parallel.pptx的幻灯片8到10。程序应该使用for循环读取每个月的降雨量。

该程序应输出字段宽度为10的月份右对齐,以及字段宽度为6的相应降雨量右对齐。

该程序应计算和显示全年的总降雨量和月平均降雨量。

该程序应计算最高和最低金额,并显示金额和相应的月份名称。

提示1:检查CS1336_Lect7c_Arrays_Compare_Parallel.pptx的幻灯片3和4,找到最低和最高的代码。

提示2:此外,您必须跟踪您发现最高和最低金额的索引。输出索引对应的月份名称。例如,如果字体rainfall3是降雨量最低的月份,您将打印字体month3作为相应的月份。

验证:不接受月度降雨量数字为负数。当输入如图1所示时,您的程序应生成如图2所示的输出。

图1:(输入)

代码语言:javascript
运行
复制
3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3

图2:(输出)

代码语言:javascript
运行
复制
   January  3.20
  February  0.55
     March  2.20
     April  0.56
       May  0.24
      June  0.95
      July  2.00
    August  0.35
 September  5.90
   October  1.10
  November  2.80
  December  0.30

The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.

这是我的代码。

代码语言:javascript
运行
复制
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
   const int SIZE = 12;
   string months[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   double rainfall [SIZE];
   double highest;
   double lowest;
   double total, average;

   cin >> rainfall[SIZE];

   for (int i = 0; i < SIZE; i++)
   {
      cout << right << setw(10) << months[SIZE] << right <<setw(6) << rainfall[i] << endl;
   }

   for ( int i = 0; i < SIZE; i++)
   {
      if (rainfall[i] > highest)
      {
         highest = rainfall[i];
      }
   }
   for (int i = 0; i < SIZE; i++)
   {
      if (rainfall[i] < lowest)
      {
         lowest = rainfall[i];
      }

   }
   for(int i = 0; i < SIZE; i++)
      {

        total = rainfall[0] + rainfall[1] + rainfall[2] + rainfall[3] + rainfall[4];
        cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
      }
      for(int i = 0; i << SIZE; i++)
      {
         average = total / SIZE;
         cout << "The average monthly rainfall for the year is " << average << " inches." << endl;
      }

   return 0;
}
EN

回答 3

Stack Overflow用户

发布于 2018-11-21 10:57:13

我已经修复了你的代码,并在我认为你离线的地方添加了评论。请尝试使用您的输入,最重要的是理解在代码中所做的更改,以及为什么这会在您的代码中修复问题。这样,你不仅可以复制/粘贴代码,还可以在这个过程中学习。祝你编码愉快!

代码语言:javascript
运行
复制
#include "stdafx.h"    
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
    const int SIZE = 12;
    string months[]{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    double rainfall[SIZE];
    double highest = 0.0;
    double lowest = 0.0;
    double total = 0.0, average = 0.0;

    double input;
    for (int i = 0; i < SIZE; ) //Dont increment i here since we want to ignore negative numbers.
    {
        cin >> input;
        cin.ignore(1000, '\n');
        if (input > 0.0)
        {
            rainfall[i] = input;

            //for the first element assign lowest and highest to same value.
            if (i == 0)
            {
                highest = input;
                lowest = input;
            }

            if (input > highest) highest = input;
            if (input < lowest) lowest = input;
            total += rainfall[i]; //You need to keep adding all the months rainfall in total. ? why did you add only four and that too in loop?
            i++; //if the number is non negative increment i.
        }
    }


    for (int i = 0; i < SIZE; i++)
    {
        cout << right << setw(10) << months[i] << right << setw(6) << rainfall[i] << endl;
    }

    average = total / SIZE;

    cout << "The total amount of rainfall for the year is " << total << " inches." << endl;
    cout << "The highest amount of rainfall for the year is " << highest << " inches." << endl;
    cout << "The lowest amount of rainfall for the year is " << lowest << " inches." << endl;
    cout << "The average monthly rainfall for the year is " << average << " inches." << endl;

    return 0;
}
票数 0
EN

Stack Overflow用户

发布于 2018-11-21 12:00:32

您的指示相当具体和充分。一次拿一个就行了。首先,您需要验证是否已将12月份的降雨数据读取到double数组中。无论是声明数组还是验证读取的降雨量数量,都需要一个常量。所以:

代码语言:javascript
运行
复制
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#define MONTHS 12   /* if you need a constant, #define one (or more) */

您可以选择在何处声明包含每个month名称的string数组。您可以将数组声明为main()的局部数组,也可以将名称声明为全局数组(可能更可取)。您不会更改每个月的名称,因此应将数组限定为const,例如

代码语言:javascript
运行
复制
int main (void) {

    double rainfall[MONTHS] = {0},  /* declare/initialize variables */
        sum = 0, min = 10000, max = 0, avg = 0;
    const string month[] =  { "January", "February", "March", "April",
                        "May", "June", "July", "August",
                        "September", "October", "November", "December" };
    int mo = 0, maxmo = 0, minmo = 0;

每当您读取循环中的多个输入时,您都希望基于有效输入控制循环,并确保在使用数据之前没有发生流错误(eofbit, failbit, badbit)。在您的示例中,只需一个读取循环就可以读取信息,计算与totalaverage一起使用的sum,以及确定maxmin以及每个数据发生的月份(maxmominmo)。(注意: min被初始化到足够大,以便它可以确定实际的最小值。初始化到0将不起作用)

代码语言:javascript
运行
复制
    while ((cin >> rainfall[mo])) {     /* while good input */
        if (rainfall[mo] > 0) {         /* validate positive rainfall */
            sum += rainfall[mo];        /* increment sum by rainfall */
            if (rainfall[mo] < min)     /* set min and minmo */
                min = rainfall[mo], minmo = mo;
            if (rainfall[mo] > max)     /* set max and maxmo */
                max = rainfall[mo], maxmo = mo;
            mo++;
        }
    }

现在,读取循环完成后,您可能希望验证您是否实际拥有12个月的降雨量数据,然后计算平均值,例如:

代码语言:javascript
运行
复制
    if (mo != MONTHS)   /* valdate 12 months of data read */
        cerr << "warning: only '" << mo << "' months data available.\n";

    avg = sum / (double)mo;

剩下的就是格式化输出了。除非您在随后的输出中对它们进行了更改,否则只需设置一次。您可以简单地设置:

代码语言:javascript
运行
复制
    cout << fixed << setprecision(2);   /* set fixed and precision(2) */

浮点输出现在将输出两位小数。剩下的就是输出所需的信息,例如

代码语言:javascript
运行
复制
    for (int i = 0; i < mo; i++)        /* output monthly data */
        cout << setw(10) << month[i] << setw(6) << rainfall[i] << '\n';

    /* output statistics */
    cout << "\nThe most rainfall was " << max << " inches in "
            << month[maxmo] << ".\n";
    cout << "The least rainfall was " << min << " inches in "
            << month[minmo] << ".\n";
    cout << "The total amount of rainfall for the year is "
            << sum << " inches.\n";
    cout << "The average monthly rainfall for the year is "
            << avg << " inches.\n";

(注意:上面实际上不需要多个cout调用,您可以简单地使用一个cout并将所有信息串在一起)

总而言之,您可以执行如下操作:

代码语言:javascript
运行
复制
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#define MONTHS 12   /* if you need a constant, #define one (or more) */

int main (void) {

    double rainfall[MONTHS] = {0},  /* declare/initialize variables */
        sum = 0, min = 10000, max = 0, avg = 0;
    const string month[] =  { "January", "February", "March", "April",
                        "May", "June", "July", "August",
                        "September", "October", "November", "December" };
    int mo = 0, maxmo = 0, minmo = 0;

    while ((cin >> rainfall[mo])) {     /* while good input */
        if (rainfall[mo] > 0) {         /* validate positive rainfall */
            sum += rainfall[mo];        /* increment sum by rainfall */
            if (rainfall[mo] < min)     /* set min and minmo */
                min = rainfall[mo], minmo = mo;
            if (rainfall[mo] > max)     /* set max and maxmo */
                max = rainfall[mo], maxmo = mo;
            mo++;
        }
    }

    if (mo != MONTHS)   /* valdate 12 months of data read */
        cerr << "warning: only '" << mo << "' months data available.\n";

    avg = sum / (double)mo;

    cout << fixed << setprecision(2);   /* set fixed and precision(2) */
    for (int i = 0; i < mo; i++)        /* output monthly data */
        cout << setw(10) << month[i] << setw(6) << rainfall[i] << '\n';

    /* output statistics */
    cout << "\nThe most rainfall was " << max << " inches in "
            << month[maxmo] << ".\n";
    cout << "The least rainfall was " << min << " inches in "
            << month[minmo] << ".\n";
    cout << "The total amount of rainfall for the year is "
            << sum << " inches.\n";
    cout << "The average monthly rainfall for the year is "
            << avg << " inches.\n";
}

示例使用/输出

代码语言:javascript
运行
复制
$ echo "3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3" | \
./bin/rainfall
   January  3.20
  February  0.55
     March  2.20
     April  0.56
       May  0.24
      June  0.95
      July  2.00
    August  0.35
 September  5.90
   October  1.10
  November  2.80
  December  0.30

The most rainfall was 5.90 inches in September.
The least rainfall was 0.24 inches in May.
The total amount of rainfall for the year is 20.15 inches.
The average monthly rainfall for the year is 1.68 inches.

仔细检查一下,如果你还有其他问题,请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2018-11-21 10:40:45

  1. Variable SIZE没有任何意义,因为您知道年份总是有12个月。它只会浪费内存。如果您想使用某种类型的变量,请使用#define SIZE 12。将其放在#include部分的顶部。
  2. Use只能对所有任务使用一个循环。
  3. 最后两个循环也没有意义。如果你想计算总降雨量,在loop.
  4. cin >> rainfall[SIZE];中放入total += rainfall[i];也是没有意义的,这是不正确的。为什么?使用这行代码,你可以将值放入rainfall[12];,但rainfall的最后一个索引是11而不是12。要用值填充rainfall,请将cin >> ranfall[i];放入循环中。你必须使用两个不同的循环来填充rainfall和计算所有其他东西。
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53404460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档