C++ Help

Status
Not open for further replies.

Oreo

-Deactivated-
Messages
5,723
Location
England
Don't laugh, only been doing C++ for a few days now, just started arrrays on my book and got a little confused as to how this code works out how it does:

#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]=(t*4)+i+1;
cout << nums[t] << ' ';
}
cout << '\n';
}


system("pause");

return 0;

}



The outcome is:

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

Whilst i understand the outcomes themselves, e,g when [t] = 0, and = 0, the result is 1.
In my mind i don't understand how all the possibilities are displayed in the order they are. Because the first time the loop is ran, [t] and are set to 0, so the result is 1. Next loop, surely [t] would be 1 and would be 1, so the result is 6, then next loop [t] = 2 and = 2, the result is 11.

So surely the outcome would be displayed as:
1, 6, 11 ? not 1, 2, 3.

Sorry if you don't understand what i'm going on about :p But it's confusing me :confused:
 
That's a good question. It's because you're looking at the nested for loops wrong not the 2D array.

Think of nested for loops like this:

Run for loop #1
Run for loop #2
Is for loop #2 done? Yes then move back to for loop #1. No then execute for loop #2 again.

And it keeps going like that.

So the equations would look like this in order:

nums[0][0] = (0*4) + 0 + 1 = 1
nums[0][1] = (0*4) + 1 + 1 = 2 - because for loop #2 isn't done "t" stays at 0 while "i" increments by 1
nums[0][2] = (0*4) + 2 + 1 = 3 - again, for loop #2 isn't finished
nums[0][3] = (0*4) + 3 + 1 = 4 - same again
nums[1][0] = (1*4) + 0 + 1 = 5 - finally for loop #2 is finished so now loop back to for loop #1 and increment "t" by 1. For loop #2 restarts and therefore "i" is reset to 0.
nums[1][1] = (1*4) + 1 + 1 = 6 - and it starts again etc.

Hope that explains it. If not I'll try again :p.
 
Ohhhhhhhhhhhhhhhhh :D

So, if i understood you right...:

Nested for loops don't all execute each time, only once the inside one is done.
So on this instance, the first time both loops will obviously execute, but the next time the 2nd for loop is not complete until is 4 or greater then 4, once it is, the 1st for loop executes again and the 2nd for loop resets to 0 ?

Makes much more sense now!

Be prepared.. i might be making alot of these threads as i get further into my book :eek:

edit:

quick question. Sometimes the error log tells me i need to close a certain block of code with a }. But sometimes i can't find which part of the code i forgot to close, so can i just bung a } at the end of the code ? or do i need to spend ages searching through my entire code to put it in the right place..:rolleyes:
 
Code:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    for(int i = 1; i < 4; i++)
    {
        for(int j = 1; j < 4; j++)
        {
            printf("outer loop %d inner loop %d\n", i, j);
        }
    }
    return (EXIT_SUCCESS);
}
output:
outer loop 1 inner loop 1
outer loop 1 inner loop 2
outer loop 1 inner loop 3
outer loop 2 inner loop 1
outer loop 2 inner loop 2
outer loop 2 inner loop 3
outer loop 3 inner loop 1
outer loop 3 inner loop 2
outer loop 3 inner loop 3

Just a really simple demo which should clear up how nested loops work. Yes, you need to put your braces in the right place, indenting your code consistantly is a great way to make it really obvious.
 
Glad to help man :). C++ is my main language because of game design/programming so feel free to ask anything.

Here's your code properly indented and formatted:

Code:
#include <iostream>
using namespace std;

int main()
{
	int i;
	int t;
	int 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;
}

Also I wouldn't suggest using iostream and cout until you learn what namespaces are and how they work. Practicing with printf first helps a ton as it's a pretty powerful function.
 
Thanks, Kmote and Baez.

Yeh i did pretty much indent it right but i just straight copy and pasted it so it lost it's formatting.

I'm using "C++ a beginners guide" by Herbert Schildt, and it's not said anything about this printf function. the first thing it taught was cout << and cin >>.

How does printf work ?

Also, not sure how to explain this as i can't remember exactley. But the book was teaching about the goto function, so i tried to use it to goto a part of code just before an if() statement (it may of also been a for() loop) but inside the block of code after the if() statement was a cin >>. However, it didn't let me input anything when i ran the program. It just stopped. No matter what i did using several methods and different code it continued to stop as soon as it reached a single cin >> statement, or just totally ignored it and moved on to the next line of code. I really can't remember what i was doing, all i know is that after using goto it wouldn't allow any console input when running the program.

Any reason why ?
 
The reason being NEVER USE GOTO. I'm not mad but I had to make it stand out. :laughing:

Goto statements are the worst and laziest method of C++ programming and should never be used. Use function calls instead. Can you post the "cin" code you were trying to use so I can debug it?

printf works using a variable parameter list. If you don't know what that is I can explain it. Basically it's a function or object that can accept any number of arguments.

EDIT: kmote got to it first ;)
 
Even though I don't know C++, I can see similarities with other languages I am familiar with regarding the looping concept. I do ok with loops, but just you two, kmote and Baez have made this much clearer. I am glad I read this post, even if I am not the OP. :)
 
The reason being NEVER USE GOTO. I'm not mad but I had to make it stand out. :laughing:

Goto statements are the worst and laziest method of C++ programming and should never be used. Use function calls instead. Can you post the "cin" code you were trying to use so I can debug it?

printf works using a variable parameter list. If you don't know what that is I can explain it. Basically it's a function or object that can accept any number of arguments.

EDIT: kmote got to it first ;)

Heh, don't worry i know not to use them. The guy says in the book they're lazy and rarely work, but you should atleast understand how they work. So just so i understood i tried to put them into the program.

But nevermind, i don't really care why the cin >> statements didn't work 'cus i'll never be using goto anyway ;)

Hehe.. my first console based game is underway :D It's just based on stuff i've learnt so far. An RPG game where you choose where to go, get randomly attacked, stat increases etc :p

edit:
GAH WTF ! :mad:

If i'm correct this code:

num1 = rand() % 11;

will create a random number between 1 and 10. Therefor the following code:

if(num1 > 6) { code code code code etc }

should execute theoretically 3/10 times ? But it doesn't. I've ran the app like 30 times and everytime a random number 6 or under appears to of been generated..
 
Status
Not open for further replies.
Back
Top Bottom