我在课堂上有一个任务,收集12个月的降雨量并按从高到低的顺序显示。我可以显示数值但不显示月份。这是我到目前为止的代码:
#include <iostream>
#include <iomanip>
using namespace std:
int rainfall(int [], string [], int);
void sortArray(int [], string [], int);
int main()
{
//Program info
cout <<"Enter rainfall for each month and the progran will display the rainfall," <<endl;
cout <<"sorted in order of rainfall, from highest to lowest.\n" <<endl;
int const MONTHS = 12;
int values[MONTHS];
string name[MONTHS] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"};
rainfall(values, name, MONTHS);
sortArray(values, name, MONTHS);
system ("pause");
return 0;
}
int rainfall(int values[], string name[], int MONTHS)
{
//Gather rainfall number from user
cout << "\nPlease enter the amount of rainfall for each month.\n " << endl;
for(int month = 0; month <= MONTHS -1; month++)
{
cout << name[month] << ": ";
cin >> values[month];
//Validation to ensure a negative number is not entered
if(values[month] < 0)
{
cout << "Negative rainfall is not possible. Re-enter number." << endl;
cin >> values[month];
}
}
return values[MONTHS];
}
void sortArray(int values[], string name[], int MONTHS)
{
int temp;
bool swap;
do
{
swap = false;
for(int count = 0; count < MONTHS - 1; count++)
{
if(values[count] < values[count + 1])
{
temp = values[count];
values[count] = values[count + 1];
values[count + 1] = temp;
swap = true;
}
}
} while(swap);
cout << "\nHere are the months sorted from highest to lowest:\n";
for (int index = 0; index < MONTHS; index++)
cout << values[index] << " ";
}
答案 0 :(得分:2)
我看到的两件事:
要打印出几个月:
for (int index = 0; index < MONTHS; index++)
cout << values[index] << " " << name[index] << endl;
我添加了endl
,因为我认为你会喜欢它。
答案 1 :(得分:0)
您的问题是您直接对降雨量值进行排序,这意味着您丢失了与每个月对应的值的信息。解决它的一个好方法是传递一个索引数组并对该数组进行排序。换句话说,而不是
void sortArray(int values[], string name[], int MONTHS)
你会有
void sortArray(int indices[], int values[], string name[], int MONTHS)
我们的想法是在indices
函数中将main
数组从0初始化为11:
int indices[MONTHS];
for (int month = 0; month < MONTHS; month++)
{
indices[MONTHS] = month;
}
当然,在对indices
数组进行排序时,您需要比较这些索引指向的实际值,而不是
if(values[count] < values[count + 1])
{
temp = values[count];
values[count] = values[count + 1];
values[count + 1] = temp;
swap = true;
}
你会有
if(values[indices[count]] < values[indices[count + 1]])
{
temp = indices[count];
indices[count] = indices[count + 1];
indices[count + 1] = temp;
swap = true;
}
以防万一不清楚,这里有一个解释我们正在做什么以及为什么我们这样做。您不是对降雨值列表进行排序,而是对月列表进行排序,并通过该列表间接访问值(和月份名称)。换句话说,你说的是:“我已经获得了1月,2月,...,12月的价值,现在对它们进行排序”,一旦你完成排序,结果就像是“好的,几个月从最低降雨量到最高降雨量的正确顺序是2月,5月,12月,6月,......,8月“。
那么,你如何打印结果呢?
for (int index = 0; index < MONTHS; index++)
{
cout << "Rainfall for " << name[indices[index]] << " was " << values[indices[index]] << endl;
}
答案 2 :(得分:0)
似乎您最大的麻烦来自您的数据结构设计。一个好的数据结构将使算法更容易实现。
以下是一个结构示例,旨在将您要跟踪的所有数据封装在一个位置:
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
// Structs are useful for combining related data.
struct Month
{
enum
{
JAN, FEB, MAR, APR, MAY, JUN, JUL,
AUG, SEP, OCT, NOV, DEC, NUM_MONTHS
};
const char *name;
int rainfall;
Month(const char *name)
{
this->name = name;
rainfall = 0;
}
bool operator<(const struct Month& other) const
{
return (rainfall < other.rainfall);
}
};
int main()
{
// Static struct initialization avoids the hassle of working with pointers.
struct Month months[Month::NUM_MONTHS] =
{
Month("Jan"),
Month("Feb"),
Month("Mar"),
Month("Apr"),
Month("May"),
Month("Jun"),
Month("Jul"),
Month("Aug"),
Month("Sep"),
Month("Oct"),
Month("Nov"),
Month("Dec")
};
// Enums can be used for clearer access before sorting array.
months[Month::JAN].rainfall = 7;
months[Month::FEB].rainfall = 2;
months[Month::MAR].rainfall = 5;
months[Month::APR].rainfall = 3;
// Months can be easily swapped in the array.
// Be cautious that this does break the Enum associations.
struct Month tmp = months[0];
months[0] = months[1];
months[1] = tmp;
// If you can use the STL, sorting is extremely easy.
vector<struct Month> months_v(months, months + Month::NUM_MONTHS);
sort(months_v.begin(), months_v.end());
vector<struct Month>::iterator it;
for (it = months_v.begin(); it != months_v.end(); it++)
printf("%s\t%d\n", it->name, it->rainfall);
return 0;
}