Updating 2d array

This is a discussion on Updating 2d array within the C++ Programming forums, part of the General Programming Boards category; Code: // fill for (int i=0; i<10;i++) for (int j=0; j<10;j++) square[i][j]= '0'; // update square[1][1]= '4'; square[3][4]= '1'; square[7][7]= ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    Updating 2d array

    Code:
    // fill
    for (int i=0; i<10;i++)
     for (int j=0; j<10;j++)
      square[i][j]= '0';
    
    // update
    square[1][1]= '4';
    square[3][4]= '1';
    square[7][7]= '7';
    
    //print
    for (int i=0; i<10;i++) {
     for (int j=0; j<10;j++)
       cout << square[i][j];
     cout << endl;
    }
    The 2D array is not being updated, and only 0's are printed. What is wrong ?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
       char square[10][10];
       // fill
       for ( int i=0; i<10;i++ )
          for ( int j=0; j<10;j++ )
             square[i][j]= '0';
    
       // update
       square[1][1]= '4';
       square[3][4]= '1';
       square[7][7]= '7';
    
       //print
       for ( int i=0; i<10;i++ )
       {
          for ( int j=0; j<10;j++ )
             cout << square[i][j];
          cout << endl;
       }
    }
    
    /* my output
    0000000000
    0400000000
    0000000000
    0000100000
    0000000000
    0000000000
    0000000000
    0000000700
    0000000000
    0000000000
    */
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

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