Help with C++ problems..

in Genius Bar edited January 2014
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--)





#3 FIND pos1, pos2, temp, gcd



while (pos1 != pos2)


if (pos2 > pos1)






pos1 -= pos2



#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




if (!(seq_no % 2))


seq_no /=2;




seq_no=seq_no * 3 +1;


}while (seq_no != 1);

[ 11-14-2002: Message edited by: TigerWoods99 ]</p>


  • Reply 1 of 7
    First off,when you are coding use long names,the code is written to be read by humans,when the code is compiled all the names are turned into numbers anyway,using short names doesn't save any space.For example use number instead of num,greatestCommonDivisor (or is that dividend you mean? See the confusion that short names cause) instead of gcd.Another suggestion toss C++ and use Objective C instead.Remeber in OO programming you are creating code sections that model real world objects.Think in natural language instead of computerese,keep sight of what you are doing rather than how you are doing it.
  • Reply 2 of 7
    This is for C++, not anything else, and I don't need to change anything. It's what was given and I need to solve those problems.

  • Reply 3 of 7
    Look,you need to do your own homework,but you also need to get out of that class-that is some pretty crappy code your teacher has written.Most of #3 is totally unnecessary,it could be written:

    integer = 72

    integer -= 84

    greatestCommonDivisor = integer

    or more abstractly

    number = 72

    number -= 84

    What the hell does pos stand for?
  • Reply 4 of 7
    I'm honestly not trying...I just need to know how I go about figuring out the values for these....I've done like 15 others but I dont know how to figure these out.

    I dunno what pos is, I think it means position 1 position 2. ANd you may be right on the crappy code lol.
  • Reply 5 of 7
    i think the best way to approach these is to go thru them line by line. try to make as few assumptions as possible. remember that compters know nothing except what we explicitly tell them.

    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 &lt;= 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].


    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 &gt;=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.
  • Reply 6 of 7
    ok so for instance if I have this problem:

    FIND pos1, pos2, gcd

    for (pos1=72, pos2=3360; pos1 && pos2


    if (pos1&gt;pos2)


    pos1 %=pos2;



    gcd=(pos1&gt;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?
  • Reply 7 of 7
    Correct the answer would be 72.Again as I said before,this code really sucks.As you guessed pos1 is not greater than pos2,the immediately below it in brackets is skipped over(and I would like to add this code is horrifying,taking the result of a number modulus a greater number is a complete waste of time,the result will always be the original number.At the bottom " (pos1 &gt; 0) ? pos1 : pos2; " you have a conditional statement-if (pos1 &gt; 0) is true,then gcd is set to pos1,if not pos2.In this case it is obviously true,so gcd is set to pos1. What you do with these code snippets is insert some print functions after each statement and print out the values of each variable at each section so you can watch how the statements are changing the values of the variables.It's called profiling code and often it is the only way to really understand how a section of code works and resolve bugs.

    There is a good C++ reference here:

    <a href=""; target="_blank"></a>;

    [ 11-15-2002: Message edited by: Rick1138 ]</p>
Sign In or Register to comment.