Sorting strings in descending order using parallel arrays

I have been given an assignment to create a customer contact program that takes 8 customer names and phone numbers as input and stores the information in parallel arrays. The arrays must then be sorted in ascending order based on phone number maintaining the association between names and numbers. I can get the loops to run to accept the data for the arrays, and I can get it to display what I have entered into the arrays, but it is not sorting the arrays as needed.


#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
#include <locale>

int main()
{
int sub = 0; //keeps track of subscripts

int temp = 0; //variable used for swapping

int maxSub = 9; //maximum subscript

int lastSwap = 0; //position of last swap

char swap ='Y'; //indicated if a swap was made


// get names
std::string name[8] = {" "};
//store data in the array
for (int sub = 0; sub < 8; sub += 1)
{
cout << " Enter a name. " << sub + 1 << ": ";
cin >> name[sub];
}// end for

//Get numbers
std::string phone[8] = {" "};
//store data in the array
for (int sub = 0; sub < 8; sub += 1)
{
cout << " Enter a phone number." << sub + 1 << ": ";
cin >> phone[sub];
}// end for
//repeat loop instructions as long as a swap was made

while (swap =='Y')
{
std::string temp = "";
swap = 'N'; //assume no swaps are necessary
sub = 0; //begin comparing with first array element

//compare adjacent array elements to determine whether a swap is necessary
while (sub < maxSub)
{
if (phone[sub] > phone[sub + 1])
{
// a swap is necessary
temp = phone[sub];
phone[sub] = phone[sub + 1];
phone[sub + 1] = temp;
swap = 'Y';
lastSwap = sub;
}
sub = sub + 1; //increment subscript

} //end while

//display sorted array

for (int sub = 0; sub < 8; sub+= 1)

cout << phone[sub] << endl;

//end for

return 0;

}

}




Last edited on
while (sub < maxSub)

sub will take values 0 to 8, because maxSub is 9.

phone[sub] could be phone[8] which is off the end of the array and you're reading random memory.
phone[sub + 1] could be phone[9] which is even further off the end of the array and you're reading random memory.

Do not compare random memory in your sorting code.


Look closely at the location of return 0; . Look what loop it's inside.
Last edited on
OK, I see what you are saying....

Ok, so in the beginning declaration, I went back and update maxSub to 7 because the first element in an array is 0. I also moved the return ) statement out of that loop. I also updated the display array loop. Here si the code I got so far:

int main()
{
int sub = 0; //keeps track of subscripts

int temp = 0; //variable used for swapping

int maxSub = 7; //maximum subscript

int lastSwap = 0; //position of last swap

char swap ='Y'; //indicated if a swap was made


// get names
std::string name[8] = {" "};
//store data in the array
for (int sub = 0; sub < 8; sub += 1)
{
cout << " Enter a name. " << sub + 1 << ": ";
cin >> name[sub];
}// end for

//Get numbers
std::string phone[8] = {" "};
//store data in the array
for (int sub = 0; sub < 8; sub += 1)
{
cout << " Enter a phone number." << sub + 1 << ": ";
cin >> phone[sub];
}// end for

//repeat loop instructions as long as a swap was made
while (swap == 'Y')
{
std::string temp = "";
swap = 'N'; //assume no swaps are necessary
sub = 0; //begin comparing with first array element

//compare adjacent array elements to determine whether a swap is necessary
while (sub < maxSub)
{
if (phone[sub] > phone[sub + 1])
{
// a swap is necessary
temp = phone[sub];
phone[sub] = phone[sub + 1];
phone[sub + 1] = temp;
swap = 'Y';
lastSwap = sub;
}
sub += 1; //increment subscript
} //end while

//display sorted array

for (int sub = 0; sub < 8; sub+= 1)

cout << setw(10) << phone[sub] << setw(8) << name[sub] << endl;
}

return 0;
}


Here are the results that I am getting now, they are repeating several times for some reason and they are not actually getting sorted:

7035757 Misty
5165103 Jesus
7309914 Jim
6775111 Teresa
5312862 Damian
7646236 Dustin
5399264 Juanita
9003287 Tito
5165103 Misty
7035757 Jesus
6775111 Jim
5312862 Teresa
7309914 Damian
5399264 Dustin
7646236 Juanita
9003287 Tito
5165103 Misty
6775111 Jesus
5312862 Jim
7035757 Teresa
5399264 Damian
7309914 Dustin
7646236 Juanita
9003287 Tito
5165103 Misty
5312862 Jesus
6775111 Jim
5399264 Teresa
7035757 Damian
7309914 Dustin
7646236 Juanita
9003287 Tito
5165103 Misty
5312862 Jesus
5399264 Jim
6775111 Teresa
7035757 Damian
7309914 Dustin
7646236 Juanita
9003287 Tito
5165103 Misty
5312862 Jesus
5399264 Jim
6775111 Teresa
7035757 Damian
7309914 Dustin
7646236 Juanita
9003287 Tito

Last edited on
Ok, after some fooling around, I realized that i was missing a line of code. I have got it corrected and now it will display the phone numbers in ascending order, however the names are not being sorted accordingly. Can anyone point me in the right direction for this issue

int main()
{
int sub = 0; //keeps track of subscripts

int temp = 0; //variable used for swapping

int maxSub = 7; //maximum subscript

int lastSwap = 0; //position of last swap

char swap ='Y'; //indicated if a swap was made


// get names
std::string name[8] = {" "};
//store data in the array
for (int sub = 0; sub < 8; sub += 1)
{
cout << " Enter a name. " << sub + 1 << ": ";
cin >> name[sub];
}// end for

//Get numbers
std::string phone[8] = {" "};
//store data in the array
for (int sub = 0; sub < 8; sub += 1)
{
cout << " Enter a phone number." << sub + 1 << ": ";
cin >> phone[sub];
}// end for

//repeat loop instructions as long as a swap was made
while (swap == 'Y')
{
std::string temp = "";
swap = 'N'; //assume no swaps are necessary
sub = 0; //begin comparing with first array element

//compare adjacent array elements to determine whether a swap is necessary
while (sub < maxSub)
{
if (phone[sub] > phone[sub + 1])
{
// a swap is necessary
temp = phone[sub];
phone[sub] = phone[sub + 1];
phone[sub + 1] = temp;
swap = 'Y';
lastSwap = sub;
}
sub += 1; //increment subscript
} //end while
maxSub = lastSwap; //restart maximum subscript
}


//display sorted array

for (int sub = 0; sub < 8; sub += 1)
cout << setw(10) << phone[sub] << setw(8) << name[sub] << endl;

return 0;
}
Topic archived. No new replies allowed.