How to read names from a file and sort them in alphabetical order
-
Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:
#define SIZE 30
int main()
{
char name[ SIZE ]; /* Data records */FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/ /\* fopen opens file; exits program if file cannot be opened \*/ if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) { printf( "File could not be opened\\n" ); } /\* End if \*/ /\* Read records from file \*/ else { printf( "%s\\n", "Name" ); fscanf( fpPtr, "%s", name ); /\* While not end of file \*/ while( !feof( fpPtr ) ) { printf( "%s\\n", name ); fscanf( fpPtr, "%s", name ); } /\* End while \*/ fclose( fpPtr ); /\* fclose closes the file \*/ } /\* End else \*/ return 0; /\* Indicates that the program terminated successfully \*/
} /* End Main */
-
Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:
#define SIZE 30
int main()
{
char name[ SIZE ]; /* Data records */FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/ /\* fopen opens file; exits program if file cannot be opened \*/ if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) { printf( "File could not be opened\\n" ); } /\* End if \*/ /\* Read records from file \*/ else { printf( "%s\\n", "Name" ); fscanf( fpPtr, "%s", name ); /\* While not end of file \*/ while( !feof( fpPtr ) ) { printf( "%s\\n", name ); fscanf( fpPtr, "%s", name ); } /\* End while \*/ fclose( fpPtr ); /\* fclose closes the file \*/ } /\* End else \*/ return 0; /\* Indicates that the program terminated successfully \*/
} /* End Main */
Why write your own bubble sort when the standard library has a quicksort built in. That way, you get a fast sort routine that you know works... 99.9999% of the time, it's better to use someone else's library code rather than write your own.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Why write your own bubble sort when the standard library has a quicksort built in. That way, you get a fast sort routine that you know works... 99.9999% of the time, it's better to use someone else's library code rather than write your own.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Stuart Dootson wrote:
Why write your own bubble sort when the standard library has a quicksort built in.
Perhaps it's an academic exercise.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Stuart Dootson wrote:
Why write your own bubble sort when the standard library has a quicksort built in.
Perhaps it's an academic exercise.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
That's what I figured - in which case, helping him isn't helping him :-)
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:
#define SIZE 30
int main()
{
char name[ SIZE ]; /* Data records */FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/ /\* fopen opens file; exits program if file cannot be opened \*/ if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) { printf( "File could not be opened\\n" ); } /\* End if \*/ /\* Read records from file \*/ else { printf( "%s\\n", "Name" ); fscanf( fpPtr, "%s", name ); /\* While not end of file \*/ while( !feof( fpPtr ) ) { printf( "%s\\n", name ); fscanf( fpPtr, "%s", name ); } /\* End while \*/ fclose( fpPtr ); /\* fclose closes the file \*/ } /\* End else \*/ return 0; /\* Indicates that the program terminated successfully \*/
} /* End Main */
raeiko wrote:
...but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them.
You first need to get a working bubble-sort algorithm before moving on.
int nNumbers[] = { 1, 7, 4, 10, 3, 5, 6, 9 };
int nCount = sizeof(nNumbers) / sizeof(nNumbers[0]);
...
BubbleSort(nNumbers, nCount);Work on this, and when you get stuck, we'll be glad to help.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:
#define SIZE 30
int main()
{
char name[ SIZE ]; /* Data records */FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/ /\* fopen opens file; exits program if file cannot be opened \*/ if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) { printf( "File could not be opened\\n" ); } /\* End if \*/ /\* Read records from file \*/ else { printf( "%s\\n", "Name" ); fscanf( fpPtr, "%s", name ); /\* While not end of file \*/ while( !feof( fpPtr ) ) { printf( "%s\\n", name ); fscanf( fpPtr, "%s", name ); } /\* End while \*/ fclose( fpPtr ); /\* fclose closes the file \*/ } /\* End else \*/ return 0; /\* Indicates that the program terminated successfully \*/
} /* End Main */
-
Hello guys, i need you help with a C program that reads names from a .txt file and sort them in alphabetical order. I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them. Could you please give me some hints? Thanks a lot, raeiko Here is my code:
#define SIZE 30
int main()
{
char name[ SIZE ]; /* Data records */FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/ /\* fopen opens file; exits program if file cannot be opened \*/ if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) { printf( "File could not be opened\\n" ); } /\* End if \*/ /\* Read records from file \*/ else { printf( "%s\\n", "Name" ); fscanf( fpPtr, "%s", name ); /\* While not end of file \*/ while( !feof( fpPtr ) ) { printf( "%s\\n", name ); fscanf( fpPtr, "%s", name ); } /\* End while \*/ fclose( fpPtr ); /\* fclose closes the file \*/ } /\* End else \*/ return 0; /\* Indicates that the program terminated successfully \*/
} /* End Main */
Hello, I'm writing this program for an academical assignment that is why i need to use the bubble sort algorithm, even though, from what i undersand, is not the most suitable solution. I have tried to go on with my code but it's still not working and i'm getting more and more confused because i don't have really clear in my mind how to handle pointers... Here is what i have done but i'm sure it's completely wrong
/* Program that takes a text file as input and produces an output file that has all the original lines in alphabetical order */
#include <stdio.h>
#include <string.h>
#define SIZE 30int main()
{
char name[ SIZE ]; /* Data records */FILE \*fpPtr; /\* fpPtr = sorted\_file.txt pointer \*/ /\* fopen opens file; exits program if file cannot be opened \*/ if ( ( fpPtr = fopen( "sorted\_file.txt", "r" ) ) == NULL ) { printf( "File could not be opened\\n" ); } /\* End if \*/ /\* Read records from file \*/ else { printf( "%s\\n", "Name" ); fscanf( fpPtr, "%s", name ); /\* While not end of file \*/ while( !feof( fpPtr ) ) { printf( "%s\\n", name ); fscanf( fpPtr, "%s", name ); } /\* End while \*/ fclose( fpPtr ); /\* fclose closes the file \*/ } /\* End else \*/
/* sort */
for ( pass = 1; pass < SIZE; pass++ ){ /* loop to cotrol passes */
for ( counter = 0; counter < SIZE - 1; counter ++ ){ /* loop to control number of comparesons per pass */
if ( *name [ counter ] > *name [ counter + 1 ] ) {
hold = *name[ counter ];
*name[ counter ] = *name[ counter + 1 ];
*name[ counter + 1 ] = hold;
} /* End if */
}/* end of inner for */
}/* end of outer for */return 0; /\* Indicates that the program terminated successfully \*/
} /* End Main */