【发布时间】:2018-11-21 12:00:32
【问题描述】:
编写一个程序,读取 12 个月中每个月的降雨量并输出该月和该月的降雨量。然后它应该输出降雨量最多的月份、降雨量最少的月份、全年降雨量和月平均降雨量。所有降雨量均应输出小数点后两位有效数字。
该程序应包含两个并行数组:包含月份名称的字符串类型月份,以及包含相应月份降雨量(以英寸为单位)的 double 类型的降雨量。
提示:请查看 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;
}
【问题讨论】:
-
不确定我在运行时做错了什么,我没有得到输出
-
这条线应该做什么
cin >> rainfall[SIZE];? -
你可以而且必须使用一个循环,而不是五个。
-
它应该在显示的输入中读取
-
@LouisCastillo 猜你想用降雨次数填充数组
rainfall?
标签: c++