Sunday, 15 March 2015

c++ - Variable Scope and passing Global variables -



c++ - Variable Scope and passing Global variables -

i have write programme calculate value of e given tolerance. must have separate file function calculate e , must utilize global variables. can't pass function. getting same answers output every time, don't think using global variables or calling function right? sorry in advance possible improper formatting code on here.

#include<iomanip> #include<iostream> #include<cmath> #include<string> using namespace std; void find_e(); double tol = 0; double e; double euler = 2.718281828459045; double diff; string ans = "yes"; int main() { while(ans == "yes") { cout<<"this programme estimate value of euler's number desired tolerance"<<endl<<endl; cout<<"please input tolerance: "; cin>>tol; system("cls"); if(tol > 0.0) { find_e(); cout<<"the new value of e "<<fixed<<setw(12)<<setprecision(10)<<e<<endl; cout<<"the actual value of e "<<fixed<<setw(12)<<setprecision(10)<<euler<<endl; cout<<"the difference "<<fixed<<setw(12)<<setprecision(10)<<diff<<endl; } else { cout<<"you can't have negative tolerance"<<endl<<endl; } cout<<"would run again? yes yes, other key no\n\n"; cin>>ans; } system("pause"); homecoming 0; }

and here function in separate file:

#include<iomanip> #include<iostream> #include<cmath> #include<string> using namespace std; extern double e; double newe; extern double diff; int n = 1; extern double tol; void find_e() { { e = (n,(1+(1/n))); diff = abs(newe - e); newe = e; n++; } while(diff>tol); return; }

c++ global-variables scope

No comments:

Post a Comment