Thread: Unexpected output

  1. #1
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10

    Question Unexpected output

    So the latest challenge from jumping into c++ is as following.
    Code:
    Write a function that builds the multiplication table of arbitrary dimensions
    This chapter also talks a ton about 2d arrays.

    So I've built my program thus far as this.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int drawTable(int,int);
    
    
    int main()
    {
        drawTable(5,5);
    }
    /*
    0 0 0 0 0
    0 1 2 3 4
    0 2 4 6 8
    0 3 6 9 12
    0 4 8 12 16
    */
    int drawTable(int x, int y)
    {
        int table[x][y];
        int xMax = x;
        int yMax = y;
        int xSlider = 0;
        int ySlider = 0;
        bool loop = true;
        while(loop == true)
        {
    
    
            table[xSlider][ySlider] = xSlider * ySlider;
            if(xSlider < xMax && ySlider == yMax)
            {
                loop == false;
            }
            if(ySlider < yMax)
            {
                ySlider++;
            }
            else if(ySlider == yMax)
            {
                xSlider++;
                ySlider = 0;
            }
            cout << table[xSlider][ySlider];
            cin.get();
    
    
        }
        return 0;
    }
    So basically, the idea is that I can use the arrays dimensions as a placeholder, and just multiple them to get that specific spot, so table[0][0] = 0, [0][1] = 0 and so on. However the output actually seems to be randomly generated numbers so I'd like to ask, what am I doing wrong? Am I on the right track? Or have I missed the bus stop completely.

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you are printing the values before you calculate them (that is to say, by the time you reach the cout statement, you've changed the values of your index variables, and so you are not printing the value you calculated at the top of the loop). Print before you move your sliders.

  3. #3
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10
    Thanks for the reply, I've moved cout to the beginning of the loop, and initialized [0][0] to be set to 0 so I don't get whatever it held before. First 2 numbers always print out ok, after that I'm getting random garbage again.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're loop = false is broken in two ways -- one, it's a comparison instead of an assignment (your compiler will give you a warning about that line, if you will let it) and two, you are (or would be) setting it to false way too early.

  5. #5
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10
    I can't believe I didn't notice I used == whoops. I guess tired programming will do that. Also, I don't understand what you mean by saying I set it to false too early, If its conditions are met the loop should be done solving the table. At least that's how I see it.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CrimsonMagi View Post
    I can't believe I didn't notice I used == whoops. I guess tired programming will do that. Also, I don't understand what you mean by saying I set it to false too early, If its conditions are met the loop should be done solving the table. At least that's how I see it.
    If your table is only one row long, then yes. If you want more than one row, then you could be in trouble.

  7. #7
    Registered User CrimsonMagi's Avatar
    Join Date
    Nov 2013
    Location
    Oregon
    Posts
    10

    Thumbs up

    Thank you for the continued replies tabstop. at the time of writing this, I had been about 2 days without sleep so I wasn't thinking very well. So first, let me say thank you for the patience. Second I've got it fully functional and i'll leave it here in case anyone else ever needs the solution. Feel free to give me feedback how I could make this program better.

    Code:
    //Multiplication table, jumping into c++ 2d array
    #include <iostream>
    
    
    using namespace std;
    
    
    int drawTable(int,int);
    
    
    int main()
    {
        drawTable(5,5);
        return 0;
    }
    /* table added in for visual representation of the problem.
    0 0 0 0 0
    0 1 2 3 4
    0 2 4 6 8
    0 3 6 9 12
    0 4 8 12 16
    */
    int drawTable(int x, int y)
    {
        int table[x][y];
        int xMax = x;
        int yMax = y;
        int xSlider = 0;
        int ySlider = 0;
        table[0][0] = 0;
        bool loop = true;
        while(loop == true)
        {
    
    
    
    
            cout << table[xSlider][ySlider] << " ";
            ySlider++;
            if (ySlider == yMax)
            {
                ySlider = 0;
                xSlider++;
                cout << endl;
            }
            if (xSlider == xMax)
            {
                loop = false;
            }
            table[xSlider][ySlider] = xSlider * ySlider;
    
    
        }
        return 0;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can literally do it with just two for loops that are nested and normal arithmetic.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>int table[x][y];
    This doesn't work. All arrays must have a size that is known at compile-time. I would be surprised you don't get any warnings for this.
    You should use a 2D vector instead. There are examples out there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Re:Elysia's comment.

    You can also use dyn_array if your compiler supports it, and you enable C++14 features.

    But yeah, that syntax is a compiler extension in C++.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    IIRC, dynarray was delayed and will make it into a TS after C++14 is finalized.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Could be, I had done a quick google to make sure it was in C++14, and it showed it was, but my source wasn't official.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It was originally approved, but was subsequently dropped later as national body comments voiced concerns: https://www.ibm.com/developerworks/c...part_1?lang=en
    It looks like there are still many questions and issues still unanswered regarding dynarray.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected output
    By juice in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2012, 11:13 AM
  2. Unexpected output
    By juice in forum C Programming
    Replies: 24
    Last Post: 11-18-2011, 11:18 PM
  3. Unexpected output
    By GolDRoger in forum C Programming
    Replies: 9
    Last Post: 11-18-2011, 02:49 PM
  4. Unexpected Output in structure
    By bhagwat_maimt in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2006, 10:50 PM
  5. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 09:03 PM