Sorting 2d array keeping row data together
Jan 2, 2011 at 4:34am UTC
Unsure how to succinctly explain my problem, but here goes.
I have a class problem where I must input data from a file that has the name of an element and its abundance as integer. The program must sort either numerically or alphabetically.
I am trying to input the data as an array and then apply a bubble sort.
My problem arises --> when sorting how to I keep each integer with its corresponding name.
My input is something like this
20 Oxygen
1 CarbonDioxide
Here is what I have so far, it sorts numerically but obviously the name labels don't move with it.
Maybe I am going about this the wrong way?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
using namespace std;
int main()
{ char filename[50]; //Specify all variables
FILE *fp;
string a,y;
size_t element=0;
size_t abundance=0;
string array[2][2];
do {
ifstream data_stream;
cout << "Enter a filename: " ;
cin >> filename;
data_stream.open(filename);
if (data_stream.fail())
{
cout << "Incorrect filename or extension.\n" ;
goto failsafe;
}
for (int i=0; !data_stream.eof(); i++)
{
for (int j=0; j<2; j++)
{
data_stream >> array[i][j];
}
}
for (int i=0; i<2; i++)
{
cout << array[i][0] << " " << array[i][1] << '\n' ;
}
//Numerical Bubble Sort ////////////////////////////////
for (int i=0;i<2;i++)
{
for (int j=0; j<2; j++)
{
if (array[j+1][0] < array[j][0]) // swap values
{
y = array[j][0];
array[j][0] = array[j+1][0];
array[j+1][0] = y;
}
}
}
for (int i=0; i<2; i++)
{
cout << array[i][0] << " " << array[i][1] << '\n' ;
}
// /////////////////////////////////////////////////////
failsafe:
cout << "Exit? Y/N " ;
cin >> a;
}while (a !="Y" && a !="y" ); //Loops program until user wishes to end.
}
Thanks in advance.
Jan 2, 2011 at 5:27am UTC
When you do your compare, you need to swap both pieces of data at the same time. You need to tell C++ to move data in [j][1] and [j+1][1].
Also I recommend you move your swapping to a small swap function. There is one nicely made for strings in the STL String Library called swap();
So change
1 2 3
y = array[j][0];
array[j][0] = array[j+1][0];
array[j+1][0] = y;
to
1 2
swap(array[j][0], array[j+1][0]);
swap(array[j][1], array[j+1][1]);
Jan 2, 2011 at 3:27pm UTC
Thanks for the quick reply.
When I added your suggestion
1 2
swap(array[j][0], array[j+1][0]);
swap(array[j][1], array[j+1][1]);
it failed to print the second row of the sorted array on the first pass. When the program runs again having selected "n" from the failsafe menu it works fine. See screen shot below
[URL=
http://tinypic.com/view.php?pic=2d2f40m&s=7]http://tinypic.com/view.php?pic=2d2f40m&s=7[/URL]
I cannot see what is wrong with the code?
I have got around it by using a form of my original efforts:
1 2 3 4 5 6 7
y=array[i][0];
array[i][0]=array[i+1][0];
array[i+1][0]=y;
x=array[i][1];
array[i][1]=array[i+1][1];
array[i+1][1]=x;
but your suggestion seems neater and I would like to use that if you can see the problem?
Jan 2, 2011 at 3:53pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
using namespace std;
const size_t SIZE = 2;
int main()
{
char filename[50]; //Specify all variables
size_t element=0;
size_t abundance=0;
//string array[2][2];
string amounts[SIZE],
names[SIZE],
a;
do {
ifstream data_stream;
cout << "Enter a filename: " ;
cin >> filename;
data_stream.open(filename);
if (data_stream.fail())
{
cout << "Incorrect filename or extension.\n" ;
goto failsafe;
}
for (int i=0; !data_stream.eof() && i < SIZE; i++)
{
// data_stream >> array[i][j];
data_stream >> amounts[i];
data_stream >> names[i];
}
for (int i=0; i<SIZE; i++)
{
//cout << array[i][0] << " " << array[i][1] << '\n';
cout << amounts[i] << " " << names[i] << '\n' ;
}
//Numerical Bubble Sort ////////////////////////////////
for (int i=0;i<SIZE-1;i++)
{
for (int j=0; j<SIZE-1; j++)
{
//if(array[j+1][0] < array[j][0]) // swap values
if (amounts[j+1] < amounts[j])
{
swap (amounts[j], amounts[j+1]);
swap (names[j], names[j+1]);
}
}
}
for (int i=0; i<SIZE; i++)
{
//cout << array[i][0] << " " << array[i][1] << '\n';
cout << amounts[i] << " " << names[i] << '\n' ;
}
// /////////////////////////////////////////////////////
failsafe:
cout << "Exit? Y/N " ;
cin >> a;
} while (a !="Y" && a !="y" ); //Loops program until user wishes to end.
}
Use parallel arrays and not multidimensional and some other fixes.
Jan 2, 2011 at 7:10pm UTC
Thanks that works.
Any idea why it wouldn't print the last line before?
Jan 2, 2011 at 10:55pm UTC
Now I need to sort alphabetically too! *SIGH* I cannot find anything from class notes anywhere near as complicated as this assignment...
Anyway I figured now we have parallel arrays it would be easier.. alas no.
I want to try and do something like this
1 2 3 4 5 6
for (i=0;i<SIZE-1;i++)
{
for (j=i+1;j<SIZE-1;j++)
{
if (strcmp(str[j],str[j+1])>0)
// swap values...
For fear of you guys doing my whole assignment maybe you could just point me in the right direction. strcmp is a good starting point right?
Topic archived. No new replies allowed.