473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

sorting arrays

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][i] == 0)
cout << "00.0" << " ";
else
cout << fixed << setprecision(1) << scores[j][i] << "
";
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[i], 10 );
memcpy ( Lstname[i], Lstname[min], 10 );
memcpy ( Lstname[min], save, 10 );

}

}

Any ideas?

Aug 29 '06 #1
2 2723
In article <11**********************@p79g2000cwp.googlegroups .com>,
zf*****@mail.com wrote:
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.
Aug 29 '06 #2
zf*****@mail.com wrote:
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][i] == 0)
As for scores.
cout << "00.0" << " ";
else
cout << fixed << setprecision(1) << scores[j][i] << "
This is silly. If you set the field size and precision, you could just
put out scores[j][i] 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<doubleScores;
};

int main() {
std::vector<Studentstudents;
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();
}
Aug 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
7
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to...
16
by: aruna | last post by:
Given a set of integers, how to write a program in C to sort these set of integers using C, given the following conditions a. Do not use arrays b. Do not use any comparison function like if/then...
23
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
10
by: Roy Gourgi | last post by:
Hi, How would I sort an array? TIA Roy
3
by: SneakyElf | last post by:
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...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.