Ask your own question, for FREE!
Computer Science 7 Online
sleepyjess:

loopy loopy loops

sleepyjess:

Assume the int variables i, lo, hi, and result have been declared and that lo and hi have been initialized. Write a for loop that adds the integers between lo and hi (inclusive), and stores the result in result. Your code should not change the values of lo and hi. Also, do not declare any additional variables -- use only i, lo, hi, and result.

sleepyjess:

result = 0; for (i >= lo && i <= hi; i++;) { result += i; }

1 attachment
Ultrilliam:

Lemme open visual studio real qucik

Ultrilliam:

quick*

Ultrilliam:

```cpp int result = 0; ``` You forgot to identify the datatype

sleepyjess:

1 attachment
Ultrilliam:

Ah, didn't realize it already defined it

sleepyjess:

the stuff in black is what they did, but they don't show us that before we attempt the question at all

Ultrilliam:

Ah

Ultrilliam:

K finally got my visual studio setup lol

Ultrilliam:

-still figuring this out-

Ultrilliam:

I would think it would be ```cpp i = lo; for (i >= 5 && i <= 10; i++;) { result += i; } ``` but the loop isn't ending for some reason

Ultrilliam:

so I'm sitting here scratching my head at that

Ultrilliam:

unless I'm mistaking the logic should be fine :thinking_face:

sleepyjess:

sorry, brother needed me

sleepyjess:

maybe something along the lines of this? result = 0; i = lo; for (i <= hi; i++) { result += i; }

sleepyjess:

got it, was on the right train of thought with that last idea

1 attachment
Ultrilliam:

there ya go =P

sleepyjess:

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 do...while loop to compute the sum of the cubes of the first n whole 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.

sleepyjess:

total = 0; do { total += k*k*k; k++; } while(n >= k);

sleepyjess:

1 attachment
Ultrilliam:

```cpp int total = 0; do { total += k*k*k; k++; } while(n >= k); ```

sleepyjess:

total was already declared as an int

sleepyjess:

1 attachment
sleepyjess:

did it just so it would show the full code lol

sleepyjess:

got it

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!