473,671 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Lstn ame,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 2743
In article <11************ **********@p79g 2000cwp.googleg roups.com>,
zf*****@mail.co m 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.co m 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<dou bleScores;
};

int main() {
std::vector<Stu dentstudents;
while(true) {
Student s;
if(!s.ReadStude nt()) break;
students.push_b ack(s);
}
std::sort(s.beg in(), s.end());
Student::PrintH eader();
for(std::vector <Student>::iter ator 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
2982
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 an array) and I want to sort on Descending. This is displayed using print_r() function. Array
7
3255
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)) CType(local4, Short) = CType(src, Short)
22
4137
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) { return -1; } else
7
3038
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 use a regex-based sort order that you can't do in Access 2000* SQL with "ORDER BY".
16
3381
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 or switch-case c. you can use pointers only d. you cannot use any of the loops either.
23
6318
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
455
by: Roy Gourgi | last post by:
Hi, How would I sort an array? TIA Roy
3
2185
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 ( ; separates tv show and network) sort the strings according to the network and according to the show. and so: //to open file - ifstream myFile;
3
7333
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 uses unfamiliar terms and syntax. Intermediate and advanced Perl coders should find this article useful. The object of the article is to inform the reader, it is not about how to code Perl or how to write good Perl code, but to teach the Schwartzian...
5
3179
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 search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
0
8393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8598
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7437
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.