Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2005, 08:14 PM   PM User | #1
Lupercal
New to the CF scene

 
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Lupercal is an unknown quantity at this point
For Loops & Homework

I'm a first year university student who is taking a c++ programming class. In all honesty, I have no idea what I am doing, but I'm not here to ask you to tell me what to do.

We had the ever popular palindrome assignment given to us the other day, and I find myself stuck with truely understanding for loops, and how to use if statements without 'else'. I've been flagging in my program to see where I've gone wrong, however, I seem to think it's in my for loop that all hell has broken loose.

Here is the part where I seem to be stuck, and was wondering if anyone could explain to me (in general terms) the meaning behind the for loop. From my knowledge, my 'i' represents the first char in the string, while 'n' represents the last? Is this correct..?

Quote:
for (int i = 0; i <n; i++)
{
if (s[i] != s[n-1]) //&& (s[i+1] != s[n-1-i]))
{
pal = false;

}

if ((s[i] == s[n-1]) && (s[i-1] == s[n-1-i]))
{
pal = true;
}
}

return pal;
Lupercal is offline   Reply With Quote
Old 11-23-2005, 08:20 PM   PM User | #2
Lupercal
New to the CF scene

 
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Lupercal is an unknown quantity at this point
Ah! I found my problem. The program works, but I was still wondering if anyone could possibly send me a good tutorial to explanation of for loops, or explain them better?
Lupercal is offline   Reply With Quote
Old 11-23-2005, 08:42 PM   PM User | #3
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
I'm assuming n is being set to the length of your string, and as you see you're initializing i to 0. So you're checking the first character with the last character, and the second character to the second to last character and so forth, until you get to the end of the string.

I would say post any specific question you have about loops, in general it's a pretty broad topic. I did do a google on c++ loops as you might find something that will help you along.
http://www.google.com/search?hl=en&q...=Google+Search

Good luck;
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 11-23-2005, 10:24 PM   PM User | #4
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Well in general a loop is a way of executing lines of code contained within repeatedly. A for loop is a count controlled loop. That means it executes a specific number of times. The for loop takes 3 parameters. The starting value, the ending value and the amount to increment.

Code:
int i,

for (i = 1; i <= 5; i++) {
     printf("i = %i\n",i);
}
So what we have inside the loop statement is i which is set to 1 initially. After the first iteration of the loop it doesn't look at that first parameter again. After that it continues to look at the second two parameters. So with each iteration of the loop it compares i to see if it is less than or equal to 5. If that is true it continues to loop. The i++ is the amount we increment. With each iteration of the loop we add 1 to i. Remember that i++ is just a shortcut way of writing i = i +1; You could have also put i+=2 which increments by 2 each time. It is a shortcut way of writing i = i+2;

So back to the example with each iteration of this loop i gets incremented by 1. And with each iteration of the loop i is compared to see if it is less than or equal to 5. Once i increments to 6 the loop will stop because i is no longer less than or equal to 5. Thus the loop will execute whatever is inside of it 5 times since we start at 1 and stop at 5.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!

Last edited by Spookster; 11-23-2005 at 10:28 PM..
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 09:23 AM.


Advertisement
Log in to turn off these ads.