I am having a hard time understanding this...

This is a discussion on I am having a hard time understanding this... within the C++ Programming forums, part of the General Programming Boards category; So here's the code: Code: #include <iostream> using namespace std; int main() { int t,i, nums[3][4]; for(t = 0; t ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    14

    I am having a hard time understanding this...

    So here's the code:

    Code:
    #include <iostream>
    using namespace std;
    int main() {
    	int t,i, nums[3][4];
    	for(t = 0; t < 3; ++t){
    		for(i=0;i < 4; ++i) {
    			nums[t][i] = (t*4)+i+1;
    			cout << nums[t][i] << ' ';
    		}
    		cout  << "\n";
    	}
    	return 0;
    }
    And I just don't understand what this will do! My book says it will put the numbers 1 through eleven into each box, but I do not see how this is possible (I did compile this, I just don't understand how the answer was created).

    So, when the first for loop runs, t = 0, so it increments once, then it runs the second for loop, and the I = 0, so it increments until it does not equal a number less than four (which will be four). Then the mathematical thing, that assigns the numbers to the arrays, puts the number nums[t][i], which would be: nums[1][4]. The number that is put there, is (4 * t) + i + 1. So the number put in nums[1][4] is 6. But when I run it I get this:

    1 2 3 4
    5 6 7 8
    9 10 11 12


    I know I am misunderstanding something. Which dimension comes first, when assigning things to an array? does: nums[1][4], the one refers to 1 down, or one across?

    I refuse to label this thread as something as personally insulting as: "Noob question, please help".

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> when the first for loop runs, t = 0, so it increments once, then it runs the second for loop
    This is where your understanding is wrong. The loops both start at 0. You run one iteration of the loop before you increment the variable, so the first time through, t is 0 and i is 0. That makes 4*0 + 0 + 1, which of course is 1.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > cout << nums[t][i] << ' ';
    Just change it to
    cout << "t=" << t << ", i=" << i << ", nums=" << nums[t][i] << ' ';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 08:11 AM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 03:44 AM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-02-2006, 11:46 PM
  4. string methods hard time
    By Micko in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2006, 10:00 AM
  5. Filling a vector from file... having a hard time...
    By criticalerror in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2003, 06:19 PM

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