PDA

View Full Version : C++ Help


TaReK
09-14-2005, 01:01 AM
Can Anyone help me with this?

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/999999 + 1/1000000

and

1/1000000 + 1/999999 + 1/999998 + 1/999997 + ... 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).

EXTRA CREDIT #1: Use only one loop in the program (10 points).

EXTRA CREDIT #2: Write an explanation if any differences exist in your answers (20 points).

TheShaner
09-14-2005, 01:10 AM
You should have read this first before posting your homework --> Homework Policy (http://www.codingforums.com/showthread.php?t=53446)

Please make an attempt first and then come back here with any snags you have in your code and someone will assist you, as long as you're showing that you have some kind of concept of the material.

Thanks!

-Shane

TaReK
09-14-2005, 01:39 AM
I tried ...but my result comes out to be 1 all the time ... My techer said it cannot be one...

This is what I tried to do ::

#include <iostream>
using namespace std;

void main ()
{
float sum, first_name,how_many;

cout >> "Please enter how many number to sum (starting from 1/1): " >> endl;
cin << how_many;

sum = 0;
first_num = 1/1;

while (sum <= first_num)
{
sum += first_num;
first_num++;
}

double sum, first_name,how_many;

cout >> "Please enter how many number to sum (starting from 1/1): " >> endl;
cin << how_many;

sum = 0;
first_num = 1/1;

while (sum <= first_num)
{
sum += first_num;
first_num++;
}
}

TheShaner
09-14-2005, 02:03 AM
You don't need to prompt the user for any input, since you know you have to go from 1/1 to 1/1000000 and vice versa.

You should have a loop incrementing from 1 to 100000 and adding up 1/i, where i is the integer being incremented in the loop. Go from there.

-Shane

sage45
09-14-2005, 02:05 AM
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/999999 + 1/1000000

and

1/1000000 + 1/999999 + 1/999998 + 1/999997 + ... 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).

EXTRA CREDIT #1: Use only one loop in the program (10 points).

EXTRA CREDIT #2: Write an explanation if any differences exist in your answers (20 points).

Well first of all, I don't see anywhere in here where the teacher says that the program interacts with the user... In other words, you don't need to get any input from the user...

Now lets look at the problem itself... Notice what is changing... Just the denominator... In the first case from 1 to 1000000, the second is the opposite from 1000000 to 1... So then the question is what should you do?

Note: Dangit beat by Shaner... :p

-sage-

TheShaner
09-14-2005, 02:11 AM
Sorry Sage, i gave away too much and saw your post. I took a lot back there, lol. So hopefully there's still room to figure it out now, hehe.

TaReK
09-14-2005, 02:21 AM
Now lets look at the problem itself... Notice what is changing... Just the denominator... In the first case from 1 to 1000000, the second is the opposite from 1000000 to 1... So then the question is what should you do?


A loop incrementing from 1/1000 and vice varsa...

You should have a loop incrementing from 1 to 100000 and adding up 1/i, where i is the integer being incremented in the loop. Go from there.

How do I declare i .....should it be like i = 1 ?

*Thanks for gelping me out guys and sorry i m too dumb in c++, actually I missed the lecture on the while loop, so read the chapter from book, didnt understand very much...so kinda got lost when I saw fractions...

TheShaner
09-14-2005, 02:25 AM
Yes, declare i as int i = 1; then start whatever loop you'd like to use. Increment it until you reach 1000000. Construct the loop you want. Now what should be the inside this loop?

-Shane

TaReK
09-14-2005, 02:30 AM
void main ()
{
float first_num,sum;

sum = 0;
first_num = 1;

int i = 1;

while (sum >= 1000000)
{
first_num = sum + first_num;
first_num++;
}
}


Is it correct??

TheShaner
09-14-2005, 02:36 AM
void main ()
{
float first_num,sum;

sum = 0;
first_num = 1;

int i = 1;

while (sum >= 1000000)
{
first_num = sum + first_num;
first_num++;
}
}

You're not doing anything with the variable i. Your While loop should actually be i <= 1000000 and then increment i inside your loop so that i goes from 1 to 1000000. Then your loop will stop once i gets bigger than 1000000. Inside the loop, you need to be adding fractions: sum = sum + 1/i; Do you see what's happening then?

sum = 0 + 1/1
sum = 1 + 1/2
sum = 1.5 + 1/3
....

and so on. Sum is declared as a float. You also should do that same operation but storing it in a variable that is declared as a double, since your teach wants that also. Once you get that together, what would you have to do to add from 1/1000000 to 1/1?

-Shane

TaReK
09-14-2005, 02:46 AM
You're not doing anything with the variable i. Your While loop should actually be i <= 1000000 and then increment i inside your loop so that i goes from 1 to 1000000. Then your loop will stop once i gets bigger than 1000000. Inside the loop, you need to be adding fractions: sum = sum + 1/i; Do you see what's happening then?

sum = 0 + 1/1
sum = 1 + 1/2
sum = 1.5 + 1/3
....

and so on. Sum is declared as a float. You also should do that same operation but storing it in a variable that is declared as a double, since your teach wants that also. Once you get that together, what would you have to do to add from 1/1000000 to 1/1?

-Shane

void main ()
{
float first_num,sum;

sum = 0;
first_num = 1;

int i = 1;

while (i <= 1000000)
{
sum = sum + 1/i;
i++;
}

cout << "The sum of the numbers: " << sum << endl;

}

still giving me 1 as output....where did i go wrong?

TheShaner
09-14-2005, 03:29 AM
Hate to leave you hanging here, but i have to get to bed. It's been a long time since I've coded in C++. I ran my own version in my IDE and I got the same thing. I even debugged it and broke it down and the fraction 1 / i is converted into an integer, even though I explicitedly type casted 1/i as a double and float. Hopefully someone more experienced in C++ can tell you and I why 1/i is being converted to an integer even when i do something like double(1/i), pass that to a variable declared as double, and then used in the equation.

-Shane

TaReK
09-14-2005, 03:42 AM
ok shane ...no probs...thnx for help...i hope some 1 else will be on very soon...this project is due tomorrow...

aman
09-14-2005, 04:10 AM
Since both values are integers, the return value will be truncated to an int.

Define "i" as a float
float i;

Or cast it to a float
sum += 1 / (float)i;

Or declare 1 as a float
sum += 1.0f / i;

Then it will work.

TaReK
09-14-2005, 04:24 AM
it worked how would I start the next part??

and

1/1000000 + 1/999999 + 1/999998 + 1/999997 + ... 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).

aman
09-14-2005, 11:29 AM
Just do the loop backwards.

int main()
{
int index = 1000000;

float f = 0.0f;
double d = 0.0;

for( ; index > 0; --index )
{
f += 1.0f / index;
d += 1.0E0 / index;
}

printf("The sum of the numbers: float %f; double %g\n", f, d);

return 0;
}

TheShaner
09-14-2005, 02:25 PM
Aman's code is in C. For C++ code, do:

int main()
{
int index = 1000000;

float f = 0.0;
double d = 0.0;

for( ; index > 0; --index )
{
f += (float) 1.0 / (float) index;
d += (double) 1.0 / (double) index;
}

cout << "The sum of the numbers: float " << f << "; double " << d << endl;

return 0;
}

When I ran my program at home, I type casted my variables incorrectly inside the loop, so that was my problem. Thanks to Aman, now you should be good to go. Also, for extra credit, you have to put it in 1 loop. If you don't want the extra credit, keep the first loop and the loop that's above. However, if you want the extra credit, use an extra variable in your 1st loop we did that starts at 1000000 and decrements to 1 inside the loop. Then you'll have 2 extra variables storing the float and double values of the equations using the variable that is decrementing.

-Shane

TaReK
09-15-2005, 04:49 AM
First part is working fine...still struggling on second...heres what i have done::

#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;

float i = 1;

while (i <= 1000000)
{
sum = sum + 1/i;
i++;
}

cout << "1. The sum of the numbers: " << sum << endl;

sum2 = 0;
second_num = 1;

double x = 1;

while (x <= 1000000)
{
sum2 = sum2 + 1/x;
x++;
}

cout << "2. The sum of the numbers: " << sum2 << endl;


sum3 = 0;
third_num = 1000000;

float y = 1;

while (y <= 1)
{
sum3 = sum3 - 1/y;
x--;
}
cout << "3. The sum of the numbers: " << sum3 << endl;


}

TheShaner
09-15-2005, 03:02 PM
Ok, you're on the right track, but you really need to write down what each variable is doing. first_num, second_num, third_num, and fourth_num are not even doing anything. If you want to use those, then don't use i, x, and y. So do this below:

#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;

}

I added your fourth part too. Now, your third and fourth parts are incorrect, as i stated in comments. Do you see why? Iterate through your loops on paper. Hint: Look at your comparison in your while loop for the third and fourth loops. After you figure this out, consolidate to 2 loops. Then attempt to use only 1 loop!

-Shane

TaReK
09-18-2005, 02:53 AM
Thanks a lot Aman, sage and specially TheShaner.... I understood the program perfectly...