I have to print out the numbers -2.3 to 2.9 in increments of 0.4, and then add the poss and neg number
EX:
-2.3 -1.9 -1.5 -1.1 -0.7 -0.3 0.1 0.5 0.9 1.3 1.7 2.1 2.5 2.9
Sum of negative values: -7.8
Sum of positive values: 12

i have gotten a little father than this but my code is all over the board.

``````#include <iostream.h>

int main() {

for (float counter=-2.3; counter <= 3.0; counter+=.4)
cout << counter << " ";

return 0;
}``````

## All 5 Replies

add a float counter and then add the value of counter to it within the loop that begins on line 7 of the code you posted.

>> but my code is all over the board.
Looks ok to me, just add the code I mentioned above and you have the answer.

You are very close. You just need to add a couple of things to your loop.

``````#include <iostream>
using namespace std;
int main()
{
// The sum of no numbers is zero, so...
float sum_of_positive = 0.0;
float sum_of_negative = 0.0;

for (float counter=-2.3; counter <= 3.0; counter+=.4)
{
cout << counter << " ";
// [B]todo: if counter is less than zero, add it to the sum_of_negative[/B]
// [B]todo: same kind of thing for positive numbers[/B]
}

// [B]todo: print out your sums here[/B]

return EXIT_SUCCESS;
}``````

Hope this helps.

[EDIT]
man, too slow again...

fooey.

[EDIT]
man, too slow again...

fooey.

Happes to all of us all the time :D

If youre still lost heres some code you can use:

``````#include <iostream>
using namespace std;
int main()
{

float sum_of_positive = 0.0;
float sum_of_negative = 0.0;
float counter;

for (counter=-2.3; counter <= 3.0; counter+=.4)
{
cout << counter << " ";
if (counter < 0)
sum_of_negative +=counter;
else
sum_of_positive +=counter;

}

cout << "Sum of negative values: " << sum_of_negative;
cout << "Sum of positive values: " << sum_of_positive;

return 0;
}``````
commented: And what does he learn when you do the work for him? -2
commented: How to cheat! It's an important part of life. +13

Nobody learns if you just give them answers. My post was as close to it as I dare go.

The trick to programming hasn't anything to do with syntax or language or loops or anything. It is thinking about the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.