Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 06-27-2009, 05:57 PM   #1 (permalink)
 
*^[#@"'.,;:\|/:;,.'"@#]^*

Join Date: Dec 2006

Location: Leicester, England

Posts: 3,654

Oreo will become famous soon enoughOreo will become famous soon enough

Send a message via MSN to Oreo
Default C++ Help

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][i]=(t*4)+i+1;
cout << nums[t][i] << ' ';
}
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 [i] = 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 [i] are set to 0, so the result is 1. Next loop, surely [t] would be 1 and [i] would be 1, so the result is 6, then next loop [t] = 2 and [i] = 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 But it's confusing me
Oreo is offline  
Old 06-28-2009, 05:42 AM   #2 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,465

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C++ Help

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 .
__________________


Last edited by Baez; 06-28-2009 at 05:45 AM.
Baez is offline  
Old 06-28-2009, 11:15 AM   #3 (permalink)
 
*^[#@"'.,;:\|/:;,.'"@#]^*

Join Date: Dec 2006

Location: Leicester, England

Posts: 3,654

Oreo will become famous soon enoughOreo will become famous soon enough

Send a message via MSN to Oreo
Default Re: C++ Help

Ohhhhhhhhhhhhhhhhh

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 [i] 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 :o

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:

Last edited by Oreo; 06-28-2009 at 11:21 AM.
Oreo is offline  
Old 06-28-2009, 12:08 PM   #4 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,157

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: C++ Help

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.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is online now  
Old 06-28-2009, 01:27 PM   #5 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,465

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C++ Help

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.
__________________


Last edited by Baez; 06-28-2009 at 01:29 PM.
Baez is offline  
Old 06-28-2009, 04:52 PM   #6 (permalink)
 
*^[#@"'.,;:\|/:;,.'"@#]^*

Join Date: Dec 2006

Location: Leicester, England

Posts: 3,654

Oreo will become famous soon enoughOreo will become famous soon enough

Send a message via MSN to Oreo
Default Re: C++ Help

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 ?

Last edited by Oreo; 06-28-2009 at 04:54 PM.
Oreo is offline  
Old 06-28-2009, 05:06 PM   #7 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,157

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: C++ Help

Never ever use goto outside assembly. It's difficult to say whats happening without any code but it could be that you have introduced an infinite loop to your code.
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page
kmote is online now  
Old 06-28-2009, 05:06 PM   #8 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,465

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C++ Help

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
__________________

Baez is offline  
Old 06-28-2009, 05:39 PM   #9 (permalink)
oldskool's Avatar
 
Electrical Systems Design

Join Date: Jun 2008

Location: Maine, USA

Posts: 1,646

oldskool has a spectacular aura aboutoldskool has a spectacular aura about

Default Re: C++ Help

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.
oldskool is offline  
Old 06-28-2009, 05:54 PM   #10 (permalink)
 
*^[#@"'.,;:\|/:;,.'"@#]^*

Join Date: Dec 2006

Location: Leicester, England

Posts: 3,654

Oreo will become famous soon enoughOreo will become famous soon enough

Send a message via MSN to Oreo
Default Re: C++ Help

Quote:
Originally Posted by Baez View Post
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 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

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..

Last edited by Oreo; 06-28-2009 at 05:57 PM.
Oreo is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On