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.
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
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.
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
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;