我在课堂上有一个作业,要收集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] << " ";
}
发布于 2012-03-02 08:04:22
我看到了两件事:
for (int index = 0;index < MONTHS;index++) cout << index index <<“”<< nameindex << endl;
我添加了endl
,因为我觉得你会喜欢它的。
发布于 2012-03-02 08:11:08
您的问题是直接对降雨值进行排序,这意味着您会丢失有关每个月对应的值的信息。解决这个问题的一个好方法是传递一个索引数组并对该数组进行排序。换句话说,而不是
void sortArray(int values[], string name[], int MONTHS)
你就会有
void sortArray(int indices[], int values[], string name[], int MONTHS)
其思想是在main
函数中将indices
数组从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;
}
发布于 2012-03-02 09:26:30
似乎你最大的麻烦来自于你的数据结构设计。一个好的数据结构将使算法更容易实现。
下面是一个结构示例,该结构旨在将您正在跟踪的所有数据封装在一个位置:
#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;
}
https://stackoverflow.com/questions/9526069
复制相似问题