GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Feb-2008, 15:56
jrod738776 jrod738776 is offline
New Member
 
Join Date: Feb 2008
Posts: 1
jrod738776 is on a distinguished road

Help with program using a loop


Ok well I'm trying to make a program that uses the pi formula and outputs 3 different answers. The formula is p = 4*(1 - (1/2i-1) + (1/2i+1). The 3 answers I need to output are when i = 100, 1000, and 100000. I need to start with i = 2 and each time through the loop another 2 gets added to i and will only output when it is at the 3 numbers; 100, 1000, and 100000. This is what I currently have and it does not work, I think I am way off with my whole code so any help would be great.

// Author :
// Date :
// Assignment :
// Compiler :
// Command line :
// Collaborators :

#include <iostream>
using namespace std;

int main(){

// Variable declarations
double pione;
double pitwo;
double pithree;
int i = 2;



// Figure out cost of tuition for child
do{
if (i == 100);
if (i == 1000);
if (i == 100000);
i += 2;

}

while (i <= 100000){
pione = 4 *(1-(1/(2i-1) + (1/(2i + 1);
pitwo = 4 *(1-1/(2i-1) + (1/(2i + 1);
pithree = 4 *(1 - (1/(2i-1) + (1/(2i+1);

}

//tell user how much pi equals when i is 100, 1000, and 100000
cout << " When i=100 pi = " << pione << endl;
cout << " When i=1000 pi = " << pitwo << endl;
cout << " When i=100000 pi = " << pithree << endl;




return 0;
}
  #2  
Old 19-Feb-2008, 16:56
davis
 
Posts: n/a

Re: Help with program using a loop


Your code is "messed up" and you definitely need to read the guidelines.

CPP / C++ / C Code:
// Author : 
// Date : 
// Assignment : 
// Compiler : 
// Command line : 
// Collaborators : 

#include <iostream>
using namespace std;

int main()
{
    // Variable declarations
    double pione;
    double pitwo;
    double pithree;
    int i = 2;
    
    // Figure out cost of tuition for child
    do
    {
        switch( i )
        {
        case 100:
            pione = 4 *(1-(1/((2*i)-1) + (1/((2*i) + 1))));
            break;
        case 1000:
            pitwo = 4 *(1-(1/((2*i)-1) + (1/((2*i) + 1))));
            break;
        case 100000:
            pithree = 4 *(1-(1/((2*i)-1) + (1/((2*i) + 1))));
            break;
        default:
            break;
        }
        i += 2;
    } while( i <= 100000 );
    
    //tell user how much pi equals when i is 100, 1000, and 100000
    cout << " When i=100 pi = " << pione << endl;
    cout << " When i=1000 pi = " << pitwo << endl;
    cout << " When i=100000 pi = " << pithree << endl;
    
    return 0;
}



...will at least compile, but I think that you need to seriously look at your calculations, as I doubt that it is what you want from the calculations.


:davis:
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Ineffective "if then" loop on my first program ftmthy412 C++ Forum 4 12-Sep-2007 09:17
malloc/free usage, and general good programming behavior lavaka C Programming Language 7 14-Jun-2007 19:50
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 12:20
Total newbie to programming C! Need help... majortom C Programming Language 2 17-Nov-2005 09:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:11.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.