C++ selection sort character array

need to use selection sort to sort char array in descending order

Page 1 of 1

2 Replies - 11255 Views - Last Post: 27 February 2008 - 01:27 PM Rate Topic: -----

#1 hollis092  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 08

C++ selection sort character array

Posted 27 February 2008 - 11:29 AM

I am reading from a text file a list containing 10 names and 10 corresponding numbers.

Here is the input file:

Donald Trump 5.1
Paris Hilton 2.98
Lindsey Lohan 3.65
Marion Jones 1.03
Paul McCartney 3.75
Snoop Dogg 1.65
Britney Spears 2.14
Sean Combs 1.07
Heather Mills 2.31
Michael Vick 4.67

The names are stored in parallel arrays and the numbers are stored in an array corresponding to the names.


Here is my code so far: not functioning properly;

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<fstream>

using namespace std;




int main()

{

ifstream inFile;

char array1 [10] [50];
char array2 [10] [50];
char array3 [10] [5];


inFile.open("celebs.txt");

int location = 0 ;

cout<<"Top 10: Celebrities legal expences in 2007.\n\n";

cout<<left<<setw(15)<<"Name"<<left<<setw(25)<<"In Millions"<<endl;
cout<<"--------------------------"<<endl;

inFile >> array1[ location ] >> array2[ location ] >> array3[location];
cout<<array1[0]<<" "<<array2[0]<<setw(10)<<setfill('.')<<""<<array3[0]<<endl;
while ( inFile && location < 9 )
{
location++;
inFile >> array1[ location ] >> array2[ location ] >> array3[location];


cout<<array1[location]<<" "<<array2[location]<<setw(10)<<setfill('.')<<""<<array3[location]<<endl;


}

Output should look like:

Name In Millions
------------------------------------
Donald Trump...............5.1
Michael Vick.................4.67
Paul McCartney.............3.75
Lindsey Lohan...............3.65
Paris Hilton...................2.98
Heather Mills................2.31
Britney Spears...............2.14
Snoop Dogg..................1.65
Sean Combs..................1.07
Marion Jones.................1.03

Help with sorting the array holding the numbers and outputting the numbers and names in the manner shown above is needed. I can't figure out how to sort them and then match that with each name..

All help is good help.

DEV C++ is my complier.

This post has been edited by hollis092: 27 February 2008 - 11:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ selection sort character array

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: C++ selection sort character array

Posted 27 February 2008 - 11:42 AM

Please implement selection sort and then post your code using code tags again and point out where exactly you face difficulties.

Happy coding! :)
Was This Post Helpful? 0
  • +
  • -

#3 hollis092  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 08

Re: C++ selection sort character array

Posted 27 February 2008 - 01:27 PM

View Postletthecolorsrumble, on 27 Feb, 2008 - 11:42 AM, said:

Please implement selection sort and then post your code using code tags again and point out where exactly you face difficulties.

Happy coding! :)




#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<fstream>

using namespace std;

void selectionSort( int list[ ] , int length ) // Problem here with this fuction, how do i get this to sort the array
//of characters from highest to lowest -
{
int index, posSmallest, inrIndex, temp ;
for ( index = 0 ; index < length - 1; index++ )
{
posSmallest = index ;
for ( inrIndex = index + 1 ; inrIndex < length ; inrIndex++ )
if ( list[ inrIndex ] < list [ posSmallest ] )
posSmallest = inrIndex ;
temp = list [ posSmallest ] ;
list [ posSmallest ] = list [ index ] ;
list [ index ] = temp ;
}
};




int main()

{

ifstream inFile;

char array1 [10] [50];
char array2 [10] [50];
char array3 [10] [5];


inFile.open("celebs.txt");

int location = 0 ;

cout<<"Top 10: Celebrities legal expences in 2007.\n\n";

cout<<left<<setw(15)<<"Name"<<left<<setw(25)<<"In Millions"<<endl;
cout<<"--------------------------"<<endl;

inFile >> array1[ location ] >> array2[ location ] >> array3[location];
cout<<array1[0]<<" "<<array2[0]<<setw(10)<<setfill('.')<<""<<array3[0]<<endl;
while ( inFile && location < 9 )
{
location++;
inFile >> array1[ location ] >> array2[ location ] >> array3[location];

selectionSort(array3,10); // here is one problem - cant figure out how to sort array of characters
// with the function I have defined and don't know how to mod it to do so....

cout<<array1[location]<<" "<<array2[location]<<setw(10)<<setfill('.')<<""<<array3[location]<<endl;


}
















system("pause");
return 0;
}


The problem is getting the selectionSort to function properly. I want this program to read the values into the arrays and print out the arrays in descending order. with the input file given, and the output shown how do i do this?? In other words i am not completely sure how to properly implement the selection sort function to do this.... Thats where i need help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1