Bienvenue invité ( Connexion | Inscription )

> [Aide] Programme C++ [Aide]
Darknight670
posté 20 Nov 2007, 18:24
Message #1





Groupe : Inscrit
Messages : 76
Inscrit : 30-Sep-07
Lieu : Meudon, (Paris)
Membre n° 1205



Bonjour,

je débute vraiment en programmation C++ donc je crée des petits programmes pour m'entraîner. Or j'essaye de calculer Pi a l'aide la méthode Monte Carlo (pour maîtriser les rand () ) et mon programme ne marche pas ... mais sans erreurs apparentes de compilation...

CODE
##include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iomanip>

using namespace std ;

int main ()
{
cout << "Simulation de Pi\n" ;
cout << "Entrez un nombre etier de simulations a effectuer.\n" ;

int N ;
cin >> N ;

double X ;
double Y ;
double R ;
int M ;
int S ;

srand ( time (0) ) ;


while ( S < N )
{
X = 2 * rand () - 1 ;
Y = 2 * rand () - 1 ;

R = sqrt ( X*X + Y*Y ) ;

if ( R <= 1 )
{
M++ ;
S++ ;
}
else
{
S++ ;
}

}
cout << "Approximation de Pi: \n" << 4 * ( M/N ) << setprecision (5) ;
return 0;
}



EDIT 1 : Dans le debugger ça donne ca :

CODE
[Session started at 2007-11-20 18:25:20 +0100.]
Simulation de Pi
Entrez un nombre etier de simulations a effectuer.
10
Approximation de Pi:
-6.4226e+08
The Debugger has exited with status 0.


Qu'est ce qui ne va pas?
Et que me conseillez vous pour apprendre a maîtriser le C++? ( Livres , sites ET idées de projets etc... )

Ce message a été modifié par Darknight670 - 20 Nov 2007, 18:44.
Go to the top of the page
 
+Quote Post
 
Start new topic
Réponse(s)
Darknight670
posté 25 Nov 2007, 11:16
Message #2





Groupe : Inscrit
Messages : 76
Inscrit : 30-Sep-07
Lieu : Meudon, (Paris)
Membre n° 1205



Merci a tous j'ai réussi a le faire marcher ! Je ne sais pas pourquoi ça ne marchais pas donc je ne sais pas pourquoi il marche maintenant ... ( erreur de chiffres significatifs je crois...)
CODE
/* Importation de bibliothèques */
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>

using namespace std ;

int iChoix = 0 ;
/* -------------------------------------------------------------------------------------------------------------------- */
int iAlea ( void ) // Cette fonction genere un nb aleatoire....
{

static int first = 0;

if (first == 0)
{

srand (time ( NULL ) );
first = 1;

}
return ( rand () );
}
/* -------------------------------------------------------------------------------------------------------------------- */
double dExact( long int iPrecision )
{
/* Définition des variables */
double dPi = 0 ;
bool bAddition = true ;
long int liDiviseur = 3 ;
long int liCompteur = 0 ;


dPi = 1 - 1/ (double) liDiviseur;

cout << "Calcul de Pi en cours...\n"; /* Affichage d'un message en attendant que */
cout << "Svp patienter...\n\n" ; /* Pi soit calculé. */

/* Calcul de Pi */
do

{
liDiviseur = liDiviseur + 2;
if (bAddition == true)
{
dPi = dPi + 1/ (double) liDiviseur;
bAddition = false;
}

else

{
dPi = dPi - 1/ (double) liDiviseur;
bAddition = true;
}

liCompteur++;
}
while ( liCompteur <= iPrecision);

/* Affichage du résultat */
dPi = dPi * 4;
return dPi ;
}

/* -------------------------------------------------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------------------------------------------------- */

float fSimulationPi ( int iIteration )
{
float fCoordonneeX ;
float fCoordonneeY ;
float fLongueur ;
int iSimulation = 0 ;
int iPointCercle = 0 ;
int iPointTotal = 0 ;

while ( iSimulation <= iIteration )
{

iAlea () ;
fCoordonneeX = 2 * rand () / ( RAND_MAX + 1.0 ) ;
iAlea () ;
fCoordonneeY = 2 * rand () / ( RAND_MAX + 1.0 ) ;

fLongueur = sqrt (fCoordonneeX*fCoordonneeX + fCoordonneeY*fCoordonneeY) ;

if ( fLongueur < 1 )

{
iPointCercle++ ;
}

iSimulation++ ;
iPointTotal++ ;
}
return ( 4.00 * iPointCercle / iPointTotal ) ;
}

/* -------------------------------------------------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------------------------------------------------- */

int main ()
{

if ( iChoix == 0 )
{
cout << " ****************************\n" ;
cout << " * *\n" ;
cout << " * Pi Calculator *\n" ;
cout << " * Cree par Darknight670 *\n" ;
cout << " * Le 25/11/07 *\n" ;
cout << " * *\n" ;
cout << " ****************************\n" ;
}

cout << endl ;
cout << "Comment voulez vous calculez Pi?\n" ;
cout << "1.Par la methode Monte Carlo\n";
cout << "2.Grace au Theoreme de Leibniz\n" ;
cout << "0.Quitter\n" ;

cin >> iChoix ;

if ( iChoix == 1 )
{
cout << endl ;
cout << "Entrez un nombre de simulations a effectuer\n" ;

int iIteration ;
cin >> iIteration ;

cout << "Pi vaut : " << fSimulationPi ( iIteration ) ;
cout << endl ;

main () ;
}

if ( iChoix == 2 )
{

cout << endl ;
cout << "Entrez une precision de calcul\n" ;

int iPrecision ;
cin >> iPrecision ;

cout << "Pi vaut : " << dExact ( iPrecision ) ;
cout << endl;

main () ;

}

if ( iChoix == 0 )
{

cout << " Merci et au revoir!\n" ;
return 0 ;

}

return 1 ;
}

/* -------------------------------------------------------------------------------------------------------------------- */



Merci encore a tous
Go to the top of the page
 
+Quote Post

Les messages de ce sujet


Reply to this topicStart new topic
1 utilisateur(s) sur ce sujet (1 invité(s) et 0 utilisateur(s) anonyme(s))
0 membre(s) :

 



RSS Version bas débit Nous sommes le : 6 Jul 2025 - 15:27