471,086 Members | 802 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

sorting 2 related string arrays

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.

Apr 27 '07 #1
3 2049
<Sn*******@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
>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.
Apr 27 '07 #2
On Apr 27, 6:55 pm, Sneaky...@gmail.com wrote:
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.
Apr 27 '07 #3
On Apr 27, 3:55 pm, Sneaky...@gmail.com wrote:
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[[i]], showTitile[idx[j]]) 0)
{
temp = idx [i];
idx [i] = idx [j];
idx [j] = temp;
}
}
}

Now, for i=0 to num_shows -1, you have the shows [idx[i]] in order.
Apr 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Federico G. Babelis | last post: by
22 posts views Thread by mike | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
7 posts views Thread by Noah Roberts | last post: by
2 posts views Thread by zfareed | last post: by
7 posts views Thread by bkisa | last post: by
reply views Thread by saytri | last post: by
5 posts views Thread by lemlimlee | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.