首页 > 解决方案 > c++数组使用for循环输出降雨统计

问题描述

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

该程序应包含两个并行数组:包含月份名称的字符串类型月份,以及包含相应月份的降雨量(以英寸为单位)的双精度类型的降雨量。

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

程序应输出在 10 的字段宽度中右对齐的月份和在 6 的字段宽度中右对齐的相应降雨量。

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

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

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

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

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

图1:(输入)

3.2 .55 -1.2 -.9 2.2 .56 .24 .95 2.00 .35 5.9 1.1 2.8 .3

图 2:(输出)

   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.

这是我的代码。

#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;
}

标签: c++

解决方案


我已经修复了你的代码,并在我认为你离线的地方添加了评论。请根据您的输入进行尝试,最重要的是了解代码中所做的更改以及为什么这会解决您的代码中的问题。这样,您不仅可以应对/粘贴代码,还可以在此过程中学习。快乐编码!

#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;
}

推荐阅读