好的,这是一项由于输出向后显示而被扣除的分配。我应该提示用户输入然后按降序显示输入,但我的显示有问题。我有两个数组,一年[]用于保存月份,一个month[totalMonths]
用于保存用户输入。当我排序并显示输入year[]
与月份不对应时,它是固定的。因此,例如,如果用户为Jan输入1,为2月输入2,为Mar输入3,则显示为;
Jan:3
2月:2
Mar:1
关于我如何能够将月份与显示器的正确输入相对应的任何想法?这是排序和显示功能:
void sortArray(double month[], string year[], int totalMonths)
{
int temp;
bool swap;
do
{
swap = false;
for(int count = 0; count < totalMonths - 1; count++)
{
if(month[count] < month[count + 1])
{
temp = month[count];
month[count] = month[count + 1];
month[count + 1] = temp;
swap = true;
}
}
} while(swap);
cout << "------------------------------------------------------------" << endl;
cout << "Here are the months rainfall statistics sorted from highest to lowest: " << endl;
for (int index = 0; index < totalMonths; index++)
cout << year[index] << "\t " << setw(5) << month[index] << endl;
}
这是我的string year[]
定义:
string year[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
答案 0 :(得分:2)
就像Blastfurnace所指出的那样,您必须对年份数组进行排序以匹配月份。 或者,如果不能,您可以创建一个小结构来表示月份数据。像这样:
typedef struct _monthData{
double data;
int monthIndex;
} monthData;
void sortArray(monthData month[], string year[], int totalMonths)
{
int temp;
bool swap;
do
{
swap = false;
for(int count = 0; count < totalMonths - 1; count++)
{
if(month[count].data < month[count + 1].data)
{
temp = month[count];
month[count] = month[count + 1];
month[count + 1] = temp;
swap = true;
}
}
} while(swap);
cout << "------------------------------------------------------------" << endl;
cout << "Here are the months rainfall statistics sorted from highest to lowest: " << endl;
for (int index = 0; index < totalMonths; index++)
cout << year[month[index].monthIndex] << "\t " << setw(5) << month[index] << endl;
}
此致
答案 1 :(得分:1)
您是否可以重新排列year
数组?在您的交换month
值的排序例程中,您可以交换year
数组中的相应值。
如果你不想改变year
数组,你可以添加一个间接级别。在month
和year
数组中定义索引数组并对索引进行排序。
int index[12] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
// inside your sort routine...
if(month[index[count]] < month[index[count + 1]])
{
temp = index[count];
index[count] = index[count + 1];
index[count + 1] = temp;
swap = true;
}
// print the arrays...
for (int count = 0; count < totalMonths; count++)
cout << year[index[count]] << "\t " << setw(5) << month[index[count]] << endl;
答案 2 :(得分:0)
只有您必须将month[count] < month[count + 1])
更改为month[count] > month[count + 1])
。因此,您的完整代码将在下面给出:
#include<iostream>
#include<string>
#include<string.h>
#include<iomanip>
using namespace std;
void sortArray(double month[], string year[], int totalMonths)
{
int temp;
bool swap;
do
{
swap = false;
for(int count = 0; count < totalMonths - 1; count++)
{
if(month[count] > month[count + 1])
{
temp = month[count];
month[count] = month[count + 1];
month[count + 1] = temp;
swap = true;
}
}
} while(swap);
cout << "------------------------------------------------------------" << endl;
cout << "Here are the months rainfall statistics sorted from highest to lowest: " << endl;
for (int index = 0; index < totalMonths; index++)
cout << year[index] << "\t " << setw(5) << month[index] << endl;
}
int main()
{
string year[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double month[] = {12,11,10,9,8,7,6,5,4,3,2,1};
sortArray(month,year,12);
return 0;
}