【发布时间】:2014-11-04 03:49:25
【问题描述】:
好吧,我咬咬牙,这个程序确实将数字从高到低排序但是它只会在数字按从高到低的顺序排列时进行排序;如果是零星的,则不会。这是我的意思的两个例子。
示例 1:12,11,10,9,8,7,6,5,4,3,2,1。
示例 2:32,25,24,31,10,11,15,16,8,19,18,5。
您会在示例 2 中看到一些数字是如何按顺序排列的,例如 32 25 24,而另一些则不是。这是我的主要问题。我的次要问题是垂直对齐文本,使其看起来整洁。我应该为此使用 setw left,right 吗?请提供反馈。
注意 1:我使用的 IDE 是代码块。
注意 2:请记住,用户为特定月份输入的任何数字都必须与每个数字垂直平行。
注 3:我很确定问题出在我的选择排序上。所以你真的应该看看函数 void selectionsort 因为它是执行所有排序的函数。我只是不知道我的排序哪里出错了。其他一切似乎都井然有序。
/* This program lets the user enter the total rainfall
for each month into an array of doubles. The program also
calculates and displays the total rainfall for a year, the average
monthly rainfall, and the months with the highest and lowest amounts.
The program also displays the list of months, sorted in order of
rainfall from highest to lowest.*/
/* This program does not accept negative numbers for monthly rainfall
figures.*/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void SelectionSort(string[], double[], int);// Function Protoype.
int main()
{
const int SIZE = 12; /* A constant integer that represent the total
amount of months in a year. */
double totalRainfallPerMonth[SIZE]; /* Loop this array to force the user
to enter variables for each
element in this array. */
double totalRainfallPerYear = 0; /* The total amount of rainfall
(in inches) per year. */
// An array of every month.
string monthArray[SIZE]={"January", "February", "March", "April", "May",
"June", "July","August", "September",
"October", "November", "December"};
double average; // A variable that holds the average monthly rainfall.
int i; // Will be used as a counter for any loop.
cout << fixed << showpoint << setprecision(2); // Set decimal notation.
for(i=0; i<=11; i++)
{
// Prompt the user to enter values.
cout << "Please enter the total rainfall(in inches) for ";
cout << monthArray[i] << ": ";
cin >> totalRainfallPerMonth[i];
while(totalRainfallPerMonth[i] < 0) /* If the user enters a negative
value */
{
cerr << "No negative values allowed. "; // Display error message.
cout << "Please try again. ";
cin >> totalRainfallPerMonth[i];
}
}
for(i=0; i<=11; i++)
{
// Calculate the total rainfall for a year.
totalRainfallPerYear += totalRainfallPerMonth[i];
}
// Display the total rainfall for a year.
cout << "\nThe total rainfall this year is " << totalRainfallPerYear;
cout << " inches of rain. " << endl;
// Calculate the average monthly rainfall.
average = totalRainfallPerYear / SIZE;
// Display the average
cout << "\nThe average monthly rainfall per month is ";
cout << average;
cout << " inches of rain. " << endl << endl << endl;
cout << "\n" << "Month " << "\t";
cout << " Rainfall(in inches)" << endl;
cout << "-----------------------------------";
SelectionSort(monthArray, totalRainfallPerMonth, SIZE); /* Call in the
function. */
return 0;
}
void SelectionSort(string month[], double rain[], int SIZE)
{
int i;
int j;
int min;
for (i = 0; i < SIZE - 1; i++)
{
min = i; // The intial subscript or the first element.
for (j = i + 1; j < SIZE; j++)
{
if (rain[j] > rain[min]) /* if this element is greater,
then it is the new minimum */
{
min = j;
// swap both variables at the same times
double tempDouble = rain[i];
rain[i] = rain[j];
rain[j] = tempDouble;
string tempString = month[i];
month[i] = month[j];
month[j] = tempString;
}
}
}
for(i=0; i<=11; i++)
{
/* Display the amount of rainfall per month from highest to
lowest */
cout << "\n" << month[i] << "\t" << rain[i] << endl;
}
}
【问题讨论】:
-
除非要重写
sort,否则我会使用struct RainFallForMonth { double rainfall; int month;};并使用自定义比较器执行std::sort。 -
我的一个朋友告诉我做结构,但我们还没有在课堂上学习结构。
标签: c++ arrays string sorting double