【问题标题】:Sorting both a string array and a double array from highest to lowest(parallel arrays) and aligning the text从最高到最低(并行数组)对字符串数组和双精度数组进行排序并对齐文本
【发布时间】: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


【解决方案1】:

实际上在您的选择排序实现中存在一个错误:您太早且太频繁地交换元素。

仅在您对整个剩余数组执行完全扫描并因此确定全局最小值之后(我保留该术语是为了与代码中的变量名称和 cmets 保持一致,尽管实际上您正在寻找对于最大值)您应该将剩余数组的第一个元素与最小元素进行一次交换。

更正后的代码(仅显示排序函数的主循环)应如下所示:

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[min];
    rain[min] = tempDouble;

    string tempString = month[i];
    month[i] = month[min];
    month[min] = tempString;
}

【讨论】:

  • 很好地发现了这个错误。我会确保不再犯同样的错误。
  • 另外,我应该将 min 更改为 max,因为正如您所说,我正在寻找数组每个元素中的最大值。
猜你喜欢
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
相关资源
最近更新 更多