Thread: 2 dimensional array!!!

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    2 dimensional array!!!

    As of now my program as the user input an expression like *2x and then the program outputs it as

    *
    2
    x

    I am wanting to display zeros next to the elements, 2 columns of zeros so the output will be

    * 0 0
    2 0 0
    x 0 0

    This is my code
    Code:
    int main()
    {
    
    string s;
    cout << "Input the prefix expression.\n" ;
    cin >> s;
    cout << "The expression tree is:\n" ;
    
    int row = s.length();
    int col = 2;
    int array[row][col];
    int i, j, c;
    
    for (j = 0; j < row; j++)
    for (c = 0; c < col; c++)
    for (i = 0; i < s.length(); i++)
    {
    cout << s[i] << array[row][col] << endl ;
    }
    it outputs

    *2293584
    22293584
    x2293584
    *2293584
    22293584
    x2293584
    *2293584
    22293584
    x2293584
    *2293584
    22293584
    x2293584

    Edit/Delete Message
    Last edited by whitey300; 10-21-2009 at 03:06 PM. Reason: email

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Were you planning to ever put something in array[row][col], or just print out what was already there? OH: and only print new lines when you want a new line.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by whitey300 View Post
    Code:
    int main()
    {
    
    string s;
    cout << "Input the prefix expression.\n" ;
    cin >> s;
    cout << "The expression tree is:\n" ;
    
    int row = s.length();
    int col = 2;
    int array[row][col];
    int i, j, c;
    
    for (j = 0; j < row; j++)
    for (c = 0; c < col; c++)
    for (i = 0; i < s.length(); i++)
    {
    cout << s[i] << array[row][col] << endl ;
    }
    it outputs

    *2293584
    ...
    Like hell it does! That cannot even compile on a conforming C++ compiler because it is using variables rather than constants for the definition of array dimensions. Even if some non-conformant compiler allows such a bazaar thing to compile as a compiler-specific language extension, then it contains a buffer overrun anyway because array[row][col] does not exist.
    You know that row and col don't change right? So you're attempting to output the value of the same out of bounds array entry over and over!

    I suppose you're using gcc, which has weirdo things like this?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by iMalc
    Even if some non-conformant compiler allows such a bazaar thing to compile as a compiler-specific language extension
    Bizarre, iMalc, not bazaar. Don't diss my favourite version control system

    Anyway, whitey300, if you are using g++, compile with the -pedantic flag. I assume that you are already compiling with -Wall.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    Bizarre, iMalc, not bazaar. Don't diss my favourite version control system
    Damn, I know that too. What the hell was I thinking!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And for that matter, given such a thing is legal in C, it's not really that bizarre of an extension.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM