Taking C++, need help with a problem pls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billyusa
    New Member
    • Feb 2008
    • 5

    Taking C++, need help with a problem pls

    Hello all, im taking a intro to c++ class as part of my design comm, and having issues withthe codelab part (online). First off it doesnt give any help to what you are doing wrong.

    the prob is:

    Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n counting numbers, and store this value in total . Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total . Use no variables other than n , k , and total . Do NOT modify n .

    ive attempted this code:


    n=0;
    k=1;

    while( n=0; n >=1; n<=k)
    {
    total = total + k;
    k++;
    }


    not sure what im doing wrong,.

    pls help thanks
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    You need to write the algorithm in plain English along the following lines before writing the code.
    Assume n has been initialized to some value and k and total have been declared.
    initialize k=0 and total=0
    The while parameter could be (k<n) // because u don`t know the value of n
    if n=4 k would loop through 0,1,2,3
    The loop body could be:{ total+=(k+1)*(k +1)*(k+1)
    k++}
    return total //after loop terminated
    if n=3, total would = 1*1*1 + 2*2*2 +3*3*3=36 maybe!
    I hope this does not contravene the guidelines

    Comment

    • billyusa
      New Member
      • Feb 2008
      • 5

      #3
      ok i think im getting it,

      k=0
      total=0

      while (n=4; k < n; k++)
      {total=total + (k+1)*(k+1)*(k+ 1)
      }

      Comment

      • billyusa
        New Member
        • Feb 2008
        • 5

        #4
        Originally posted by billyusa
        ok i think im getting it,

        k=0
        total=0

        while (n=4; k < n; k++)
        {total=total + (k+1)*(k+1)*(k+ 1)
        }

        but it doesnt seem to be accepting it, I really dislike codelabs, ratherr then helping you it just sits there, saying ERROR!.

        your help is appreciated thanks again

        Comment

        • Simonius
          New Member
          • Feb 2008
          • 47

          #5
          Your while looks a bit weird, what you put between the () belongs after a for.
          The following code should do the trick.
          But it isn't exactly like I want to, are you sure they didn't include math so you could use the pow function?

          code removed, please offer algorithm as opposed to the answer

          Note total+=k*k*k is the same as total = total + k*k*k

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            How can you initialize n with 4 if you don`t know the value of n? You want your loop to continue until it is 1 less than n assuming your counter starts at 0.

            Comment

            • Simonius
              New Member
              • Feb 2008
              • 47

              #7
              You don't need to initialise n, that's already done.

              @ts: what you put in your while belongs in a for, you should just loop it while k is less then n because that's basicly what you want.

              Comment

              Working...