Subject: Algorithms and Data Structures - Sorting and searching
Description: Sorts ten names and coressponding telephone numbers
Files:
ASCII File = a57q1.dat found on a:\\ drive
CPP File = a57q1.cpp
Date: 17/9/2001 (Final Revision) */
#include
#include
#define size 10 // defines the maximum amount of numbers possible
class List_class // class called List_class
{
private:
long num[size]; // private data member for the telephone numbers
char words[size][30]; // private data member for the names
public:
List_class(); // public code members
void to_screen(); // code member for displaying list to screen
void num_selection_sort(); // code member for the sorting of telephone numbers
void word_selection_sort(); // code member sorting the names with coressponding telephone numbers
};
//-------------------------------------------------------
void main(void) // main program builder
{
List_class words_list; // declaration of List_class object words_list
cout << "This is the list: " << endl << endl; // descriptive heading
words_list.to_screen(); //print the unsoted list to screen
cout << "\n\nThis is the sorted list: " << endl << endl; // descriptive heading
words_list.word_selection_sort(); // sort the list
words_list.to_screen(); // print the sorted list to screen
} // end main
//-------------------------------------------------------
List_class::List_class() //read the list in from a file
{
int a; // integer for for loop that tags the array locations
fstream infile("a:\\a57q1.dat",ios::in); // file to be used in A drive named infile for a57q1.dat
for(a=0;a
{
infile >> words[a] >> num[a]; // reads the list from a file words[position] number[position]
}// end for
infile.close(); //close the file
} //constructor
//-------------------------------------------------------
void List_class::to_screen() //displays the list to the screen
{
int a; // // integer for for loop that tags the array locations
for(a=0;a
{
cout << words[a] << " " << num[a] << " "; // display the words and numbers
}// end for
}// end to screen
//----------------------------------------------------------------------------------------
void List_class::word_selection_sort() //sorts the array into increasing order
{
int i, j, loc_of_min; // three integers here track the word locations
long tphone, minp; // longs for coressponding telephone numbers
char min[30], temp[30],name[30]; // chars for name sorting
for(i=0;i
{
strcpy(min,words[i]); // copy min to words position i
minp = num[i]; // telphone numners minp is equal to num position i
loc_of_min = i; // the location of min equal i
for(j=i+1;j
{
if(strcmp(words[j],min)>0) // test words[j] with min to see if it is less than
{ // if true
strcpy(min,words[j]); //copy min value to words[j]
minp = num[j]; // coressponding telephone numnbers
loc_of_min=j; // location of min equals j
} // end if
}// end for
if(strcmp(min,words[i])>0) // if statement comparing min to see if it is less than words position i
{ // if true
strcpy(temp,words[i]); //copy temp to words position i
tphone = num[i]; // coressponding telephone numbers
strcpy(words[i],min); // copy words position i to min
num[i]= minp; // coressponding telephone numbers
strcpy(words[loc_of_min],temp); // copy words position location of min to temp
num[loc_of_min] = tphone; // coressponding telephone numbers
}// end if
}// end for
}// end selection sort
No comments:
Post a Comment