943,492 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 68394
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 7th, 2004
1

sorting an array of string

Expand Post »
is there any function that helps to sort an array of strings alphabeticly
i need to sort an array alphabeticly which contains names of students
thx 4 yr help
matika
Similar Threads
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Jun 7th, 2004
0

Re: sorting an array of string

void qsort ( void * base, size_t num, size_t width, int (*fncompare)(const void *, const void *) );
Sort using quicksort algorith.
This function uses an implementation of the quicksort algorithm to sort the num elements of an array pointed by base, each element has the specified width in bytes.
The method used to compare each pair of elements is provided by the caller to this function with fncompare parameter, that is a function called one or more times during the sort process.
Parameters.
-------------
base
Pointer to the first element of the array where the sorting process has to be performed.
num
Number of elements in the array pointed by base.
width
Width of each element in the array.
fncompare
Function that compares two elements. This should be provided by the caller to this function and must follow or be casted to a declaration like:

int fncompare (const void * elem1, const void * elem2 );
The function should receive two parameters (elem1 and elem2) that are pointers to elements, and should return an int value with the result of comparing them:

return value description
<0 *elem1 goes before *elem2
0 *elem1 == *elem2
>0 *elem1 goes after *elem2

Return Value.
(none)

Portability.
Defined in ANSI-C.

Example.
  1.  
  2. /* qsort example */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int values[] = { 40, 10, 100, 90, 20, 25 };
  7.  
  8. int compare (const void * a, const void * b)
  9. {
  10. return ( *(int*)a - *(int*)b );
  11. }
  12.  
  13. int main ()
  14. {
  15. int * pItem;
  16. int n;
  17. qsort (values, 6, sizeof(int), compare);
  18. for (n=0; n<6; n++)
  19. {
  20. printf ("%d ",values[n]);
  21. }
  22. return 0;
  23. }
  24.  
  25. Output:
  26. 10 20 25 40 90 100
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
Jun 9th, 2004
0

Re: sorting an array of string

:rolleyes: sorry but may be u did not understood my question
i need to sort an array of characters which contains names of students alphabeticly
and i'm asking if there is a function that do this job quickly (sorting the array of chararcters alphabeticly)
thx
matika
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Jun 9th, 2004
1

Re: sorting an array of string

wait... are you trying to list names of students alphabetically or are you trying to change the characters in each students name around into alphabetical order?
Reputation Points: 12
Solved Threads: 1
Newbie Poster
kalachylde is offline Offline
9 posts
since Jun 2004
Jun 9th, 2004
0

Re: sorting an array of string

hey,I will give you a direction.

You will first have to make a funtion which will compare 2 strings and say which one is greater by comparing the individual chars and returning when it hit's a char which is no equal to the other, like sad sam, (in this case m > d).

Put this into a sort algorithm.

[think this is not much help eh?,see what you can do and you might even suprise yourself.Sometimes just a direction can do wonders.If you still have problems post here,maybe with what you have coded and we will check it out.your will learn more if you do it yourself ]
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 9th, 2004
0

Re: sorting an array of string

i'm trying to list names of students alphabetically
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Jun 9th, 2004
0

Re: sorting an array of string

void arranging()
{
char
temp[30],
name[50][30];

float
temp1,
temp2,
temp3,
temp4,
temp5,
temp6,
grade1[50],
grade2[50],
grade3[50],
grade4[50],
grade5[50],
grade6[50];
int
i=0,
j,
y,
x,
n=0;;



ifstream fin("names.txt");
while(fin)
{
fin.getline(name[i],30);
i++;
}
fin.close();
ifstream finn("grades.txt");
while(finn)
{
finn>>grade1[n];
finn>>grade2[n];
finn>>grade3[n];
finn>>grade4[n];
finn>>grade5[n];
finn>>grade6[n];
n++;
}
finn.close();
for(y=0;y<i;y++)
{
for(j=y+1;j<i;j++)
{
if(name[j][0]<name[y][0])
{
for(x=0;x<30;x++)
{
temp[x]=name[j][x];
temp1=grade1[j];
temp2=grade2[j];
temp3=grade3[j];
temp4=grade4[j];
temp5=grade5[j];
temp6=grade6[j];
}
for(x=0;x<30;x++)
{
name[j][x]=name[y][x];
grade1[j]=grade1[y];
grade2[j]=grade2[y];
grade3[j]=grade3[y];
grade4[j]=grade4[y];
grade5[j]=grade5[y];
grade6[j]=grade6[y];

}
for(x=0;x<30;x++)
{
name[y][x]=temp[x];
grade1[y]=temp1;
grade2[y]=temp2;
grade3[y]=temp3;
grade4[y]=temp4;
grade5[y]=temp5;
grade6[y]=temp6;
}
}
}
}
for(y=0;y<i;y++)
{
for(j=y+1;j<i;j++)
{

if(name[j][0]==name[y][0])
{
if(name[j][1]<=name[y][1])
{
for(x=0;x<30;x++)
{
temp[x]=name[j][x];
temp1=grade1[j];
temp2=grade2[j];
temp3=grade3[j];
temp4=grade4[j];
temp5=grade5[j];
temp6=grade6[j];
}
for(x=0;x<30;x++)
{
name[j][x]=name[y][x];
grade1[j]=grade1[y];
grade2[j]=grade2[y];
grade3[j]=grade3[y];
grade4[j]=grade4[y];
grade5[j]=grade5[y];
grade6[j]=grade6[y];
}
for(x=0;x<30;x++)
{
name[y][x]=temp[x];
grade1[y]=temp1;
grade2[y]=temp2;
grade3[y]=temp3;
grade4[y]=temp4;
grade5[y]=temp5;
grade6[y]=temp6;
}
}
}

}
}
for(y=0;y<i;y++)
{
for(j=y+1;j<i;j++)
{
if(name[j][0]==name[y][0])
{
if(name[j][1]==name[y][1])
{
if(name[j][2]<name[y][2])
{
for(x=0;x<30;x++)
{
temp[x]=name[j][x];
temp1=grade1[j];
temp2=grade2[j];
temp3=grade3[j];
temp4=grade4[j];
temp5=grade5[j];
temp6=grade6[j];
}
for(x=0;x<30;x++)
{
name[j][x]=name[y][x];
grade1[j]=grade1[y];
grade2[j]=grade2[y];
grade3[j]=grade3[y];
grade4[j]=grade4[y];
grade5[j]=grade5[y];
grade6[j]=grade6[y];
}
for(x=0;x<30;x++)
{
name[y][x]=temp[x];
grade1[y]=temp1;
grade2[y]=temp2;
grade3[y]=temp3;
grade4[y]=temp4;
grade5[y]=temp5;
grade6[y]=temp6;
}
}
}
}
}
}
ofstream fout("names.txt");
for(x=1;x<i;x++)
{
fout<<name[x]<<endl;
}
fout.close();
ofstream fott("grades.txt");
for(x=1;x<i;x++)
{
fott<<grade1[x]<<" ";
fott<<grade2[x]<<" ";
fott<<grade3[x]<<" ";
fott<<grade4[x]<<" ";
fott<<grade5[x]<<" ";
fott<<grade6[x]<<endl;
}
fott.close();

}
that's what i wrote it suposes that it took the names from a file
this sort the names it only compares the 3 1st char in the name
i.e. it sort the names according to the 3 first characters
i didnt succed to compare all the characters of the name
if u can help i'll be thxful
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Jun 9th, 2004
0

Re: sorting an array of string

C or C++?

The C solution is qsort, and the only difference from what was already shown is the comparison function you write.

In C++ there is a std::sort.
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. typedef std::vector<std::string>::iterator iter;
  5. typedef std::vector<std::string>::const_iterator citer;
  6. void Show(std::vector<std::string> &alist, const char *label = "")
  7. {
  8. if ( label )
  9. {
  10. std::cout << label << std::endl;
  11. }
  12. for ( citer it = alist.begin(), end = alist.end(); it < end; ++it )
  13. {
  14. std::cout << ' ' << *it << std::endl;
  15. }
  16. }
  17. int main()
  18. {
  19. // Make a list.
  20. std::vector<std::string> mylist;
  21. mylist.push_back("Phil");
  22. mylist.push_back("Bob");
  23. mylist.push_back("Fred");
  24. // Show the list.
  25. Show(mylist, "Before:");
  26. // Sort the list.
  27. std::sort(mylist.begin(), mylist.end());
  28. // Show the list.
  29. Show(mylist, "After:");
  30. return 0;
  31. }
  32. /* my output
  33. Before:
  34.  Phil
  35.  Bob
  36.  Fred
  37. After:
  38.  Bob
  39.  Fred
  40.  Phil
  41. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 11th, 2004
0

Re: sorting an array of string

hey,here a simple way to see which of 2 strings is greater

  1.  
  2. int str_alp(char *s1,char *s2)
  3. {
  4. if(strcmp(s1,s2)==0)return(0); //if they are equal
  5.  
  6. for(int i=0;s1[i]!=0;i++)
  7. {
  8. if(s1[i] > s2[i])return(1);
  9. else if(s1[i] < s2[i])return(2);
  10. }
  11.  
  12. return (2); //hey if they are not equal and s1 not greater than s2 then s2 is greater
  13. }

See if you can use it
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 11th, 2004
0

Re: sorting an array of string

>hey,here a simple way to see which of 2 strings is greater

Why not just check to see whether strcmp returns 1 (for greater than)? Or simply return the value returned by strcmp?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: C output problem
Next Thread in C Forum Timeline: converting from binary to decimal and vice versa





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC