hello,
i have been writing a program to sort names from one file and numbers from another file.
i have the files being read in. each contains 1500 names or numbers. i have to sort the names alphabetically and the numbers have to move with the name it corresponds to. right now each name is matched with the number in the opposit file. i know that i have to use the function called sort.
can anyone give me an example of how to sort alphabetically???? and also change the numbers list at the same time?
thanks

this is m code so far:

// Include Section
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

// Main Program
int main( )
{
// Constant Declarations
char names[1500][30];//names arrray from input
char nums[1500][30];//number arrray from input
int i=0; //sets i to 0 for counting numbers
int n=0; //sets n to 0 for counting names
int end;// end of the input




// Output Identification
system("CLS");
cout << "In Class 13 by Adam Zankowski- "
<< "Lookup\n\n";

ifstream in_streamname;
in_streamname.open("E:\\phonenumberlookup\\phoneNames.txt");
if (in_streamname.fail())//opens names file and test for fail
{
cout <<"input file opening failed.\n";
}

while (! in_streamname.eof())// reads names
{
in_streamname.getline(names[n],30);
n++;
}

ifstream in_streamnum;
in_streamnum.open("E:\\phonenumberlookup\\phoneNums.txt");
if (in_streamnum.fail()) //opens and test for fail
{
cout <<"input file opening failed.\n";
}
while (! in_streamnum.eof())//reads input from file
{
in_streamnum.getline(nums[i],30);
i++;
}
end=i;

in_streamname.close();//closes input file
in_streamnum.close();//closes input file
do
{
void sort ( )





}


cout << "\n\nEnd Program.\n";

return 0;
}

First, do you know how to code a sort? You could read your textbook or look here

Once you can sort one array, simply modify the function to take both the names and numbers arrays. Sort based on names - every time you move two names about, make the same exchange between same indexes in the numbers array.

yeah i know how to use it for numbers. is it essentially the same as for characters. i did read the book, but all the examples in it are for numbers. and what ways should i modify the function to change both?

To sort strings instead of numeric values, use the strcmp( ) function instead of < or >.

When you do the exchanges, do them for both the names and the ph. numbers arrays.

so something like this???
(its not debugged yet)

// Include Section
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

// Main Program
int main( )
{
	// Constant Declarations
	char names[2000][30];//names arrray from input
	char nums[2000][30];//number arrray from input
	int i=0; //sets i to 0 for counting numbers
	int n=0; //sets n to 0 for counting names
	int end;// end of the input
	
	
	

	// Output Identification
	system("CLS");
	cout << "In Class 13 by Adam Zankowski- "
		 << "Lookup\n\n";

	ifstream in_streamname;
in_streamname.open("E:\\phonenumberlookup\\phoneNames.txt");
if (in_streamname.fail())//opens names file and test for fail
{
		cout <<"input file opening failed.\n";
}

while (! in_streamname.eof())// reads names
{
	in_streamname.getline(names[n],30);
	n++;
}

ifstream in_streamnum;
in_streamnum.open("E:\\phonenumberlookup\\phoneNums.txt");
if (in_streamnum.fail()) //opens and test for fail
{
	cout <<"input file opening failed.\n";
}
while (! in_streamnum.eof())//reads input from file
{
	in_streamnum.getline(nums[i],30);
	i++;
}
end=i;

in_streamname.close();//closes input file
in_streamnum.close();//closes input file


      int i, j, flag = 1;    // set flag to 1 to begin initial pass
      int temp;             // holding variable
      int arrayLength = array.length( ); 
      for(i = 1; (i <= arrayLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (arrayLength -1); j++)
         {
               if ( strcmp(names[j], names[j+1]) == 1)  // ascending order simply changes to <
              { 
                    strcpy(temp,names[j]);             // swap elements
                    strcpy(names[j],names[j+1]);
                    strcpy(names[j+1], temp);
                    flag = 1;               // indicates that a swap occurred.
               
			    strcpy(temp,nums[j]); // swap elements
				strcpy(nums[j],nums[j+1]);
				strcpy(nums[j+1], temp);

				flag = 1; // indicates that a swap occurred.
				}
			   }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned




	cout << "\n\nEnd Program.\n";

	return 0;
}

Yes, you've got it.

A couple comments.

for(i = 1; (i <= arrayLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (arrayLength -1); j++)
         {

Bubble sort can be made a bit better by not checking all the way to the end every time through the outer loop. You've put the item at the end that belongs there, so don't visit it again. Also, loops dealing with arrays generally are written with counter at 0, go to < size. Just an idiom you should be consistent with. And, in Bubble (and some other simple sorts), the main loop should go one less time than there are items. After the last two are sorted, there's nothing gained by looking at just one item.
So, try this (not that you'll be able to measure a difference on any small data sets)

for(i = 0; (i < arrayLength-1) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (arrayLength -1 - i ); j++)
         {

I'm glad to see you used the flag to stop short when a full pass is made with no swaps. You don't need to set the flag to 1 twice in the swap block.

Is this int arrayLength = array.length( ); really working for you? You get the length of your arrays (the number of valid items) from the counters n or i in your reading loops. Matter of fact, you should check that n and i are the same, to ensure good data has been read in.

this: int arrayLength = array.length( );
i dont really know. i was working on this and then i got an im from another student in my class and said i need that and then signed off. so i dont really know what its doing. it looks like its checking the length of the string??????

More likely he's using a vector to store the bunch of strings as opposed to your creating the 2D char array. Like I said before, n or i give you that information.

so i can get rid of the = array.length( )

so i can get rid of the = array.length( )

absolutely

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.