Newby array problem

This is a discussion on Newby array problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I have never done anything with multi dimensinal arrays before, and got stuck with a simple code, what's supposed ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    19

    Newby array problem

    Hi,
    I have never done anything with multi dimensinal arrays before, and got stuck with a simple code, what's supposed to fill array "Table" with zeros. I keep getting error: error C2059: syntax error : '['
    I'd be greateful, if sb told me, what's wroing with it.
    Code:
    int Table[12][12];
    for ( int a = 0; a < 12; a++ ) {
    	for ( int b = 0; b < 12; b++ ) {
    		Table[a][b] = [0][0];
    	}
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you're not assigning right, just change the [0][0] to 0.

    Code:
    int Table[12][12];
    for ( int a = 0; a < 12; a++ ) {
    	for ( int b = 0; b < 12; b++ ) {
    		[b]Table[a] = 0; //nothing special here, just a regular assignment
    	}
    }
    another shortcut would be to do this:
    Code:
    int Table[12][12]={0};
    That initializes the entire array of arrays to zero.
    Last edited by major_small; 11-28-2005 at 01:02 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Thanx

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Here is an easier way to initialize an array:
    Code:
    int Table[12][12] = {0};
    When you use an initializer list, which is what is on the right hand side, and you don't specify all the values for each index position, then the positions you don't specify will be initialized with 0. So, all you have to do is specify 0 for the first element in the array, and since the rest of the elements aren't specified, they will be initialized with 0.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    thanks for reading my post
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    19
    Thanx for all the answers, but I have one more question: Is there something like .length value in Java? I tryed sizeof() But it just repeats the action 12x12 times, when used in loop.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    not really, if you're using C-style strings, you could use strlen(char*) from the <cstring> header.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    In C++, you would generally use the standard vector class for this purpose. That class has a length() method. The syntax might look odd at first, but vectors are easier and safer in the long run than regular C style arrays. This is the equivalent to a 12x12 int array initialized to 0:
    Code:
    std::vector<std::vector<int> > Table(12, std::vector<int>(12));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM

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