Thread: How do I simplify this code so its smaller

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    How do I simplify this code so its smaller

    input text from record.txt:
    Code:
    to 1 C 261 red
    infinity 4 C 261 blue
    and 1 g_sharp 415 cyan
    beyond 2 g_sharp 415 green
    . . . . .
    My c++ code:
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib> // exit()
    
    using namespace std;
    
    int main()
    {
        string array[100][5];
        string read_array[5];
        string array_element_two = "";
        string array_element_five = "";
        int first_counter = 0;
        int second_counter = 0;
        int third_counter = 0;
    
        ifstream read_record_file("record.txt");
    
        while(read_record_file >> read_array[0] >> read_array[1] >> read_array[2] >> read_array[3] >> read_array[4])
        {
            array[first_counter][0] = read_array[0];
            array[first_counter][1] = read_array[1];
            array[first_counter][2] = read_array[2];
            array[first_counter][3] = read_array[3];
            array[first_counter][4] = read_array[4];
            first_counter++;
        }
    
        read_record_file.close();
    
        for(int i = 0; i < first_counter; i++)
        {
            cout << array[second_counter][0] << " " << array[second_counter][1] << " " << array[second_counter][2] << " " << array[second_counter][3] << " " << array[second_counter][4] << endl;
            second_counter++;
    
            third_counter++;
            if(third_counter == 1)
            {
                array_element_two = array[second_counter][1];
                array_element_five = array[second_counter][4];
            }
            else if(third_counter == 2)
            {
                cout << array_element_five << " " << array[second_counter][4] << " " << array_element_two << " " << array[second_counter][1] << endl;
                array_element_two = array[second_counter][1];
                array_element_five = array[second_counter][4];
            }
            else if(third_counter == 3)
            {
                cout << array_element_five << " " << array[second_counter][4] << " " << array_element_two << " " << array[second_counter][1] << endl;
                array_element_two = array[second_counter][1];
                array_element_five = array[second_counter][4];
            }
        }
    }
    my program output which is good;
    Code:
    to 1 C 261 red
    infinity 4 C 261 blue
    blue cyan 4 1
    and 1 g_sharp 415 cyan
    cyan green 1 2
    beyond 2 g_sharp 415 green
    . . . . .
    
    Process returned 0 (0x0)   execution time : 0.016 s
    Press any key to continue.
    What I want to do it the third_counter value is in a else if condition, if I use this format I will have to have 100 else if conditions to get through the 100 string array, which salem said I shouldn't do, I should use a loop instead.

    So I don't know how, can you help me.

  2. #2
    Guest
    Guest
    You should look more closely for repeating patterns. If you have 0 1 2 3 0 1 2 3 0 1... , then that offers itself to be replaced by a loop. Likewise, if two variable in your system always carry the same value, one can do the job of both. There's lots of room for compactification in your example.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I fixed it, it works now. What I was doing wrong was I was trying to set third counter to 0 after it got to 3 the first time.

    Here's the working code:
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib> // exit()
    
    using namespace std;
    
    int main()
    {
        string array[100][5];
        string read_array[5];
        string array_element_two = "";
        string array_element_five = "";
        int first_counter = 0;
        int second_counter = 0;
        int third_counter = 0;
    
        ifstream read_record_file("record.txt");
    
        while(read_record_file >> read_array[0] >> read_array[1] >> read_array[2] >> read_array[3] >> read_array[4])
        {
            array[first_counter][0] = read_array[0];
            array[first_counter][1] = read_array[1];
            array[first_counter][2] = read_array[2];
            array[first_counter][3] = read_array[3];
            array[first_counter][4] = read_array[4];
            first_counter++;
        }
    
        read_record_file.close();
    
        for(int i = 0; i < first_counter; i++)
        {
            cout << array[second_counter][0] << " " << array[second_counter][1] << " " << array[second_counter][2] << " " << array[second_counter][3] << " " << array[second_counter][4] << endl;
            second_counter++;
    
            third_counter++;
            if(third_counter == 1)
            {
                array_element_two = array[second_counter][1];
                array_element_five = array[second_counter][4];
            }
            else if(third_counter == 2)
            {
                if((array_element_five != ".") && (array[second_counter][4] != ".") && (array_element_two != ".") && (array[second_counter][1] != "."))
                {
                    cout << array_element_five << " " << array[second_counter][4] << " " << array_element_two << " " << array[second_counter][1] << endl;
                }
    
                array_element_two = array[second_counter][1];
                array_element_five = array[second_counter][4];
            }
            else if(third_counter == 3)
            {
                if((array_element_five != ".") && (array[second_counter][4] != ".") && (array_element_two != ".") && (array[second_counter][1] != "."))
                {
                    cout << array_element_five << " " << array[second_counter][4] << " " << array_element_two << " " << array[second_counter][1] << endl;
                }
                array_element_two = array[second_counter][1];
                array_element_five = array[second_counter][4];
                third_counter = 2;
            }
        }
    }
    Here's the input record.txt file:
    Code:
    testing 1 F 349 red
    testing 2 F 349 green
    one 3 g_sharp 415 blue
    two 1 C 261 red
    three 2 A 440 green
    testing 3 F 349 blue
    testing 1 F 349 red
    one 2 g_sharp 415 green
    two 3 C 261 blue
    testing 1 F 349 red
    testing 2 F 349 green
    one 3 g_sharp 415 blue
    two 1 C 261 red
    . . . . .
    And here's the output:
    Code:
    testing 1 F 349 red
    testing 2 F 349 green
    green blue 2 3
    one 3 g_sharp 415 blue
    blue red 3 1
    two 1 C 261 red
    red green 1 2
    three 2 A 440 green
    green blue 2 3
    testing 3 F 349 blue
    blue red 3 1
    testing 1 F 349 red
    red green 1 2
    one 2 g_sharp 415 green
    green blue 2 3
    two 3 C 261 blue
    blue red 3 1
    testing 1 F 349 red
    red green 1 2
    testing 2 F 349 green
    green blue 2 3
    one 3 g_sharp 415 blue
    blue red 3 1
    two 1 C 261 red
    . . . . .
    
    Process returned 0 (0x0)   execution time : 0.031 s
    Press any key to continue.
    I could put the two variables array_element_two, array_element_five into an 2 element array but it would be harder to read, so its good that way for me.

    Thanks for the reply.
    Last edited by jeremy duncan; 05-16-2016 at 05:01 PM. Reason: 2 to 3 first sentence

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    In case your interested in how the code looks now here it is:

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void work_with_string_int_to_find_condition(string array_element_five, string array[][5], int previous_element_one, int current_element_one, int second_counter)
    {
        cout << array_element_five << " " << array[second_counter][4] << " " << previous_element_one << " " << current_element_one << endl;
    }
    
    int main()
    {
        string array[100][5];
        string read_array[5];
        string array_element_two = "";
        string array_element_five = "";
        int first_counter = 0;
        int second_counter = 0;
        int third_counter = 0;
        int previous_element_one = 0;
        int current_element_one = 0;
    
        ifstream read_record_file("record.txt");
    
        while(read_record_file >> read_array[0] >> read_array[1] >> read_array[2] >> read_array[3] >> read_array[4])
        {
            array[first_counter][0] = read_array[0];
            array[first_counter][1] = read_array[1];
            array[first_counter][2] = read_array[2];
            array[first_counter][3] = read_array[3];
            array[first_counter][4] = read_array[4];
            first_counter++;
        }
    
        read_record_file.close();
    
        for(int i = 0; i < first_counter; i++)
        {
            cout << array[second_counter][0] << " " << array[second_counter][1] << " " << array[second_counter][2] << " " << array[second_counter][3] << " " << array[second_counter][4] << endl;
            second_counter++;
    
            third_counter++;
            if(third_counter == 1)
            {
                array_element_two = array[second_counter][1];
                previous_element_one = stoi(array_element_two);
                array_element_five = array[second_counter][4];
            }
            else if(third_counter == 2)
            {
                if((array_element_five != ".") && (array[second_counter][4] != ".") && (array_element_two != ".") && (array[second_counter][1] != "."))
                {
                    array_element_two = array[second_counter][1];
                    current_element_one = stoi(array_element_two);
                    work_with_string_int_to_find_condition(array_element_five, array, previous_element_one, current_element_one, second_counter);
                    array_element_two = array[second_counter][1];
                    previous_element_one = stoi(array_element_two);
                }
    
                array_element_five = array[second_counter][4];
            }
            else if(third_counter == 3)
            {
                if((array_element_five != ".") && (array[second_counter][4] != ".") && (array_element_two != ".") && (array[second_counter][1] != "."))
                {
                    array_element_two = array[second_counter][1];
                    current_element_one = stoi(array_element_two);
                    work_with_string_int_to_find_condition(array_element_five, array, previous_element_one, current_element_one, second_counter);
                    array_element_two = array[second_counter][1];
                    previous_element_one = stoi(array_element_two);
                }
    
    
                array_element_five = array[second_counter][4];
                third_counter = 2;
            }
        }
    }
    Here's the record.txt file:
    Code:
    testing 1 F 349 red
    testing 2 F 349 green
    one 3 g_sharp 415 blue
    two 1 C 261 red
    three 2 A 440 green
    testing 3 F 349 blue
    testing 1 F 349 red
    one 2 g_sharp 415 green
    two 3 C 261 blue
    testing 1 F 349 red
    testing 2 F 349 green
    one 3 g_sharp 415 blue
    two 1 C 261 red
    . . . . .
    And here's the output which is correct:
    Code:
    testing 1 F 349 red
    testing 2 F 349 green
    green blue 2 3
    one 3 g_sharp 415 blue
    blue red 3 1
    two 1 C 261 red
    red green 1 2
    three 2 A 440 green
    green blue 2 3
    testing 3 F 349 blue
    blue red 3 1
    testing 1 F 349 red
    red green 1 2
    one 2 g_sharp 415 green
    green blue 2 3
    two 3 C 261 blue
    blue red 3 1
    testing 1 F 349 red
    red green 1 2
    testing 2 F 349 green
    green blue 2 3
    one 3 g_sharp 415 blue
    blue red 3 1
    two 1 C 261 red
    . . . . .
    
    Process returned 0 (0x0)   execution time : 0.016 s
    Press any key to continue.
    You know, coding is tiring for me, because I have to think.

  5. #5
    Guest
    Guest
    Nice work! One thing I alluded to with my comment about variables having the same value is the for-loop starting on line #38. Hiding all other variables for a moment, don't i and second_counter carry the same value, whenever they're being used? (unless work_with_string_int_to_find_condition() modifies the latter)
    Code:
    int first_counter = 0;
    int second_counter = 0;
    int third_counter = 0;
    (...)
    for(int i = 0; i < first_counter; i++) // i goes 0, 1, 2...
    {
        (...)
        second_counter++; // goes 0, 1, 2...
    }
    If you think that's the case, then see if you can get by with using just one variable for both. I don't want to take the challenge from you, if it's homework.

    Another thing I noticed is that you're using std::string to store characters:
    Code:
    string array_element_two = "";
    (...)
    array_element_two = array[second_counter][1];
    Keep in mind that std::string contains individual elements of type char, not string. Your code works because of automatic conversions, but you may have deceived yourself here without knowing. As an exercise, try:
    Code:
    char array_element_five;
    (...)
    array_element_five = array[second_counter][4];
    (...)
    if((array_element_five != '.') && ... etc.) // single quotes to check against another character
    You also don't need the temporary array string read_array[5] used in the loop on line #19, think about why.
    Last edited by Guest; 05-17-2016 at 05:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New Smaller and Smaller Arrays.
    By jastiv in forum Game Programming
    Replies: 1
    Last Post: 07-02-2012, 01:37 PM
  2. Can someone help me simplify this?
    By Shackdaddy836 in forum C Programming
    Replies: 1
    Last Post: 10-07-2011, 02:56 PM
  3. Simplify Fraction Help
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2009, 01:17 PM
  4. Need to simplify/shorten this code. Help.
    By Lonck in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2007, 04:23 AM
  5. simplify this?
    By markg in forum C Programming
    Replies: 2
    Last Post: 10-25-2005, 08:09 PM

Tags for this Thread