Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
R

raeiko

@raeiko
About
Posts
6
Topics
3
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How to read names from a file and sort them in alphabetical order
    R raeiko

    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 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 \*/
    

    /* 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 */

    C / C++ / MFC algorithms help tutorial question

  • How to read names from a file and sort them in alphabetical order
    R raeiko

    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 */

    C / C++ / MFC algorithms help tutorial question

  • C program - modify bubble sort - need someone to check my code
    R raeiko

    Hello, i need to sort out this problem: "The bubble sort represented in fig 6.15 of your text book is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort. After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass and so on." Could someone check my code? I should get an output like this but i don't:confused:: After pass 0: 2 4 6 8 10 12 68 45 37 89 After pass 1: 2 4 6 8 10 12 45 37 68 After pass 2: 2 4 6 8 10 12 37 45 After pass 3: 2 4 6 8 10 12 37 After pass 4: 2 4 6 8 10 12 After pass 5: 2 4 6 8 10 After pass 6: 2 4 6 8 After pass 7: 2 4 6 After pass 8: 2 4 There is something wrong with the loop... Thanks a lot!!! raeiko

    /* Program that modifies the bubble sort array in fig. 6.15 */

    #include <stdio.h>
    #define SIZE 10

    int main ( void )
    {
    int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; /* declare and initialize array a */
    int pass; /* passes counter */
    int i; /* comparisons counter */
    int hold; /* temporary location used to swap arrays elements */
    int NumOfComparisons = 0;

    printf( "Data items in original order:\\n\\n" ); 
    
    for ( i = 0; i <SIZE; i++ ){ /\* display array output \*/
    	printf( "%4d", a\[ i \] );
    } /\* end of for loop \*/
    
    printf( "\\n\\n" );
    
    /\* bubble sort \*/
    for ( pass = 1; pass < 9; pass++ ){
    	for ( i = 0; i < 9 - pass; i++ ){
    		if ( a\[ i \] > a\[ i + 1\] ){
    		hold = a\[ i \];
    		a\[ i \] = a\[ i + 1\];
    		a\[ i + 1\] = hold;
    		}/\* end of if \*/
    
    	NumOfComparisons = NumOfComparisons + 1;
    
    	}/\* end of inner for \*/
    
    	for ( i = 0; i < SIZE - pass; i++ ){
    		printf( "After pass %d: %4d\\n", pass, a\[ i \] ); /\* the problem should be here \*/
    		printf( "\\n " );
    	}/\* end of for \*/
    
    }/\* end of outer for \*/
    
    printf( "Data items in ascending order:\\n\\n" );
    
    for ( i = 0; i < SIZE; i++ ){ /\* output array in ascending order \*/
    	printf( "%4d", a\[ i \] );
    }
    
    printf( "\\n\\n" );
    
    printf( "Number of comparisons: %d\\n", NumOfComparisons );
    

    return 0;
    }

    C / C++ / MFC data-structures performance help question code-review

  • c program that inputs 5 pairs of integers and determine if the second one is multiple of the first one
    R raeiko

    Thank you so much!!! Now i see what was wrong:-) raeiko

    C / C++ / MFC help question

  • c program that inputs 5 pairs of integers and determine if the second one is multiple of the first one
    R raeiko

    If i don't ask too much,could you please show me? This thing has been driving me crazy for one week...and i guess this is still nothing in comparison to advanced C...I feel a bit stupid... Thanks again, raeiko

    C / C++ / MFC help question

  • c program that inputs 5 pairs of integers and determine if the second one is multiple of the first one
    R raeiko

    Hello everybody, i need some help with my code. The program seems to work,the problem is that it only outputs the result for the last pair of numbers and repeat it for 5 times. Could you please help me to understand what i'm doing wrong? Thanks a lot, raeiko /* Program that reads a pair of numbers and determines whether the second number is multiple of the first one */ #include <stdio.h> int multiple ( int j, int z); /* function prototype */ int main( void ) /* function main begins program execution */ { int num1, num2, x, i; /* declare variables */ for ( x = 1; x <= 5; x++ ){ printf( "Enter the first number:" ); /* prompt for input */ scanf_s("%d", &num1 ); /* read number from user */ if ( num1 != 0){ printf( "Enter the second number:" ); /* prompt for input */ scanf_s("%d", &num2 ); /* read number from user */ } else { break; printf( "\nBroke from loop because num1 must be greater than 0\n" ); /* break loop if num1 == 0 */ } } multiple( num1, num2); return 0; } int multiple ( int j, int z ) /* copy of the argment to function */ { int y; /* counter */ int result; result = z % j; for ( y = 1; y <= 5; y++ ) if( result == 0){ printf( "%d is multiple of %d\n", z, j ); } else { printf( "%d is not multiple of %d\n", z, j ); } return result; } /* end of multiple function */

    C / C++ / MFC help question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups