| | |
sorting an array of string
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
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.
C Syntax (Toggle Plain Text)
/* qsort example */ #include <stdio.h> #include <stdlib.h> int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main () { int * pItem; int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) { printf ("%d ",values[n]); } return 0; } Output: 10 20 25 40 90 100
Real Eyes Realize Real Lies
My Resume
My Resume
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
]
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
] •
•
Join Date: May 2004
Posts: 19
Reputation:
Solved Threads: 0
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
{
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
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.
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.
C Syntax (Toggle Plain Text)
#include <iostream> #include <vector> #include <algorithm> typedef std::vector<std::string>::iterator iter; typedef std::vector<std::string>::const_iterator citer; void Show(std::vector<std::string> &alist, const char *label = "") { if ( label ) { std::cout << label << std::endl; } for ( citer it = alist.begin(), end = alist.end(); it < end; ++it ) { std::cout << ' ' << *it << std::endl; } } int main() { // Make a list. std::vector<std::string> mylist; mylist.push_back("Phil"); mylist.push_back("Bob"); mylist.push_back("Fred"); // Show the list. Show(mylist, "Before:"); // Sort the list. std::sort(mylist.begin(), mylist.end()); // Show the list. Show(mylist, "After:"); return 0; } /* my output Before: Phil Bob Fred After: Bob Fred Phil */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
hey,here a simple way to see which of 2 strings is greater
See if you can use it
C Syntax (Toggle Plain Text)
int str_alp(char *s1,char *s2) { if(strcmp(s1,s2)==0)return(0); //if they are equal for(int i=0;s1[i]!=0;i++) { if(s1[i] > s2[i])return(1); else if(s1[i] < s2[i])return(2); } return (2); //hey if they are not equal and s1 not greater than s2 then s2 is greater }
See if you can use it
>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?
Why not just check to see whether strcmp returns 1 (for greater than)? Or simply return the value returned by strcmp?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Sorting an Array of String (Visual Basic 4 / 5 / 6)
- Problem of sorting words of each string using pointers (C++)
- bubble sorting in an array (C)
- Conver int Array into a String (Java)
- Array to String (PHP)
Other Threads in the C Forum
- Previous Thread: Please help with algorithm
- Next Thread: New Type
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






