Thread: How can I improve this working code?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    How can I improve this working code?

    This is my aprox 2nd or 3rd time I have ever dealt with 2d arrays.
    I am taking an unknown amount of data, putting it into a single array. Then spiting it into a 2d array.

    Just knowing their probably is a different more efficient way to do this. Their has to be a better way to do this?

    So that would be a challenge wouldn't it?

    The thing I did get really stuck on is getting "strlen" off the second element of a 2d array. that is why I coded it like I did. Because I could not figure that one out.


    what is a more efficient to take an unknown amount of data and deal with it accordingly to the specs.

    specs
    Code:
    working with x amount of lines, or, unknown amount of data. 
    
    Open file  then add it into a single array, then sort it 
    into a 2d array using only the first line and third line. 
    Then print output to screen. Using the lest amount
    of lines of code needed to do so. 
    
    No using the compilers abilities to read everything on one line.
    Must be formatted for readability.
    I'd just like to know a more efficient way to write this.
    my code.
    Code:
     
     
     #include <stdio.h>
     #include <string.h>
     
     
     #define MAXBUF 1024
     
     
     int main (void)
    {
    
        FILE *fp;
        char music[MAXBUF];
        char check_music[2][MAXBUF];
        fp = fopen("enter_sandman", "r");
        char c = '\n';
        int a = 0;
        
        // fill array with word including end line
        while (( c = fgetc(fp) )!= EOF )
            music[a++] = c;
        
        fclose(fp); 
    
        
        
        // a whole bunch of stuff to keep
        // track of everything. 
        
        int b = 0, count = 0, check = 0, move_arr = 0, x = 0;
        int string1 = 0, string2 = 0, z = 0;
        
        
        //working with x amount of lines
        // need 1st and 3rd
        // ........ can the rest
        
        
        //only need to look at 3 lines
        while ( b != 3 )
        { 
        // reset to go back into inner loop
            check = 0;
            
            while ( check != 1 )
            {    // marks for 2nd line
                if (count == 1)
                {
                    //skips second line
                    // just runs it out to the end
                    while ( music[++x] != '\n');
                    // prevents it from returning inside here
                    count++;
                     
                }
                else
                { 
                    
                    //move_arr keep alignment with roll
                    // z keeps alignment with x 
                    check_music[move_arr][z++] = music[x];
                 
                    x++;
                    
                    // switch off 1st and 3rd line
                    // to keep track of length of
                    // each line
                    switch (count)
                    {
                        case 0:
                            string1++;
                            break;
                        case 2:
                            string2++;
                            break;
                        default:
                            break;
                    }
             
            
            // if end line 
                    if ( music[x] == '\n')
                    {
                        // count is 0
                        // now it is one
                        // marked for 2nd line
                        count++;
                         
                    // moves the 2d 1st element to 
                    // get 3rd line 
                        move_arr++;
                        // kicks it out of this
                        // inner loop
                        check = 1;
                        // resets lenght count
                        //to copy 2nd elements
                        // check_music[1][z = 0 -> length]
                        z = 0;
                         
                 
                    }
                }
                 
            }
            b++;
        }    
         
         // self explanatory? 
         
            int g = 0;
            for ( ; g < string1; g++)
                printf("%c", check_music[0][g]);
                        
        printf("\n");
            
           
            for ( g = 0; g < string2; g++)
                printf("%c", check_music[1][g]);
        
        printf("\n");
        
        
         
    return 0;     
    }
    output
    Code:
    userx@slackwhere:~/bin
    $ ./last_3_words
    Say your prayers, little one
    
    To include everyone
    data file, something everyone might identify with.
    Code:
    Say your prayers, little one
    Don't forget, my son
    To include everyone
    
    Tuck you in, warm within
    Keep you free from sin
    Till the Sandman he comes
    
    Sleep with one eye open
    Gripping your pillow tight
    
    Exit: light
    Enter: night
    Take my hand
    We're off to never never land
    
    Something's wrong, shut the light
    Heavy thoughts tonight
    And they aren't of Snow White
    
    Dreams of war, dreams of liars
    Dreams of dragon's fire
    And of things that will bite
    
    Sleep with one eye open
    Gripping your pillow tight
    Last edited by userxbw; 11-10-2017 at 11:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Improve my code
    By hariharaan in forum C Programming
    Replies: 0
    Last Post: 05-20-2015, 02:05 AM
  2. Can I improve my code?
    By advancedk in forum C Programming
    Replies: 1
    Last Post: 07-27-2008, 07:47 AM
  3. Help me improve my code!
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 09-19-2006, 10:04 AM
  4. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM

Tags for this Thread