sorting arrays

Z

zfareed

I managed to sort a 2-dim array of lastnames but I am having trouble
with printing the corresponding first names which are stored in a
different 2-dim array. Here is my code for:

bubbleSort(Lstname,j);
// Printing array
cout << "STUDENT " << setw(8) << "ID " << setw(20)<< "S1 S2
S3 S4"
<< " S5 S6 S7 S8 AVG"<< endl;
cout <<
"--------------------------------------------------------------"
<< "-----------------"<<endl;
k=j;
for(j=0;j<k;j++)
{

cout << Lstname[j] << Fstname [j] << " " << Id[j] << " ";

for(i=0;i<8;i++)
if(scores[j] == 0)
cout << "00.0" << " ";
else
cout << fixed << setprecision(1) << scores[j] << "
";


cout << average[j];
cout << endl;

}



void bubbleSort (char Lstname[][10], int j)
{

int n;
char save[10];
int min;

for(n=0; n < j;n++)
cout << Lstname[n] << endl;
cout << endl;
cout << "Sorted list" << endl;

//Sort
for (int i = 0; i < j-1 ; i++)
{
min = i;
for (int t = i + 1; t < j; t++)
{
if (strcmp (Lstname[t],Lstname[min]) < 0 )
min = t;
}


memcpy ( save, Lstname, 10 );
memcpy ( Lstname, Lstname[min], 10 );
memcpy ( Lstname[min], save, 10 );

}

}

Any ideas?
 
D

Daniel T.

I managed to sort a 2-dim array of lastnames but I am having trouble
with printing the corresponding first names which are stored in a
different 2-dim array.

You have two choice depending on what your teacher has already covered.
(a) use a struct or class.

struct Person {
char firstName[10];
char lastName[10];
};

Then have a single array of Person objects and sort the array, or if
your teacher hasn't covered structs or classes yet, swap the first names
at the same time you swap the last names.
 
R

Ron Natalie

I managed to sort a 2-dim array of lastnames but I am having trouble
with printing the corresponding first names which are stored in a
different 2-dim array. Here is my code for:

You don't show us the declaration of Lstname. I see you still haven't
taken our advice to use the real string type rather than trying to
deal with arrays of characters. If your names are 10 characters or
longer you're going to write off the end of the array you give.

Anyhow, the other thing you've not told us is what you saw versus
what you were expecting.
k=j;
for(j=0;j<k;j++)

Single letter variable names make things hard to understand. Further
it's not clear why you are apparently changing the meaning of k from
"the number of names" to an index at this points. Integer variables
are cheap and so are letters in variable names.
{

cout << Lstname[j] << Fstname [j] << " " << Id[j] << " ";

OK, now where did Firstname and ID come from aall of a sudden.
for(i=0;i<8;i++)
if(scores[j] == 0) As for scores.
cout << "00.0" << " ";
else
cout << fixed << setprecision(1) << scores[j] << "


This is silly. If you set the field size and precision, you could just
put out scores[j] even if it had zero.

Further, the fact that you seem to have about four arrays here that are
all dimensioned by k, kind of begs that a class (or struct) should
have been used to keep all these together. If you sort the last names
don't you want all the other information to travel with it?
void bubbleSort (char Lstname[][10], int j)


By the way, unless the goal here is to learn how to write
inefficient sorting algorithms, why not use std::sort?


OK...I'll give you a hint how a real C++ programmer might write
things. I'll leave out some things so you can still make a
contribution to this program.

class Student {
public:
bool ReadStudent(); // read info from stream
// return false on end of input
static void PrintHeader(); // print column headings.
void PrintStudent(); // Output student

bool operator<(const Student& other) {
return LastName < other.LastName;
}
private:
std::string FirstName;
std::string LastName;
ind ID;
std::vector<double> Scores;
};

int main() {
std::vector<Student> students;
while(true) {
Student s;
if(!s.ReadStudent()) break;
students.push_back(s);
}
std::sort(s.begin(), s.end());
Student::printHeader();
for(std::vector<Student>::iterator it = students.begin(): it !=
students.end(); ++it)
it->PrintStudent();
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top