0

Disclaimer: I know that parallel arrays are awful and should be avoided and that selection sort is not the most efficient sort, but in this case that's the way the boss man wants it done. I have looked at a lot of different websites and none that really seem to nail down an answer. Also, it is probably good to point out that I am new to C++ and only know fairly basic coding and debugging.

I have two simple parallel arrays and am trying to design a simple selection sort that sorts one of the arrays, and then swaps the elements in the second array accordingly. I have the selection sort part working, but it does not seem to swap the elements in my second array correctly.

Here is what my output looks like:

1 (jibberish)
2 (jibberish)
3 (jibberish)
4 (jibberish)
5 (jibberish)

Where I have (jibberish) the console does not form any identifiable letter, just odd shapes (if it's helpful, the last element that is output is a heart).

Here's what it is supposed to look like:

1 a
2 b
3 c
4 d
5 e

Now I realize that I could easily run a selection sort on the second array in this scenario, but my point is to get the second array to swap elements respective to what the selection sort does to the first array.

Is there any way to keep these arrays lined up correctly? I've been trying to solve this issue for most of the day and I'm sure it's a fairly simple thing to figure out, but my brain is shot.

Below is my code, thanks in advance for looking at it.

#include "stdafx.h"
#include <iostream>

using namespace std;


//Function Prototypes
void sort(int num[], char alph[], int size);





//Selection sort function
void sort(int num[], char alph[], int size)
{
int startScan;
int minIndex;
int minValue;

for (startScan = 0; startScan < (size - 1); startScan++)    //Moves through the elements
{
    minIndex = startScan;
    minValue = num[startScan];

    int index = 0;

    for (index = startScan + 1; index < size; index++)  //Compares the elements
    {
        if (num[index] < minValue)
        {
            minValue = num[index];
            minIndex = index;
        }
    }

    num[minIndex] = num[startScan];
    num[startScan] = minValue;

    alph[minIndex] = alph[startScan];
    alph[startScan] = alph[index];
}
}






//Main
int _tmain(int argc, _TCHAR* argv[])
{
int num[] = {5, 3, 1, 4, 2};
char alph[] = { 'e', 'c', 'a', 'd', 'b' };
int const SIZE = 5;


//Prints out unsorted array
cout << "This is the unsorted arrays." << endl;
cout << endl;

for (int count = 0; count < SIZE; count++)
{
    cout << num[count] << " \t ";
    cout << alph[count] << endl;
}

cout << endl;
cout << endl;


//Calls the sort function
sort(num, alph, SIZE);


//Prints out the sorted array
cout << "This is the sorted array." << endl;
cout << endl;

for (int count = 0; count < SIZE; count++)
{
    cout << num[count] << " \t";
    cout << alph[count] << endl;
}


//Pause
char temp[50];
cin >> temp;


return 0;
}

EDIT: I edited the

alph[minIndex] = num[startScan]

issue so it reads correctly now as:

alph[minIndex] = alph[startScan]

I am now getting this as an output:

1 (jibberish)
2 (jibberish)
3 (jibberish)
4 (jibberish)
5 e

EDIT 2: I edited the line of code under my previous edit and the arrays are now lining up properly and I am no longer getting a bunch of jibberish for outputs. Below is the edited sort function of my code:

//NOTICE temp VARIABLE CHANGES!
void sort(int num[], char alph[], int size)
{
int startScan;
int minIndex;
int minValue;
int temp;

for (startScan = 0; startScan < (size - 1); startScan++)    //Moves through the elements
{
    minIndex = startScan;
    minValue = num[startScan];
    temp = alph[startScan];

    int index = 0;

    for (index = startScan + 1; index < size; index++)  //Compares the elements
    {
        if (num[index] < minValue)
        {
            minValue = num[index];
            minIndex = index;
            temp = alph[index];
        }
    }

    num[minIndex] = num[startScan];
    num[startScan] = minValue;

    alph[minIndex] = alph[startScan];
    alph[startScan] = temp;
}
}
Oryn
  • 5
  • 1
  • 6
  • The other problem is the line below the one you just fixed you stored the value for minValue you need to do the same for when you change the car array. – West Apr 04 '15 at 02:15
  • That did it. I added a new variable, temp, and have it function basically as another minValue. I will post my new code for anyone else trying to figure this out. Thanks for you help. – Oryn Apr 04 '15 at 02:19

2 Answers2

0

See this line:

alph[minIndex] = num[startScan];

Second faulty line:

alph[startScan] = alph[index];

It should be:

alph[startScan] = alph[minIndex];

By the time the code exits of the inner loop, size has a value of one beyond the array size.

My advice: Use an IDE and the debugger to follow up code execution and examine variables. Also, my first hint should have gotten you looking at incorrect indexes. C++ does not care to check array bounds by default. You usually get garbage when getting out of bounds or following a incorrect pointer. You can remedy the first problem by selecting a compiler option to check for array bounds. That will slow down your application during development time but can be removed once everything works correctly.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Thanks for pointing that out! You can only stare at things for so long before everything starts to look the same. I am now getting the last element of the second array to print out right but the prior four are still jibberish. – Oryn Apr 04 '15 at 01:54
  • Thanks for the tips! I forgot that C++ compilers don't check for any out of bounds errors! I'll definitely look into learning more about debugging, thanks again for your tips and hints! – Oryn Apr 04 '15 at 02:27
  • I forgot to mention that you need a temporary variable to properly swap the alphanumeric characters. – Tarik Apr 04 '15 at 02:33
0

The best solution might be to change your

num[minIndex] = num[startScan];
num[startScan] = minValue;

char  temp=alph[minIndex];
alph[minIndex] = alph[startScan];
alph[startScan] = temp;

to this which does the job and really can't be made any simpler.

std::swap(num[minIndex], num[startScan]);
std::swap(alph[minIndex],alph[startScan]);
West
  • 722
  • 7
  • 16
  • This is a very simple, elegant solution. I had seen ".swap" before and it never worked (evidently it's only for strings?). Anyways, this is nice and cleans up the code a bit. Thanks! – Oryn Apr 04 '15 at 02:31
  • No it will work for any pretty much anything check the list at cpprefference:http://en.cppreference.com/w/cpp/algorithm/swap – West Apr 04 '15 at 02:33
  • I love those people, they're awesome. When google used to let you ban search results I would ban cplusplus.com to only get cppreference. – West Apr 04 '15 at 02:40