从最高到最低对字符串数组和双数组进行排序(并行数组)并对齐文本

Sorting both a string array and a double array from highest to lowest(parallel arrays) and aligning the text

本文关键字:数组 排序 并行 文本 对齐 字符串      更新时间:2023-10-16

好吧,我会咬一口,这个程序确实会从最高到最低对数字进行排序,但只有当数字按从高到低的顺序排列时,它才会进行排序;如果是偶发性的,就不会。这里有两个例子来说明我的意思。

示例1:12,11,10,9,8,7,6,5,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;
 }
}

事实上,在选择排序实现中存在一个错误:过早且频繁地交换元素。

只有在对整个剩余数组执行了全面扫描并因此确定了全局最小值之后(为了与代码中的变量名和注释保持一致,我保留了这个术语,尽管事实上你正在寻找最大值),你才应该用最小元素对剩余数组的第一个元素执行一次交换。

校正后的代码(只显示了排序功能的主循环)应该如下所示:

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;
}