Urgent C++ Help needed!

I need some help with my program due on Monday!! These are the requirements for the assignment:

The assignment is to write a computer program that will add 1/3 to itself a large number of times and to compare the result to multiplying 1/3 by the number of times 1/3 was added to itself. It is also to do the same thing with ½.
The program is to do this arithmetic twice, once using single precision (float) and once using double precision (double). Both of these will be in one program. Make certain you use a type for your counter that works with these large numbers.


Your program will do these additions 109 (1 billion) times. It is to print out the results, every power of ten times (10, 100, 1000, etc. up to 1 billion) along with the result of multiplying 1/3 to itself up to that point. Do NOT use fixed point to output the results. Each output should give the power of 10, the result from the addition of 1/3 to itself up to that point, the multiplication of 1/3 times the number of additions, the difference between these last two numbers, the result of adding ½ to itself up to that point, the multiplication of ½ times the number of additions up to that point, and the difference between these last two numbers. The program must output a header before this data explaining what the output is. Remember to do this for both single and double precision.


Make certain to have comments at the beginning of your program with your name, assignment number 3, date, and a brief explanation of the program. Also make certain to put comments within your program. Make certain to properly block the code in your program. Pay attention to the quality of your output, it should not be slopped all over the screen, but be in neat columns (perhaps use \t in the output statements). Remember you are only to do the multiplications when you output, do NOT do them every pass in the loop.


Items to think about:
1. Did you notice any difference between the results for ½ and 1/3?
2. Did you notice any difference between the results for single and double precision?

This is what I have so far:

// Nathan Annan
// Assignment #3 for Introduction to Programming at Century College
// October 6, 2012
// This program will add 1/3 to itself a large number of times and will compare the result to multiplying 1/3 by the number of times 1/3 was added to itself.
// It will also do the same for 1/2.

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

{
//definite variables
const float one_third = .33;
const float one_half = .5;
double DOUBLEHALF_SUM;
float FLOATHALF_SUM;
double DOUBLETHIRD_SUM;
float FLOATTHIRD_SUM;
int counter;

for(counter = 0; counter < 10E8; counter++) {
while(counter == 10E0 || counter == 10E1 || counter == 10E2 || counter == 10E3 || counter == 10E4 || counter == 10E5 || counter == 10E6 || counter == 10E7 || counter == 10E8) {
cout << " 1/2 added to itself " << counter << " times = " << counter * one_half << endl;
cout << " 1/2 multiplied by itself " << counter << " times = " << pow(one_half, counter) << fixed << endl;
cout << " 1/3 added to itself " << counter << " times = " << counter * one_third << endl;
cout << " 1/3 multiplied by itself " << counter << " times = " << pow(one_third, counter) << fixed << endl;
counter++;
}
}
system ("pause");
return 0;
}


Last edited on
Assignment wrote:
Your program will do these additions 109 (1 billion) times.
Could you explain how 109 == 1000000000, please?
What do you mean can I explain how..?
I think the OP meant 1e9 which is 1 billion.

@anna0004 please use code tags - the <> button on the right.

With this part:
const float one_third = .33;

could introduce some error from the start. try this:

const float one_third = 1.0 / 3.0;
Last edited on
Thanks for the help TheIdeasMan.! I haven't done any assignments using these requirements so I'm still a little confused but thanks!
Last edited on
Consider using a variable for the exponent in your loop condition.


I'll see what I can do. Thanks.
Topic archived. No new replies allowed.