|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hello to everyone.
My Question: struct patientlist { char name[25]; char surname[25]; char doctor[25]; } patient[5]; I'm trying to sort these entries. User will input 5 patien's information and the programme will give the sorted results: 1. Surname ascending 2. Doctor ascending I tried bubble sort but these are string so I couldnt sort them: Here is the bubble sort method: Code:
void bubbleSort(int *array, int length)
{
int i, j, temp;
int test;
for(i = length - 1; i > 0; i--)
{
test=0;
for(j = 0; j < i; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
test=1;
}
}
if(test==0) break;
}
}
Then i had a idea of using atoi function. Can it work ? I mean i coudlnt make it work but is there a possibility? my sample programme's output should be like that: 1. Surname ascending: Surname Name Doctor Jack John Marty Kylie Johns Ken Maale Jack Marl Oily Jack Mika Tyrie Katie Perty Can u help me ? Last edited by Gordack; 04-19-2007 at 07:04 PM. |
| Sponsored Links |
|
|
|
|||
|
Quote:
I'm trying to do this in C language. |
|
|||||
|
I can't really help you out then. I'm not very good with algorithms and I don't have the time to try to make it work.
I can probably help you out with some theory. You've to send the whole array of patientlist's (patient) to the function, else it will not have anything to compare. The example you have made (bubbleSort()) does not sort multiple arrays, only one! In your patientlist-structure, you have array of strings - and it isn't those array that have to be sorted, it is the array of arrays, if we can say it in that way. That's the reason why you have to send the whole array of patientlist's to the function. When that is said, you have to use something similar to the example you came with. Compare each character, for one or two, or maybe them all in one time, and then exchange the strings - but do not forget to exchange the other strings in the array too, they belong the exchanged string. To make it a bit clearer, look at this... Code:
int x, y;
for(x = 0; x < 5; x++)
{
for(y = 0; y < strlen(patient[x].surname); y++)
{
/*
We can now compare patient[x].surname[y] with
patient[x].surname[y+1]. Each of them contains a
character. And of course, when it's in ascending
order, you're gonna compare using the '<' operator.
That's because the smallest value (ASCII) is going
to be the first in the array.
*/
}
}
Good luck! Last edited by v0id; 04-20-2007 at 08:31 AM. |
|
|||||
|
You're looking at needing two things:
1) you need a string copy function and a string compare function 2) you will need a temporary variable to hold values in. What you need to do is use the string compare function to compare the strings, and a copy function with the temporary string to perform a swap. Once you have those, you should be able to modify the bubble sort to accomplish what you want.
__________________
To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. | To view links or images in signatures your post count must be 0 or greater. You currently have 0 posts. Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
| Sponsored Links |
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| need help in counting a specify character in a string. | lichy | Perl | 2 | 06-27-2007 10:03 PM |
| [Help] String manipulation. | afiser | Visual Basic Programming | 1 | 05-31-2007 03:24 PM |
| Finding non-standard sorting order via graph theory | elle | General Programming | 1 | 05-17-2007 09:36 PM |
| Seperate values in a string | Chan | C# Programming | 4 | 07-25-2006 09:17 AM |
| Removing HTML from a String using ColdFusion | roger | ASP, ASP.NET and Coldfusion | 0 | 05-11-2006 10:19 PM |
| Xav | ........ | 1315.71 |
| MeTh0Dz|Reb0rn | ........ | 1053.7 |
| morefood2001 | ........ | 879.43 |
| John | ........ | 877.37 |
| marwex89 | ........ | 869.98 |
| WingedPanther | ........ | 814.88 |
| Brandon W | ........ | 710.1 |
| chili5 | ........ | 300.72 |
| Steve.L | ........ | 225.26 |
| dargueta | ........ | 192.86 |
Goal: 100,000 Posts
Complete: 82%