Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-19-2007, 07:02 PM
Gordack Gordack is offline
Newbie
 
Join Date: Apr 2007
Posts: 4
Credits: 0
Rep Power: 0
Gordack is on a distinguished road
Default Sorting String ( Need help ASAP)

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-20-2007, 12:18 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

Are you doing this in C or C++?
In C++ I would prefer to use a function from the STL, because it's much more secure, than coding one from the scratch by hands. I don't know if you have to do the function by hand, or you can choose by yourself?

Example using the STL sort() from algorithm:
Code:
#include <iostream>
#include <algorithm> // sort()

int main()
{
	int arr[]   = {5, 3, 4, 1, 2}; // Gonna be: 1 2 3 4 5
	int arrSize = 4;
	
	std::cout << "Before sort()" << std::endl;
	for(int i = 0; i <= arrSize; i++)
		std::cout << arr[i] << std::endl;
		
	std::sort(arr, arr+arrSize+1);
		
	std::cout << "After sort()" << std::endl;
	for(int j = 0; j <= arrSize; j++)
		std::cout << arr[j] << std::endl;
		
	return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-20-2007, 05:25 AM
Gordack Gordack is offline
Newbie
 
Join Date: Apr 2007
Posts: 4
Credits: 0
Rep Power: 0
Gordack is on a distinguished road
Default

Quote:
Originally Posted by v0id View Post
Are you doing this in C or C++?
In C++ I would prefer to use a function from the STL, because it's much more secure, than coding one from the scratch by hands. I don't know if you have to do the function by hand, or you can choose by yourself?

Example using the STL sort() from algorithm:
Code:
#include <iostream>
#include <algorithm> // sort()

int main()
{
	int arr[]   = {5, 3, 4, 1, 2}; // Gonna be: 1 2 3 4 5
	int arrSize = 4;
	
	std::cout << "Before sort()" << std::endl;
	for(int i = 0; i <= arrSize; i++)
		std::cout << arr[i] << std::endl;
		
	std::sort(arr, arr+arrSize+1);
		
	std::cout << "After sort()" << std::endl;
	for(int j = 0; j <= arrSize; j++)
		std::cout << arr[j] << std::endl;
		
	return 0;
}


I'm trying to do this in C language.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-20-2007, 08:25 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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.

        */
    }
}
Hope you understood just a bit.
Good luck!

Last edited by v0id; 04-20-2007 at 08:31 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-20-2007, 11:12 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,479
Last Blog:
wxWidgets is NOT code ...
Credits: 815
Rep Power: 28
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 02:02 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads