2 dimensional array!!!

This is a discussion on 2 dimensional array!!! within the C++ Programming forums, part of the General Programming Boards category; As of now my program as the user input an expression like *2x and then the program outputs it as ...

  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 04:06 PM. Reason: email

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,308
    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
    22,119
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,308
    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,185
    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, 08:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21