Loops

I'm stuck on how to exactly use loops to make a program function like the text below.

Help would be grateful.

Thanks.





Write a program to calculate and print the result of the following two operations:

1/1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/99999999 + 1/100000000

and

1/100000000 + 1/99999999 + 1/99999998 + 1/99999997 + ... + 1/3 + 1/2 + 1/1

Results should be totaled and printed once using float variables for each calculation and once using double variables for each calculation. There should be a total of four answers printed. Make sure that the answers are adequately labeled. You may use any of the three loop mechanisms (while, do-while, or for).

Looks like homework to me. we wont do it for you!!!

look here

http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Yes, it may be homework, but I've been out for the past week because of surgery and I've been trying to catch up from home.

Being my first time touching C++, I don't have much background to the language.

I asked for help, not for the written code to do the operations at hand.

I've already looked at that section yesterday, it does no good for what is asked for the program.


So any further help or hints would be great.

Still awaiting from an email from my professor.

Thanks.
alright well try this

for(int n = 1; n !> 100000000; n++)
{
total = (total + (1.0 / n));
}

let me know if you get this. i only give you code because if you understand this pat then you can reformat it for the other parts of the assignment.
Last edited on
I get that this line of code must deal with the total of a character (a digit in this case) add to a sum.
Obv. you would have to create either a float or have int figures, right?
Remember that (int / int) does not produce a float.
total would be a float
at the beginning of this for loop you have n = 1 and add it to total which should be zero to start.
1/1=1,so you are adding one to total
next it would be----- total(or 1) = total +1/2, so now total = 1.5
and this keeps going, adding 1/3,1/4,1/5,ect.., till n is bigger than 100,000,000
By doing n !> 100000000 it gives an error you can't do that.

Also, its asking us to initialize total for it to be able to use it.

When I try running the program it gives error and when i ignore them it's just bad news.

FYI Recklein3 and I are in the same class. We are just trying to get this done.

There's no such thing as !>. [Not [greater than]] is [lower than or equal to].
Exactly..

See the code that kyle input above..
i attempted the foundation that kyle was giving me and it doesn't work...

so lets try this again....
This is what I have so far:




#include <iostream>

using namespace std;

void main ()
{
float first_num,sum;
double second_num,sum2;
float third_num,sum3;
double fourth_num,sum4;

sum = 0;
first_num = 1;

while (first_num <= 1000000)
{
sum = sum + 1/first_num;
first_num++;
}
cout << "1. The sum of the numbers: " << sum << endl;

sum2 = 0;
second_num = 1;

while (second_num <= 1000000)
{
sum2 = sum2 + 1/second_num;
second_num++;
}
cout << "2. The sum of the numbers: " << sum2 << endl;

sum3 = 0;
third_num = 1000000;

/* There is an error below... can you see it? */
while (third_num <= 1)
{
sum3 = sum3 - 1/third_num;
third_num--;
}
cout << "3. The sum of the numbers: " << sum3 << endl;

sum4 = 0;
fourth_num = 1000000;

/* There is an error below... can you see it? */
while (fourth_num <= 1)
{
sum4 = sum4 - 1/fourth_num;
fourth_num--;
}
cout << "4. The sum of the numbers: " << sum4 << endl;

}





Here's the problem:


Can't put 100,000,000 without the program halting when I run it (1,000,000 works).
Why is that?


Also wondering why the three last sums are different from the first when I do put 1,000,000.

Final question , how can i take it another step and put into one loop?
Will I need to break and cont ?

Thanks.
Last edited on
Anyone?

Thanks.
Topic archived. No new replies allowed.