C++ C++;需要一个家庭作业程序的帮助,该程序从一个文件中读取一个双精度列表,并为每个双精度添加一个字符串
我在这个家庭作业计划上遇到了麻烦,现在我被困在了我应该做的事情上。程序从一个文件中读取12个双精度的列表。每个双精度代表一年中12个月的总降雨量(英寸),按月份顺序排列。12个双精度应写入一个数组。要求如下 这是我现在拥有的代码,我不知道需要在函数声明C++ C++;需要一个家庭作业程序的帮助,该程序从一个文件中读取一个双精度列表,并为每个双精度添加一个字符串,c++,c++11,C++,C++11,我在这个家庭作业计划上遇到了麻烦,现在我被困在了我应该做的事情上。程序从一个文件中读取12个双精度的列表。每个双精度代表一年中12个月的总降雨量(英寸),按月份顺序排列。12个双精度应写入一个数组。要求如下 这是我现在拥有的代码,我不知道需要在函数声明displayRainsion、bubbleSort、和swap中添加哪些内容,还需要在main中添加哪些内容: // Headers #include <iostream> // cout, cin #include <
displayRainsion
、bubbleSort
、和swap
中添加哪些内容,还需要在main中添加哪些内容:
// Headers
#include <iostream> // cout, cin
#include <cstdlib> // exit()
#include <string> // strings
#include <fstream> // file processing
#include <iomanip> // stream manipulation
using namespace std;
// Global variables
const int MAX_MONTHS = 12; // months for rainfall
const string FILENAME = "TotalMonthlyRainfall2014.txt"; // The name of the file that is read
// Function declarations
int loadMonthlyRainfall (double rain[MAX_MONTHS], string fileName, int maxMonths);
void displayRainfall(string months[MAX_MONTHS], double rain[MAX_MONTHS], int monthCount);
void bubbleSort(double rain[MAX_MONTHS], int size);
void swap(int& a, int& b);
int main()
{
int monthCount = 0;
string months[MAX_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rain[MAX_MONTHS];
loadMonthlyRainfall(rain, FILENAME, MAX_MONTHS);
// Make sure we place the end message on a new line
cout << endl;
// The following is system dependent. It will only work on Windows
system("PAUSE");
return 0;
}
int loadMonthlyRainfall(double rain[MAX_MONTHS], string fileName, int maxMonths)
{
ifstream inFile; // Input file stream
int numMonths = 0;
// Open file
inFile.open(fileName);
if (!inFile)
{
throw "Can not open file";
}
//
for (int r = 0; r < MAX_MONTHS; r++)
{
inFile >> rain[r];
}
// Close the file
inFile.close();
for (int r = 0; r < MAX_MONTHS; r++)
{
cout << rain[r] << " ";
}
cout << endl;
return numMonths;
}
void displayRainfall(string months[MAX_MONTHS], double rain[MAX_MONTHS], int monthCount)
{
}
void bubbleSort(double rain[MAX_MONTHS], int size)
{
}
void swap(int& a, int& b)
{
}
调用泡泡运动(降雨,最大月数)代码>在main
中进行以下更改:
void displayRainfall(string months[MAX_MONTHS], double rain[MAX_MONTHS], int monthCount)
{
//title
cout << "Monthly Rainfall for 2014" << endl;
//print minimum rainfall
cout << "Minimum: " << months[MAX_MONTHS-1] << " "<< rain[MAX_MONTHS-1] << endl;
//print maximum rainfall
cout << "Maximum: " << months[0] << " "<< rain[0] << endl;
//get and print average rainfall
double avg = 0.0;
for (int i = 0; i < MAX_MONTHS; i++)
{
avg+=rain[i];
}
avg = avg/MAX_MONTHS;
cout << "Monthly Average for Year: " << avg << endl;
//print ordered rainfall by month
cout << "Sorted by Total Rainfall" << endl;
cout << "Month" << setw(10) << "Inches" << setw(10) << endl;
for (int i = 0; i < MAX_MONTHS; i++)
{
cout << setw(10) <<months[i] << setw(10) << rain[i] << endl;
}
}
void bubbleSort(double rain[MAX_MONTHS], int size)
{
string months[MAX_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
//sort rainfall descending
for (int i = 1; i < size; i++)
{
for (int j = 0; j < size - i; j++)
{
if (rain[j] < rain[j+1])
{
swap(rain[j], rain[j+1]);
swap(months[j], months[j+1]);
}
}
}
//called inside bubbleSort because months and rain are sorted in scope
displayRainfall(months, rain, 0);
}
void swap(int& a, int& b)
{
int& temp = a;
a = b;
b = temp;
}
void显示降雨量(字符串月份[最大月数]、双倍降雨[最大月数]、整数月数)
{
//头衔
难道这就是我现在掌握的代码,我不知道该怎么办:——你到底迷路了什么?只是说“我迷路了”不是一个重点问题。来自:要求家庭作业帮助的问题必须包括您迄今为止为解决问题所做工作的总结,以及您解决问题的困难描述。除非您向陌生人询问驾驶方向,否则我迷路不是问题描述。。
void displayRainfall(string months[MAX_MONTHS], double rain[MAX_MONTHS], int monthCount)
{
//title
cout << "Monthly Rainfall for 2014" << endl;
//print minimum rainfall
cout << "Minimum: " << months[MAX_MONTHS-1] << " "<< rain[MAX_MONTHS-1] << endl;
//print maximum rainfall
cout << "Maximum: " << months[0] << " "<< rain[0] << endl;
//get and print average rainfall
double avg = 0.0;
for (int i = 0; i < MAX_MONTHS; i++)
{
avg+=rain[i];
}
avg = avg/MAX_MONTHS;
cout << "Monthly Average for Year: " << avg << endl;
//print ordered rainfall by month
cout << "Sorted by Total Rainfall" << endl;
cout << "Month" << setw(10) << "Inches" << setw(10) << endl;
for (int i = 0; i < MAX_MONTHS; i++)
{
cout << setw(10) <<months[i] << setw(10) << rain[i] << endl;
}
}
void bubbleSort(double rain[MAX_MONTHS], int size)
{
string months[MAX_MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
//sort rainfall descending
for (int i = 1; i < size; i++)
{
for (int j = 0; j < size - i; j++)
{
if (rain[j] < rain[j+1])
{
swap(rain[j], rain[j+1]);
swap(months[j], months[j+1]);
}
}
}
//called inside bubbleSort because months and rain are sorted in scope
displayRainfall(months, rain, 0);
}
void swap(int& a, int& b)
{
int& temp = a;
a = b;
b = temp;
}