2017-12-11 8 views
-2

Ich habe ein Projekt, an dem ich gearbeitet habe und ich bin entsetzlich verloren, wenn es darum geht, wie Blasensortieren und alles andere ins Spiel kommt. Ich muss meine Ausgabe wie die Punktetabelle im Kommentarteil aussehen lassen. Ich habe einen Teil davon gemacht, bin aber verloren gegangen. Wenn es um Blasensortierung und leere Funktionen geht, bin ich etwas verloren. Ich weiß, dass Void-Funktionen nichts zurückgeben sollen, wenn sie nicht hauptsächlich aufgerufen werden, aber ich werde immer noch etwas verwirrt. Das und die Art, wie Arrays auch in Dinge hineinspielen. Kann jemand erklären, wie man meinen Code repariert? Im Moment versuche ich nur, die Punkte vom Größten zum Geringsten zu bekommen. Also muss meine Blasensortierung es verringern.Probleme beim Ändern der Ausgabe, um die Blasensortierung widerzuspiegeln

//****************************************************************************** 
    // 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 


} 
//------------------------------------------------------------------------------------------------------------------------------- 
+1

Hallo und willkommen bei SO. Ich habe Verständnis für Ihre Probleme, aber Sie müssen hier einige Regeln und Richtlinien befolgen. Ihre Frage ist ein wenig zu unklar und breit. Wie es steht, liest es als "hilf mir mit meinem Code". Wir würden gerne, aber Sie müssen ** spezifisch ** sein. Identifizieren Sie ein Problem, das Sie haben, und erstellen Sie ein [mcve] dafür und fragen Sie danach. Dann hat Ihre Frage die erwartete Ausgabe, aber es fehlt die tatsächliche Ausgabe, die Sie haben. Und dies führt zurück zum ersten Thema: Sie müssen ein klares spezifisches Problem stellen. – bolov

+2

"Leere Funktionen sollen nichts zurückgeben, wenn sie nicht hauptsächlich aufgerufen werden" Falsch. Sie sollen nichts zurückgeben. Zeitraum. Nein * außer * oder * außer * Klausel. – bolov

+1

sehr wichtig: Schalten Sie Ihre Compiler-Warnungen ein, und behandeln Sie sie vorzugsweise als Fehler, besonders seit Sie lernen. Sie erhalten einige sehr hilfreiche Diagnosen, wie zum Beispiel die Verwendung von unitären Variablen. – bolov

Antwort

0

Von Beginn Ihrer sort

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

ist es keine Erwähnung von club, wins, draws in Ihrer sort Umsetzung.

Ferner ist die erste Schleife hat undefiniertes Verhalten, in der erste Runde for (i = 0; i < j; ++i) hat j nicht initialisiert worden ist, so kann der Compiler die gesamte Funktion weglassen, oder tut etwas anderes es will.

Wenn Sie das beheben, dann ist in der inneren ersten Schleife j < j-i-1 immer falsch, weil i immer 0 oder höher ist.

Die zweite Schleife ist keine gültige Syntax, weil Sie den Namen points für den Schleifenindex verwenden und ihn dann wie ein Array verwenden. Sie starten es auch zwei über das Ende der Eingabe-Arrays hinaus, möchten Sie mit SIZE-1 starten, wenn Sie auf das Element unter points[index] zugreifen möchten.

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 
     } 
    } 
} 
+1

Wenn Sie diese Funktionssignaturen * nicht * verwenden müssen, sollten Sie wahrscheinlich ein 'struct result {std :: string club; int gewinnt; int zeichnet; int Punkte; } 'und operiere mit einer Sammlung von' Result's. Vorzugsweise 'std :: vector Ergebnisse;' – Caleth

Verwandte Themen