sorting 2 related string arrays

S

SneakyElf

i am very green with c++ so i get stuck on very simple things
anyway, i need to write a program that would
read data from file (containing names of tv shows and their networks)
one line at a time ( ; separates tv show and network)
sort the strings according to the network and according to the show.
and so:

//to open file -

ifstream myFile;
myFile.open ("tvshows.txt");
if (!myFile) {
cerr << "Error opening the file" << endl;
exit (1);
}

// the two arrays would be:
string showTitle [10];
string showNetwork [10];

i was thinking of using the linearsort() for sorting strings, however
i am litte confused as to how to read the data from the file into
arrays (since the one is titles and the other is network - how to tell
how to separate?)
and also would be be better to use a two-dimensional array in this
case?
as i said, not clear on the sorting at all :)

any topics (or websites) where i could read and improve my ever so
wonderful programming skills would be appreciated.
thanks.
 
J

Jim Langston

i am very green with c++ so i get stuck on very simple things
anyway, i need to write a program that would
read data from file (containing names of tv shows and their networks)
one line at a time ( ; separates tv show and network)
sort the strings according to the network and according to the show.
and so:

//to open file -

ifstream myFile;
myFile.open ("tvshows.txt");
if (!myFile) {
cerr << "Error opening the file" << endl;
exit (1);
}

// the two arrays would be:
string showTitle [10];
string showNetwork [10];

i was thinking of using the linearsort() for sorting strings, however
i am litte confused as to how to read the data from the file into
arrays (since the one is titles and the other is network - how to tell
how to separate?)
and also would be be better to use a two-dimensional array in this
case?
as i said, not clear on the sorting at all :)

any topics (or websites) where i could read and improve my ever so
wonderful programming skills would be appreciated.
thanks.

I'm not sure if this is homework or not, it sure sounds like it. Since it
may be homework I'll just give you some pointers.

I would read the entire line in one go, then seperate it by the ;
std::string line
std::geline( myFile, line );

You can then use std::string.find to find the position of the ';' and then
use substr to break out the parts.

One thing though, if you have two different arrays for the title and
network, when you sort one the other will stay as it was, so they won't
match anymore. You might want to use a structure
struct Show
{
std::string Title;
std::string Network;
} Shows[10];

although I would probalby use a std::vector instead.
 
S

Salt_Peter

i am very green with c++ so i get stuck on very simple things
anyway, i need to write a program that would
read data from file (containing names of tv shows and their networks)
one line at a time ( ; separates tv show and network)
sort the strings according to the network and according to the show.
and so:

//to open file -

ifstream myFile;
myFile.open ("tvshows.txt");
if (!myFile) {
cerr << "Error opening the file" << endl;
exit (1);

don't do that - throw an exception instead. Like so...

#include <iostream>
#include < stdexcept >

int main()
{
bool condition(true);
try {
if ( condition )
{
throw std::runtime_error("some usefull message");
}
} // try block
catch( const std::exception& r_e )
{
std::cout << "Error: " << r_e.what();
std::cout << std::endl;
}
}
}

// the two arrays would be:
string showTitle [10];
string showNetwork [10];

i was thinking of using the linearsort() for sorting strings, however
i am litte confused as to how to read the data from the file into
arrays (since the one is titles and the other is network - how to tell
how to separate?)
and also would be be better to use a two-dimensional array in this
case?
as i said, not clear on the sorting at all :)

any topics (or websites) where i could read and improve my ever so
wonderful programming skills would be appreciated.
thanks.

Are arrays a requirement?
Why not a std::map< std::string, std::string > or std::set< Show >?
You could write a class called Show which encapsulates name and
network as members.
You'll need an appropriate op< in order to provide weak ordering (a
std::set or std::map's elements are already sorted upon insertion).
Associatice containers have the advantage over conventional containers
in that searches are very fast.

If you prefer arrays, try loading a std::vector< Show > instead and
then order the elements with std::sort <algorithm>.

Why not try them all and compare? You'll think twice about using an
array next time.
 
M

Michael Angelo Ravera

i am very green with c++ so i get stuck on very simple things
anyway, i need to write a program that would
read data from file (containing names of tv shows and their networks)
one line at a time ( ; separates tv show and network)
sort the strings according to the network and according to the show.
and so:

//to open file -

ifstream myFile;
myFile.open ("tvshows.txt");
if (!myFile) {
cerr << "Error opening the file" << endl;
exit (1);

}

// the two arrays would be:
string showTitle [10];
string showNetwork [10];

i was thinking of using the linearsort() for sorting strings, however
i am litte confused as to how to read the data from the file into
arrays (since the one is titles and the other is network - how to tell
how to separate?)
and also would be be better to use a two-dimensional array in this
case?
as i said, not clear on the sorting at all :)

any topics (or websites) where i could read and improve my ever so
wonderful programming skills would be appreciated.
thanks.

OK. first, you probably want to use a struct, but let's leave them
separate for now.
#define maxshows 10
#define showsize 40
#define networksize 10
char showTitle [maxshows] [showsize];
char showNet [maxshows] [networksize];
short num_shows = 0;

while ((num_shows < maxshows) && !myfile.eof ())
{
myfile.getline (showTitle [num_shows], showsize, ';');
myfile.getline (showNet [num_shows], networksize);
}

That will get the data into the parallel arrays.

You probably will have to write your own sorting for parallel arrays.

The usual "index" sort works like this:

short i,j, temp;
short idx [maxshows];
for (i = 0; i < num_shows; i++)
idx [
for (i = 0; i < num_shows - 1; i++)
{
for (j = i+1; j < num_shows; j++)
{
if (istrcmp (showTitile [idx[], showTitile[idx[j]]) > 0)
{
temp = idx ;
idx = idx [j];
idx [j] = temp;
}
}
}

Now, for i=0 to num_shows -1, you have the shows [idx] in order.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top