Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 3 Weeks Ago   #1 (permalink)
Folding Fanatic
 
tofunater's Avatar
 
intel nvidia

Join Date: Sep 2006
Location: Dekalb, IL
Posts: 2,473

Rep: 203 tofunater is acknowledged by manytofunater is acknowledged by manytofunater is acknowledged by many
Unique Rep: 176
Folding Team Rank: 280
Trader Rating: 24
Default Selection sorting Arrays.

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

Quote:
Originally Posted by iandh View Post
People like this know deep down inside that they are absolutely worthless, so they must constantly come up with new ways to give themselves worth, otherwise they may be tempted to end it all one day by OD'ing on Care-Bears marathons.

System: Hedonism, Stanford Style
CPU
i7 920 D0
Motherboard
Asus P6T6 WS revolution
Memory
6 gigs G-skill
Graphics Card
Tri-sli 260's
Hard Drive
620aaks
Sound Card
on board
Power Supply
Zalman 850
Case
hardwood techstation(soon)
CPU cooling
GTZ/bix 360/355 (soon)
GPU cooling
water (soon)
OS
Vista 64
Monitor
22" Samsung 225BW
Overclock.net - 2009 Chimp Challenge Champions
tofunater is offline I fold for Overclock.net   Reply With Quote
Old 3 Weeks Ago   #2 (permalink)
Folding Fanatic
 
tofunater's Avatar
 
intel nvidia

Join Date: Sep 2006
Location: Dekalb, IL
Posts: 2,473

Rep: 203 tofunater is acknowledged by manytofunater is acknowledged by manytofunater is acknowledged by many
Unique Rep: 176
Folding Team Rank: 280
Trader Rating: 24
Default

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;
    }
  }
Hmmm... This compiles, does it look like it will successfully track the additional arrays?
__________________

Quote:
Originally Posted by iandh View Post
People like this know deep down inside that they are absolutely worthless, so they must constantly come up with new ways to give themselves worth, otherwise they may be tempted to end it all one day by OD'ing on Care-Bears marathons.

System: Hedonism, Stanford Style
CPU
i7 920 D0
Motherboard
Asus P6T6 WS revolution
Memory
6 gigs G-skill
Graphics Card
Tri-sli 260's
Hard Drive
620aaks
Sound Card
on board
Power Supply
Zalman 850
Case
hardwood techstation(soon)
CPU cooling
GTZ/bix 360/355 (soon)
GPU cooling
water (soon)
OS
Vista 64
Monitor
22" Samsung 225BW
Overclock.net - 2009 Chimp Challenge Champions
tofunater is offline I fold for Overclock.net   Reply With Quote
Old 3 Weeks Ago   #3 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

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
Overclock.net Mod of the Month

Last edited by Spotswood : 3 Weeks Ago at 07:12 PM
Spotswood is offline   Reply With Quote
Old 3 Weeks Ago   #4 (permalink)
Folding Fanatic
 
tofunater's Avatar
 
intel nvidia

Join Date: Sep 2006
Location: Dekalb, IL
Posts: 2,473

Rep: 203 tofunater is acknowledged by manytofunater is acknowledged by manytofunater is acknowledged by many
Unique Rep: 176
Folding Team Rank: 280
Trader Rating: 24
Default

Quote:
Originally Posted by Spotswood View Post
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.
Could you elaborate a bit more? I'm still kinda lost
__________________

Quote:
Originally Posted by iandh View Post
People like this know deep down inside that they are absolutely worthless, so they must constantly come up with new ways to give themselves worth, otherwise they may be tempted to end it all one day by OD'ing on Care-Bears marathons.

System: Hedonism, Stanford Style
CPU
i7 920 D0
Motherboard
Asus P6T6 WS revolution
Memory
6 gigs G-skill
Graphics Card
Tri-sli 260's
Hard Drive
620aaks
Sound Card
on board
Power Supply
Zalman 850
Case
hardwood techstation(soon)
CPU cooling
GTZ/bix 360/355 (soon)
GPU cooling
water (soon)
OS
Vista 64
Monitor
22" Samsung 225BW
Overclock.net - 2009 Chimp Challenge Champions
tofunater is offline I fold for Overclock.net   Reply With Quote
Old 3 Weeks Ago   #5 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

Quote:
Originally Posted by tofunater View Post
Could you elaborate a bit more? I'm still kinda lost
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
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 3 Weeks Ago   #6 (permalink)
Folding Fanatic
 
tofunater's Avatar
 
intel nvidia

Join Date: Sep 2006
Location: Dekalb, IL
Posts: 2,473

Rep: 203 tofunater is acknowledged by manytofunater is acknowledged by manytofunater is acknowledged by many
Unique Rep: 176
Folding Team Rank: 280
Trader Rating: 24
Default

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;
    }
so would this work?
__________________

Quote:
Originally Posted by iandh View Post
People like this know deep down inside that they are absolutely worthless, so they must constantly come up with new ways to give themselves worth, otherwise they may be tempted to end it all one day by OD'ing on Care-Bears marathons.

System: Hedonism, Stanford Style
CPU
i7 920 D0
Motherboard
Asus P6T6 WS revolution
Memory
6 gigs G-skill
Graphics Card
Tri-sli 260's
Hard Drive
620aaks
Sound Card
on board
Power Supply
Zalman 850
Case
hardwood techstation(soon)
CPU cooling
GTZ/bix 360/355 (soon)
GPU cooling
water (soon)
OS
Vista 64
Monitor
22" Samsung 225BW
Overclock.net - 2009 Chimp Challenge Champions
tofunater is offline I fold for Overclock.net   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 04:20 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.11919 seconds with 8 queries