|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Selection sorting Arrays.
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Folding Fanatic
![]() |
Here I am again, and today I'm trying to figure out how to apply the selection sort algorithm to a group of arrays that I have.
-1st array consists of 30 names -2,3,and 4 consist of separate statistics relating to each name. I have to sort the names alphabetically, A-Z, while simultaneously swapping the corresponding stats from the other three arrays. I've barely hobbled through the rest of my arrays assignment, and this one has me stumped. Can anyone help me? Here is the rest of the program that I've developed so far. Function 3 is where I need help. Code:
#include <iostream>
#include <iomanip>
#include <ctype.h>
#include <fstream>
using namespace std;
int buildArrays(int[], int[], int[]);
void printArrays( string[], int[], int[], int[], int);
void sortArrays( string[],int[], int[], int[], int);
#define NUM_PLAY 30
int main()
{
int goals[NUM_PLAY];
int assists[NUM_PLAY];
int shots[NUM_PLAY];
string players[NUM_PLAY] = { "Martin Havlat", "Patrick Kane", "Jonathan Toews",
"Kris Versteeg", "Brian Campbell", "Andrew Ladd",
"Dave Bolland", "Patrick Sharp", "Duncan Keith",
"Cam Barker", "Dustin Byfuglien", "Brent Seabrook",
"Troy Brouwer", "Sammy Pahlsson", "Colin Fraser",
"Ben Eager", "Matt Walker", "Adam Burish",
"Aaron Johnson", "Niklas Hjalmarsson", "Brent Sopel",
"Jack Skille", "Jordan Hendry", "Pascal Pelletier",
"Tim Brent", "Jacob Dowell" };
int numberOfPlayers = 0;
numberOfPlayers = buildArrays(goals, assists, shots);
printArrays(players, goals, assists, shots, numberOfPlayers);
sortArrays(players, goals, assists, shots, numberOfPlayers);
system ("pause");
return 0;
}
//Function 1 constructs the arrays
int buildArrays(int goals[], int assists[], int shots[])
{
ifstream inFile;
inFile.open( "averages.txt" );
if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}
int num;
int count = 0;
while( inFile )
{
inFile >> num;
goals[count] = num;
inFile >> num;
assists[count] = num;
inFile >> num;
shots[count] = num;
++count;
}
inFile.close();
return count;
}
//Function 2 prints out the data
void printArrays( string players[], int goals[], int assists[], int shots[], int numberOfPlayers)
{
cout<<"\nChicago Blackhawks 2008-2009 Player Stats";
cout<<"\nPlayer Goals Assists Points Shots Shooting%";
for(int count = 0; count<numberOfPlayers; ++count) {
cout<<players[count]
<<setw(5)
<<goals[count]
<<setw(3)
<<assists[count]
<<setw(3)
<< goals[count] + assists[count]
<<setw(3)
<<shots[count]
<<setw(3)
<<goals[count] / shots[count] * 100;
}
}
//Function 3 sorts in ascending order based on names
void sortArrays( string players[], int goals[], int assists[], int shots[], int numberOfPlayers)
{
//!!!!Selection Sort Function Here!!!!
cout<<"\nChicago Blackhawks 2008-2009 Player Stats";
cout<<"\nPlayer Goals Assists Points Shots Shooting%";
for(int count = 0; count<numberOfPlayers; ++count) {
cout<<players[count]
<<setw(5)
<<goals[count]
<<setw(3)
<<assists[count]
<<setw(3)
<< goals[count] + assists[count]
<<setw(3)
<<shots[count]
<<setw(3)
<<goals[count] / shots[count] * 100;
}
}
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Folding Fanatic
![]() |
Code:
int i, j, min;
for (i = 0; i < (NUM_PLAY-1); i++)
{
min = i;
for (j = (i+1); j < NUM_PLAY; j++) {
if(players[j] < players[min]) {
min = j;
}
}
if (i != min) {
string swap = players[i];
players[i] = players[min];
players[min] = swap;
}
}
|
|||||||||||||
|
|
|
|
|
#3 (permalink) |
|
Case Modder
![]() |
You just need to add a couple more swaps for the other arrays (using the same indexes 'i' and 'min').
Oh, and change NUM_PLAY to numberOfPlayers.
__________________
Rich Custom Wooden Case Builder
Last edited by Spotswood : 3 Weeks Ago at 07:12 PM |
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Folding Fanatic
![]() |
Could you elaborate a bit more? I'm still kinda lost
|
|||||||||||||
|
|
|
|
|
#5 (permalink) |
|
Case Modder
![]() |
Code:
if (i != min) {
string swap = players[i];
players[i] = players[min];
players[min] = swap;
int stat = goals[i];
goals[i] = goals[min];
goals[min] = stat;
stat = assists[i];
assists[i] = assists[min];
assists[min] = stat;
}
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
Folding Fanatic
![]() |
Code:
int i, j, min;
for (i = 0; i < (numberOfPlayers-1); i++)
{
min = i;
for (j = (i+1); j < numberOfPlayers; j++) {
if(players[j] < players[min]) {
min = j;
}
}
if (i != min) {
string swap = players[i];
players[i] = players[min];
players[min] = swap;
int swap2 = goals[i];
goals[i] = goals[min];
goals[min] = swap2;
int swap3 = assists[i];
assists[i] = assists[min];
assists[min] = swap3;
int swap4 = shots[i];
shots[i] = shots[min];
shots[min] = swap4;
}
|
|||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|