【问题标题】:Trouble with changing the output to reflect bubble sorting更改输出以反映冒泡排序的问题
【发布时间】:2018-05-24 13:39:11
【问题描述】:

我有一个我一直在做的项目,当谈到冒泡排序和其他一切如何发挥作用时,我非常迷茫。我必须让我的输出看起来像评论部分的分数表。我已经完成了一部分,但迷路了。当谈到冒泡排序和 void 函数时,我有点迷茫。我知道 void 函数不应该返回任何东西,除非在 main 中调用,但我仍然对此感到有些困惑。那以及数组发挥作用的方式也是如此。谁能解释如何修复我的代码?现在我只是想让分数从最高到最低显示。所以我的冒泡排序需要让它减少。

   //******************************************************************************
    // Programmer:
    // Course:
    // Program:
// Date:
// Description: This program generates a standings report for the Premier League as of Nov 2017.
//------------------------------------------------------------------------------
//
// Premier League, Nov 26 2017
// Club                      Pts   Wins  Draws
// -----------------------------------------
//  1 Manchester City     37    12   1
//  2 Manchester United   29    9    2
//  3 Chelsea             26    8    2
//  4 Arsenal             25    8    1
//  :
//  :
// 18 West Ham            10    2    4
// 19 Swansea              9    2    3
// 20 Crystal Palace       8    2    2
//
//******************************************************************************
#include <iostream>
#include <iomanip>    // for setw()
using namespace std;

// prototypes
void computePoints(int wins[], int draws[], int points[], int size);
// Receives the number of wins and draws for each club and computes their total points.

void sort(int points[], string club[], int wins[], int draws[], int size);
// Sorts the given arrays based on the points into decreasing order of points.

void swap(int & x, int & y);
// Swaps the integers x and y.

void swap(string & x, string & y);
// Swaps the strings x and y.

void printStandings(string club[], int wins[], int draws[], int points[], int size);
// Prints a table for the current standings.
//-------------------------------------------------------------------------------------------------------------------------------
int main()
{
  const int SIZE = 20;    // number of clubs
  string club[SIZE] = {"Arsenal", "Bournemouth", "Brighton", "Burnley", "Chelsea", "Crystal Palace", "Everton", "Huddersfield", "Leicester", "Liverpool", "Manchester City", "Manchester United", "Newcastle", "Southampton", "Stoke City", "Swansea City", "Tottenham", "Watford", "West Bromwich", "West Ham"};
  int wins[SIZE] =  {8, 4, 4, 6, 8, 2, 3, 4, 3, 6, 12, 9, 4, 4, 3, 2, 7, 6, 2, 2};
  int draws[SIZE] = {1, 2, 4, 4, 2, 2, 3, 3, 5, 5,  1, 2, 2, 4, 4, 3, 3, 3, 5, 4 };
  int points[SIZE];

  // compute the points for each club (function call)
  computePoints(wins, draws, points, SIZE);
      for (int i = 0; i < SIZE; i++)
    { 
      cout << points[i] << endl;
    }
cout << endl;  
  // sort all club data into decreasing order of points (function call)
sort(points, club, wins, draws, SIZE);
     for (int i = 0; i < SIZE; i++)
    { 
      cout << points[i] << endl;
    }
  // print the standings (function call)
// printStandings(points, club, wins, draws, SIZE);
  return 0;
}
//-------------------------------------------------------------------------------------------------------------------------------
// implementation
void computePoints(int wins[], int draws[], int points[], int SIZE)
{

  for (int i = 0; i < SIZE; i++)
 {

  points[i] = (wins[i] * 3) + draws[i] * 1;

 }


}
//-------------------------------------------------------------------------------------------------------------------------------
void sort(int points[], string club[], int wins[], int draws[], int SIZE)
{
  // bubble sort algorithm (see lesson-24)
  // Note: when swapping points[i] and points[i+1], the same elements in arrays club, wins, and
  // draws must be swapped. 

int i, j;
    for (i = 0; i < j; ++i)
    {
        for (j = 0; j < j-i-1; ++j)
        {
            // Comparing consecutive data and switching values if value at j > j+1.
            if (points[j] > points[j+1])
            {
                points[j] = points[j]+points[j+1];
                points[j+1] = points[j]-points[j + 1];
                points[j] = points[j]-points[j + 1];
            }
        }
        // Value at j-i-1 will be maximum of all the values below this index.
    } 
    for(int points = SIZE + 1; points > 0; points--);    
    { 
      for(int i = 0;  i > points[i];  i++)  //make one pass & compare adjacent elements      
      { 
        if(points[i] >= points[i+1])  //if adjacent pairs are out of order, swap them.   

          swap(points[i], points[i+1]);      
      }   
    } 
}
//-------------------------------------------------------------------------------------------------------------------------------
void swap(int & x, int & y)
{
  int temp = x; 
  x = y;
  y = temp;

}
//-------------------------------------------------------------------------------------------------------------------------------
void swap(string & x, string & y)
{
 string temp = x;
 x = y;
 y = temp;
}
//-------------------------------------------------------------------------------------------------------------------------------
void printStandings(string club[], int wins[], int draws[], int points[], int SIZE)
{
// output the heading
cout << "Premier League, Nov 26 2017" << endl;

// output the corresponding elements of all four arrays


}
//-------------------------------------------------------------------------------------------------------------------------------

【问题讨论】:

  • 您好,欢迎来到 SO。我对您的问题表示同情,但您需要在此处遵循一些关于 SO 的规则和准则。你的问题有点太不清楚和宽泛了。就目前而言,它读作“帮助我处理我的代码”。我们很乐意,但您需要具体。确定您遇到的一个问题并为它创建一个minimal reproducible example 并询问它。然后您的问题具有预期的输出,但缺少您的实际输出。这又回到了第一个问题:你需要一个明确的具体问题。
  • "除非在 main 中调用,否则 void 函数不应返回任何内容" False。他们不应该返回任何东西。时期。没有 unlessexcept 子句。
  • 非常重要:打开编译器警告,最好将它们视为错误,尤其是在您学习的时候。您将获得一些非常有用的诊断信息,例如使用单元化变量。
  • 第二个非常重要:这是一个很容易解决的问题,或者至少可以通过调试大大缩小范围。请花时间学习如何使用调试器。它非常有帮助,您是否可以节省很多时间盯着代码看。我不能足够强调能够调试自己的代码的重要性。早点学习吧!

标签: c++ arrays function bubble-sort


【解决方案1】:

从你的sort开始

// Note: when swapping points[i] and points[i+1], the same elements in arrays club, wins, and
// draws must be swapped. 

在您的sort 实现中没有提及clubwinsdraws

此外,第一个循环有未定义的行为,第一次循环for (i = 0; i &lt; j; ++i)j 还没有被初始化,所以编译器可以省略整个函数,或者做任何事情否则它想要

如果你解决了这个问题,那么在内部的第一个循环中,j &lt; j-i-1 始终为 false,因为 i 始终为 0 或更高。

第二个循环不是有效的语法,因为您将名称 points 重复用于循环索引,然后像数组一样使用它。 您还可以在输入数组末尾 两个 处开始它,如果要访问位于 points[index] 的元素,则需要以 SIZE-1 开头。

void sort(int points[], string club[], int wins[], int draws[], int SIZE)
{
  // bubble sort algorithm (see lesson-24)
  // Note: when swapping points[i] and points[i+1], the same elements in arrays club, wins, and
  // draws must be swapped. 
    for (int i = SIZE - 1; i; --i) // stops when i is 0, as only 0 converts to false
    {
        for (int j = 0; j < i; ++j) // loop from 0 to i
        {
            // do comparison and swap all the necessary values
        }
    }
}

【讨论】:

  • 除非您必须使用这些函数签名,否则您可能应该使用struct result { std::string club; int wins; int draws; int points; } 并对results 的集合进行操作。最好是std::vector&lt;result&gt; results;
猜你喜欢
  • 2016-02-18
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
相关资源
最近更新 更多