Sorting arrays of pointers with function?

Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Sorting arrays of pointers with function?

 
0
  #1
Nov 21st, 2004
Hello ladies and gents, I have tried to write my sorting function but keep on getting error messages.

Ive been reading the tutorials on this forum wich explaines alot but I'm still having lots of trouble in comprehending how pointers work and more important, how that I can correctly write the code that I need for them

Hope someone can help me out and explain with this piece of code what I'm doing wrong and how I have to change it and most important why it has to be changed!




  1. void sorteer(char *naam[])
  2. {
  3. for (short i=0; i<9;i++)
  4. {
  5. for (short i=0; naam[i]!= '\0';i++)
  6. {
  7. char temp;
  8.  
  9. stricmp(*naam[i], *naam[i+1]);
  10. strcpy(temp, *naam[i]);
  11. strcpy(*naam[i], *naam[i+1]);
  12. strcpy(*naam[i+1], temp);
  13. }
  14. }
  15.  
  16. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,300
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 225
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #2
Nov 21st, 2004
Describe, in English sentences, what you think the data types are for the following:
  • naam
  • temp
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Sorting arrays of pointers with function?

 
0
  #3
Nov 21st, 2004
Right, you are confusing 'chars' with 'array of chars' and 'array of array of chars'. It would probably help yourself to describe what the variables are ("this is a char", "this is an array of chars") (to you) and then describe what you want to do (like "copy the array of chars xxxx to a temporary array of chars"), and match that with what the variables are and how you are modifying them (with * or []).

We could just tell you what we see wrong, but then you may still not get the issues; this will be a little work but it is vital that pointer usage becomes second nature to you if you are going to write anything significant in C or C++.

Good luck!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #4
Nov 22nd, 2004
Ok guys,

I'll try to explain what my idea is/was with this function, I want to be able to sort ten names wich are randomly picked and put them in alphabetical order.

char temp= a temporary place to put the first of the names into so the actual first place of the name is free!

stricmp(*naam[i], *naam[i+1]); = this in my opinion will make a comparison of the first and second name to see wether the first name is should be in front of the second, if not it will switch the names by:

strcpy(temp, *naam[i]); = puts the name into a temporary place.

strcpy(*naam[i], *naam[i+1]); = puts the second name into the first place.

strcpy(*naam[i+1], temp); = puts the first name wich was transported to temp into the second place.


naam equals to me a firstname from a person.
temp equals to me as a temporary place to put a certain name wich isn't in it correct place.

Hope you guys understand what I mean by this explanation?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,300
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 225
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #5
Nov 22nd, 2004
Well temp is a single char, so you must be dealing with really trivial names. Similarly, your use of the string functions is wrong because you are feeding them single chars, not strings.

Or: you are not trying to swap names, you are only trying to swap letters. But this won't work with the functions you are using.

And since both of your loops use i, do you know which i you are using in the innermost loop?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #6
Nov 23rd, 2004
- Ive allready changed the char temp into char temp[20] so it can contain not only one letter but indeed the full name!

- If I'm feeding it with char's, then how do I feed him strings, that is the question I'm asking

- I was using the inner [i] to get from the first to the second name, tough, Ive come to realize that:

- don't need it, because I have one loop from 0-9.

- and stricmp is selecting number [i] and [i+1].
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #7
Nov 23rd, 2004
This is what Ive come up with:

  1. for(int i=0;i<9;i++)
  2. {
  3. int resultaat= stricmp(naam[i], naam[i+1]);
  4.  
  5. if (resultaat==1)
  6. {
  7. strcpy(temp, *naam);
  8. strcpy(*naam, *(naam+i));
  9. strcpy(*(naam+i), temp);
  10. }
  11. }

My theorie on it:

I didn't need the second loop because the first as Ive said before is selecting the names one by one.

stricmp will give a value to resultaat of -1, 0 or 1, if the value is one, the selection will step in and threw use of strcpy the two names will get switched!

That's my theorie :!:

Problem is, it doesn't work, so I would really really really appreciate it, if someone could explain to me why it's not working.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,300
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 225
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #8
Nov 23rd, 2004
Are you sorting an array of strings? Or are you sorting an array of pointers to strings? Given the topic, I've assumed the latter.
  1. void mysort(const char *name[], size_t size)
  2. {
  3. size_t i, j;
  4. for ( i = 0; i < size - 1; ++i )
  5. {
  6. for ( j = i; j < size; ++j )
  7. {
  8. if ( strcmp(name[i], name[j]) > 0 )
  9. {
  10. const char *swap = name[i];
  11. name[i] = name[j];
  12. name[j] = swap;
  13. }
  14. }
  15. }
  16. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Sorting arrays of pointers with function?

 
0
  #9
Nov 23rd, 2004
Hi, the idea is just to put ten names, dave, johan, eric, kathy, ... ten names in alphabetical order, to do this I wanted to write this function in wich instead of arrays being used, it would be done by pointers, so selecting and putting them in alphabetical order would be done by pointers :!:

That's what I wanted to do, thanks for the example and yes, it is the second subject that I wanted to do

But, Ive got one more thing to ask, could you please explain the following, what and why you are doing this.

for ( i = 0; i < size - 1; ++i )
for ( j = i; j < size; ++j ) why do you need two different variables i and j ?


if ( strcmp(name[i], name[j]) > 0 ) You've got me here, why two different letters, I assume they are connected towards int i and int j, but why, how...

And why do you make it so that they both have to be bigger then 0 ???????

Thanks again for the help!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,300
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 225
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Sorting arrays of pointers with function?

 
0
  #10
Nov 23rd, 2004
Originally Posted by JoBe
Hi, the idea is just to put ten names, dave, johan, eric, kathy, ... ten names in alphabetical order, to do this I wanted to write this function in wich instead of arrays being used, it would be done by pointers, so selecting and putting them in alphabetical order would be done by pointers :!:
How do you declare the array that you are passing to the function?

Originally Posted by JoBe
But, Ive got one more thing to ask, could you please explain the following, what and why you are doing this.

for ( i = 0; i < size - 1; ++i )
for ( j = i; j < size; ++j ) why do you need two different variables i and j ?
I'm not that hip on sorting, but it looked like the bubble sort to me.

Originally Posted by JoBe
if ( strcmp(name[i], name[j]) > 0 ) You've got me here, why two different letters, I assume they are connected towards int i and int j, but why, how...

And why do you make it so that they both have to be bigger then 0 ???????
Your assumption of -1, 0, 1 as return values is not entierly accurate for strcmp.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC