Help with C++ problems..
Ok, I've already done a lot of the others but I'm not sure about these ones:
int index, odd
int square = 0
int num = 13
int pos1, pos2, temp
int gcd
int seq_no, i
char ch1, ch2
#1- FIND odd, square, index, num
for (index = 1; index <= num; ++index)
{
odd = index * 2 - 1;
square = square + odd;
}
#2 FIND i, ch1, ch2
for (i=90; i>=65; i--)
{
ch1=char(i);
ch2=char(i=32)
}
#3 FIND pos1, pos2, temp, gcd
pos1=72
pos2=84
while (pos1 != pos2)
{
if (pos2 > pos1)
{
temp=pos1
pos1=pos2
pos2=temp
}
pos1 -= pos2
}
gcd=pos1
#4 FIND pos1, pos2, gcd
for (pos1=72, pos2=3360; pos1 && pos2
{
if (pos1>pos2)
{
pos1 %=pos2;
}
}
gcd=(pos1>0) ? pos1 : pos2;
#5- FIND seq_no
seq_no=9;
do
{
if (!(seq_no % 2))
{
seq_no /=2;
}
else
{
seq_no=seq_no * 3 +1;
}
}while (seq_no != 1);
[ 11-14-2002: Message edited by: TigerWoods99 ]</p>
int index, odd
int square = 0
int num = 13
int pos1, pos2, temp
int gcd
int seq_no, i
char ch1, ch2
#1- FIND odd, square, index, num
for (index = 1; index <= num; ++index)
{
odd = index * 2 - 1;
square = square + odd;
}
#2 FIND i, ch1, ch2
for (i=90; i>=65; i--)
{
ch1=char(i);
ch2=char(i=32)
}
#3 FIND pos1, pos2, temp, gcd
pos1=72
pos2=84
while (pos1 != pos2)
{
if (pos2 > pos1)
{
temp=pos1
pos1=pos2
pos2=temp
}
pos1 -= pos2
}
gcd=pos1
#4 FIND pos1, pos2, gcd
for (pos1=72, pos2=3360; pos1 && pos2
{
if (pos1>pos2)
{
pos1 %=pos2;
}
}
gcd=(pos1>0) ? pos1 : pos2;
#5- FIND seq_no
seq_no=9;
do
{
if (!(seq_no % 2))
{
seq_no /=2;
}
else
{
seq_no=seq_no * 3 +1;
}
}while (seq_no != 1);
[ 11-14-2002: Message edited by: TigerWoods99 ]</p>
Comments
PLEASE HELP!!!
integer = 72
integer -= 84
greatestCommonDivisor = integer
or more abstractly
number = 72
number -= 84
What the hell does pos stand for?
I dunno what pos is, I think it means position...like position 1 position 2. ANd you may be right on the crappy code lol.
so, for no. 1:
index starts at one, and goes up, one integer at a time, to num (which, previously is set to 13, and for this problem is as good as a constatnt 13, since num doens't chnage in here). even better would be to actually write out the values at the for-loop iterations, until u understand what is happening. so, as long as (index <= num) the for-loop still gets iterated. so:
1st iteration: index=1. odd=(1*2)-1 [=1]. square=0+1 [=1].
2nd iteration: index=2. odd=(2*2)-1 [=3]. square=1+3 [=4].
3rd iteration: index=3. odd=(3*2)-1 [=5]. square=4+5 [=9].
(etc.)
i'm not gonna do it all for u, but i think u can get the idea. ther might be a shortcut, but i dont think it'll be easier to get ur head around. the computer doesn't do any shortcuts.
now, no. 2 is a bit tricky, and not well written. 1st iteration (beginning): i=90. ch1='Z' (90th char). ch2=' ' (spcae, the 32nd char). also, i gets changed to 32. its bad style, especially in examples for beginners, to have a line do two or more things at once. the epresion a=b, can be used as a value itself, and is equal to b, as in: c= (a=b), but its gory and disgusting.
2nd iteration: doesn't actually go into the for-loop, because i=31 && 31 is NOT >=65. i==31 because at the end of 1st iteration i==32, and then i is dec'd (i--) because at the end of the for-loop it gets dec'd.
so, for no. 2 the answer is: i=32, ch1='Z', ch2=' '.
i dont hve time for the rest, cuz i gotta sleep for my finals, but i suggest u try going thru them line by line. if u aren't sure about somehting, try to figure out how the language (c/c++) dictates the action.
FIND pos1, pos2, gcd
for (pos1=72, pos2=3360; pos1 && pos2
{
if (pos1>pos2)
{
pos1 %=pos2;
}
}
gcd=(pos1>0) ? pos1 : pos2;
since for the first thing pos1 isnt greater than pos2 you say that statement cant be true and do the next one which would be 3360 % 72 or 48...then for gcd just put in 72 as pos1?
There is a good C++ reference here:
<a href="http://www.cplusplus.com/doc/tutorial/" target="_blank">http://www.cplusplus.com/doc/tutorial/</a>
[ 11-15-2002: Message edited by: Rick1138 ]</p>