C++ Pi Estimation Program Not Working Properly - c++

I am currently writing a program that estimates Pi values using three different formulas pictured here: http://i.imgur.com/LkSdzXm.png .
This is my program so far:
{
double leibniz = 0.0; // pi value calculated from Leibniz
double counter = 0.0; // starting value
double eulerall = 0.0; // value calculated from Euler (all integers)
double eulerodd = 0.0; // value calculated from Euler (odds)
double eulerallans; // pi value calculated from Euler series (all integers)
double euleroddans; // pi value calculated from Euler series (odd integers)
int terms;
bool negatives = false;
cin >> terms;
cout << fixed << setprecision(12); // set digits after decimal to 12
while(terms > counter){
leibniz = 4*(pow(-1, counter)) / (2*counter+1) + leibniz;
eulerall = (1/pow(counter+1,2)) + eulerall;
eulerodd = 32*(pow(-1, counter)) / (pow(2*counter + 1, 3)) + eulerodd;
counter++;
eulerallans = sqrt(eulerall*6);
euleroddans = pow(eulerodd, 1.0/3.0);
cin >> terms;
if (terms < 0){
if(!negatives)
negatives=true;
}
}
cout << right << setw(14) << "# TERMS" << setw(15) << "LEIBINZ" << setw(15) << "\
EULER-ALL" << setw(15) << "EULER-ODD" << endl;
cout << right << setw(14) << terms << " " << leibniz << " " << eulerallans << " "\
<< euleroddans <<endl;
cout << "There were " << negatives << " negative values read" << endl;
return 0;
}
The sample input file that I am using is:
1
6
-5
100
-1000000
0
And the sample output for this input file is:
1 4.000000000000 2.449489742783 3.174802103936
6 2.976046176046 2.991376494748 3.141291949057
100 3.131592903559 3.132076531809 3.141592586052
When I run my program all I get as an output is:
# TERMS LEIBINZ EULER-ALL EULER-ODD
1
4.000000000000
2.449489742783
1.000000000000
So, I have two problems with my program:
1) It is only reading the first value in the input file and stopping.
2) The equation for eulerodd seems to be off, but I can't figure out what the problem is.
EDIT: Thanks to #RaphaelMiedl, I solved problem 2. Now I only have problem 1 to deal with.
Help is greatly appreciated.

I don't have the time to go through all of your code but
euleroddans = pow(32*eulerodd, 1/3);
immediately jumped me when I skimmed over it. 1/3 is integer arithmetic and gives you 0. Probably not what you wanted, you probably want 1.0/3.0 or something of the like there.
Now a bit of a late addition since I had time to look at your code again. #paddy is right that you only got one input statement in your code.
I'd probably put the whole pi calculation and outputting in a function and then loop over the input like:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void my_pi_func(int term);
int main()
{
int term;
cout << fixed << setprecision(12); // set digits after decimal to 12
while (cin >> term){
my_pi_func(term);
}
return 0;
}
void my_pi_func(int term)
{
if (term <= 0){
cout << "\n\nNegative value or 0 read\n\n" << endl;
return;
}
double leibniz = 0.0; // pi value calculated from Leibniz
int counter = 0; // starting value
double eulerall = 0.0; // value calculated from Euler (all integers)
double eulerodd = 0.0; // value calculated from Euler (odds)
double eulerallans = 0.0; // pi value calculated from Euler series (all integers)
double euleroddans = 0.0; // pi value calculated from Euler series (odd integers)
while(term > counter){
leibniz = 4*(pow(-1, counter)) / (2*counter+1) + leibniz;
eulerall = (1/pow(counter+1,2)) + eulerall;
eulerodd = (pow(-1, counter)) / (pow(2*counter + 1, 3)) + eulerodd;
counter++;
eulerallans = sqrt(eulerall*6);
euleroddans = pow(32*eulerodd, 1.0/3.0);
}
cout << right << setw(14) << "# TERMS" << setw(15) << "LEIBINZ" << setw(15)
<< "EULER-ALL" << setw(15) << "EULER-ODD" << endl;
cout << right << setw(14) << term << " " << leibniz << " " << eulerallans
<< " " << euleroddans <<endl;
}
You can see a working version here. Note that I made some more slight adjustments. For one I changed terms into term since that plural s was somehow bothering me. Also I changed counter to be an int since a counter as double variable doesn't make sense in my opinion. Also I changed how negative/0 values output. But all in all you should get what you should change if you compare with yours.

Related

Avoid rounding off Nth digit of Pi

I am trying to solve the first problem of this collection
To find and display the value of Pi till the Nth digit (taken as user input limited by some maximum value, my guess the maximum number of decimal places the biggest ds can hold which is long double i guess?)
Below is a snippet of my attempt.
//declarations
typedef numeric_limits< long double > db;
int MAX = db::digits10;
int N = 0; //digit allowance
long double pi_val = M_PI;
string string_pi;
//user input
while(1){
cout << "Enter a value of n < or = " << MAX << endl;
cin >> N;
if(N>18){cout << "Lesser please" << endl;}
else
break;
}
//outputs
cout << "Pi till the " << N << " digit is: ";
cout << setprecision(N+1) << fixed << pi_val << endl;
string_pi = to_string((long double)pi_val * pow(10, N));
cout << string_pi << endl;
cout << "The Nth digit of pi is: " << string_pi[N] << endl;
Ps I am aware that more novel ways exist to go about the problem but since I am an extreme beginner at cpp, I'd attain a better sense of achievement by making my idea work.
Now the problem, it displays the Nth digit correctly but the output of Pi till the Nth value always gets rounded off despite the 'fixed' and 'setprecision()' so I had to display an extra digit to tally with the latter output
How can I fix this problem, if possible without changing the main logic I have used
Thank you

Uninitialized local variable that is actually initialized? [duplicate]

This question already has answers here:
How can I declare and define multiple variables in one line using C++?
(10 answers)
Comma as separator in variable initialization (not as operator)
(2 answers)
How does the Comma Operator work
(9 answers)
Closed 1 year ago.
I am writing a program that deals with values in an input file. My variables include total, taxtotal, subtotal, etc. & they have already been declared and initialized, yet I am getting two error messages: "uninitialized local variable 'subtotal' used" and the same for the variable "taxtotal".
Here is my source code:
#include "stdafx.h"
#include<stdio.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ifstream shoppingBasketFile;
shoppingBasketFile.open("HW3_Data.txt");
bool isTaxed = false;
char taxValue = 0;
char inPrice[64];
char name[128];
double price, taxtotal, subtotal, total = 0;
if (shoppingBasketFile.is_open())
{
// display the header info:
cout << "o Thank you for shopping at StuffMart" << endl;
cout << setw(3) << left << "o "
<< setw(20) << left << "Item"
<< setw(12) << "Unit Price"
<< setw(4) << "Tax"
<< endl
<< "o -------------------------------------" << endl;
// parse the input file until end of file;
while (!shoppingBasketFile.eof())
{
// parse the item name:
shoppingBasketFile >> name;
cout << "Name = " << name << endl;
if (name == NULL)
{
// what should we really do here?
continue;
}
// parse the price:
shoppingBasketFile >> price;
if (price < 0 || price > 100000000000) {
continue;
}
cout << "Price = " << price << endl;
// parse the isTax flag:
shoppingBasketFile >> isTaxed;
shoppingBasketFile >> taxValue;
cout << "Is taxed? = " << taxValue << endl;
// if end of file break out of this loop:
if (!shoppingBasketFile.good()) break;
if (isTaxed == true) {
taxtotal = taxtotal + (.085 * price);
taxValue = 'Y';
}
else {
taxValue = 'N';
}
//display tax as Y instead of T/1
if (isTaxed == true) {
cout << "Tax: Y" << endl;
}
else {
cout << "Tax: N" << endl;
}
//compute the subtotals
subtotal = subtotal + price;
// display the item info:
cout << "name" << name << ", price: $" << price << ", is taxed: " << taxValue << endl;
// reset input values:
name, price, isTaxed = 0;
// end of while loop
}
//compute the final total:
total = subtotal + taxtotal;
//output the totals
cout << "o" << setw(37) << "---------------" << endl
<< "o " << setw(26) << "Subtotal $" << fixed << setprecision(2) << right << subtotal << endl
<< "o " << setw(26) << "Tax (8.5%) $" << fixed << setprecision(2) << right << taxtotal << endl
<< "o " << setw(26) << "Total $" << fixed << setprecision(2) << right << total << endl;
}
shoppingBasketFile.close();
return 0;
}
How can I eliminate these error messages? I am using Microsoft's Visual C++ compiler, if that matters.
In this declaration:
double price, taxtotal, subtotal, total = 0;
the type name double applies to all 4 variables, but the = 0 initialization applies only to total.
As others have said, the most direct fix is:
double price = 0, taxtotal= 0, subtotal = 0, total = 0;
but it's better style to declare each variable on its own line:
double price = 0.0;
double taxtotal = 0.0;
double subtotal = 0.0;
double total = 0.0;
Note that using 0 is perfectly valid (the int value will be implicitly converted to the double value 0.0), but using a floating-point constant is more explicit.
(I've chosen to align the initializers vertically. Some might prefer not to do that.)
I'm guessing you haven't gotten to pointers yet. When you do, you'll encounter another reason to declare each variable on its own line. This:
int* x, y, z;
defines x as an int*, but y and z as int. Using one declaration per line, as for the initializers above, avoids this opportunity for error and confusion:
int* x;
int* y;
int* z;
Once you get your code to compile, you'll have a problem with this line:
name, price, isTaxed = 0;
That's a valid statement, but it doesn't do what you think it does.
, is the comma operator. It evaluates its left and right operands in order, and yields the value of the right operand, discarding the value of the left operand. The statement evaluates and discards the current value of name, then evaluates and discards the current value of price, then assigns the value 0 to isTaxed. (Thanks to user4581301 for pointing this out.)
You could write this as:
name = price = isTaxed = 0;
(since an assignment yields the value that was assigned) or, more simply, as:
// name = 0;
price = 0.0
isTaxed = false;
I've commented out the assignment to name, since it's an array and you cannot assign a value to an array object. I won't show a corrected version because I don't know what you're trying to do here.
Suggestion: Start small, keep it simple, and confirm at each step that your code works before adding new code. I think you've tried to write too much code at once. You have nearly 100 lines of code that won't even compile. I've been programming for a long time and I wouldn't write that much code without making sure it compiles and runs.
It looks like you declared subtotal in your statement here:
double price, taxtotal, subtotal, total = 0;
but only initialized total with value 0, causing its use on the right side of the assignment to trigger the error:
subtotal = subtotal + price;
To initialize multiple items simply add the "=" explicitly.
Example:
double price = 0, taxtotal = 0, subtotal = 0, total = 0;

C++ calculating the ratio of 2 numbers [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
Hi guys I'm rather new to programming and working my way through Stroustrup's "Programming, Principles and Practice Using C++" and I've come to a complete standstill at the end of Chapter 3 with an exercise asking you to write a piece of code that does a number of calculations involving 2 numbers which includes finding the ratio of the numbers. Unfortunately this hasn't been covered at all in the book and I'm tearing my hair out trying to figure it out by myself, only able to find examples of code way to advanced for my small little brain.
The code I have at the moment is:
double ratio;
if (val2 > val1)
ratio = (val2 / val1);
if (val2 < val1)
ratio = (val1 / val2);
cout << "The ratio of " << val1 << " and " << val2 << " is 1:" << ratio << '\n';
which works fine for numbers that equate to a whole ratio (e.g. 100 and 25) however despite me setting the variable "ratio" as a double it removes any decimals from the answer in cases of non whole number ratios. Can anyone tell me where I'm going wrong?
When dividing integers the result is integer (integer arithmetics is used):
11 / 2 == 5
11 % 2 == 1 /* remainder */
and when dividing floating point values the result is floating point as well:
11.0 / 2 == 5.5
11 / 2.0 == 5.5
((double) 11) / 2 == 5.5
In your case
double ratio = (val2 / val1);
you have an integer division and only after the disvison performed the outcome of it is cast to double. You can either declare val2 and val1 as double:
double val1;
double val2;
or cast at least one argument of the ratio to double:
double ratio = ((double)val2) / val1;
The fact that result type is double doesn't matter if the original division is performed on integral types (truncating the decimal part).
So to solve your problem, either:
Use a floating point type for the input numbers as well
Cast one of the numbers to a floating point type before division
I did the whole problem from Stroustrup's "Programming, Principles and Practice Using C++. Here is the codes although no comments.
int main()
{
/** --------Numbers-----*/
int val1;
int val2;
double largest; //I'll store here the largest value
double smallest; //I'll store here the smallest value
cout<< " Enter two Numbers to play with\n";
while(cin>> val1>>val2){
if(val1<val2){
cout<< "smallest: "<<val1<<endl;
cout<< "largest: "<<val2<<endl;
//If the above argument succeeds, largest and smallest will get their values
largest=val2;
smallest=val1;}
if(val1>val2){
cout<< "smallest: "<<val2<<endl;
cout<< "largest: "<<val1<<endl;
//If the above argument succeeds, largest and smallest will get their values
largest=val1;
smallest=val2;}
int their_sum=val1+val2;
int their_product=val1*val2;
int their_diff=val1-val2;
double ratio1;
ratio1=largest/smallest;
cout<<"Sum: "<<their_sum<<endl;
cout<<"Difference: "<<their_diff<<endl;
cout<<"Product: "<<their_product<<endl;
cout<<"Ratio: "<<ratio1;
}
return 0;
}
There is nothing new in this code, everything was covered in the previous chapters.
If at all you need ratio of two numbers say a,b in the form of n:m (where n>=1) then simply find the GCD(a,b) and divide a,b with this result.
eg:
a=4,b=6;
GCD(a,b)=2;
n=4/2=>2
m=6/2=>3
so ratio of 4 and 6 is 2:3
#include<iostream>
using namespace std;
class Test
{
public:
void check()
{
int x,y;
cout<<"Enter 1st number";
cin>>x;
cout<<"Enter 2nd number";
cin>>y;
int a;
int d= gcd(x,y);
cout<< x/d << " : " << y / d << endl;
}
int gcd(int x, int y) // 14, 21
{
int d;
if(y>x)
{
y=x+y;
x=y-x;
y=y-x;
}
for(int i=1; i<=y; i++)
{
if(x%i==0 && y%i==0 )
{
d=i;
}
}
return d;
}
};
int main()
{
Test t;
t.check();
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int val1,val2;
cout << " Enter two integer values followed by enter" << endl << endl;
cin >> val1;
cin >> val2;
if(val1 < val2) // To determine which value is larger and which one is smaller
{
cout << val1 << " is smaller than" << val2 << endl << endl << "And" << val2 << " is larger than " << val1 << endl<<endl;
}
enter code here
else if( val2 < val1)
{
cout<<val2 <<" is smaller than"<< val1<<endl<<endl<<"And"<< val1 << " is larger than "<< val2<< endl << endl;
}
cout << "The sum of "<< val1<<" and "<<val2<<" is "<< val1+val2<<endl<<endl;
// diplaying the sum of the two numbers
enter code here
cout << " The difference between "<<val1<< " and "<<val2<< " is " << val1-val2<<endl;
// displays the difference of val2 from val1
cout << " The difference between "<<val2<< " and "<<val1<< " is " << val2-val1<<endl;
// displays thr difference of val1 fromval2
enter code here
enter code here
cout << " The product of " <<val1<< " and " << val2<< " is " << val1*val2<< endl<<endl;
// displaying the product of val1 and val2
enter code here
enter code here
enter code here
// now to diplay the ratio of the two numbers
double ratio1;
cout << " The ratio of "<<val1<<" and "<<val2<<" is ";
if(val1 < val2)
{
ratio1= ((double)val2) /val1;
cout << ratio1;
}
else if(val1 > val2)
{
ratio1= ((double)val1) /val2;
cout << ratio1;
}
}

logic help for smallest/largest value

I went thru so many version of the algorithm to sort smallest and largest that my brain is fried. The book up to this point and searching online haven't helped at all.
I'm having difficulties at saving the last.
I used 3 in, 10 cm and 5 cm as test cases. Entering 3 in first, becomes the largest, entering 5 cm second becomes smallest and then 10 cm becomes smallest again. Tried different version for over 2 hours, even re-wrote that entire section. In the book Programming Principles and Practices using C++, its in the review section, before that I cant find anything to help me out.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
vector<double>all_meters;
double smallest= 0,print_smallest, largest = 0,print_largest, num = 0;
string unit, s_input, num_s_input, small_unit, large_unit;
while(cin.good()){
cout << "\n\t\t\t\tEnter '|' to exit.\n\n";
cout << "\t\tNumber to compare followed by white space and unit:";
cin >> num_s_input;
if(num_s_input.compare("|") == 0 || (s_input.compare("|") == 0)){
double sum = 0;
for (double x : all_meters) sum+=x;
cout << "Sum: " << setprecision(4) << sum << "m\n";
cout << "Smallest number: " << print_smallest << small_unit << endl
<< "Largest number: " << print_largest << large_unit << endl
<< "Total number of values: " << all_meters.size() << endl
<< "All the entered numbers converted to meters are: \n";
for (double i = 0; i<all_meters.size(); ++i){
cout << all_meters[i] << setprecision(2) <<"m ";
}
cout << "\nAlright now, goodbye then !\n" << endl;
break;
}
else{
cin >> s_input;
num = strtod(num_s_input.c_str(), NULL);
unit = s_input;
double meter = 0;
if(unit=="cm"){
meter = num / 100;}
else if(unit=="in"){
meter = num / 39.370;}
else if(unit=="ft"){
meter = num / 3.2808;}
else if(unit=="m"){
meter = num;}
else {
cout << "\n\tYou entered wrong unit!\t\n";}
if(largest==0){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest==0&&meter<largest){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
else if(largest<meter){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest>meter){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
all_meters.push_back(meter);
sort(all_meters.begin(),all_meters.end());
}
}
}
Managed to solve it without using limit, added the new changes to the code. Thanks for the help guys !
More than likely your problem comes from the fact that you are initializing smallest to 0. If you never enter anything smaller than 0 then smallest will never change.
When finding the minimum and maximum values you want to set the the initial value to the largest or smallest number respectively that it can hold. So in this case we would use
double smallest = std::numeric_limits<double>::max();
double largest = std::numeric_limits<double>::lowest()
double num = 0;
This was anything in your data set should be less than smallest and everything should be grater than largest.
This does require #include <limits>
You need to choose a standard unit of measure. The question suggests meters, so use that (you can use float or double for this, depending on what precision you need).
The problem is then simple, create some variables for the sum, smallest seen, and largest seen, the for each new input, convert to the standard format, and update the variables.
The solution (to get you started, not working code) might look something like this:
// You can represent the different types of units as integers
float convertToMeters(float unconvertedValue, int unit) {
// Convert unconvertedValue based on unit
}
float smallest = std::numeric_limits<float>::max();
float largest = std::numeric_limits<float>::lowest();
float sum = 0.0f;
// Update for each new input
while (new_input) {
float convertedValue = convertToMeters(new_value, unit);
// Update total
sum += convertedValue;
// Update smallest and largest
if (convertedValue > largest) largest = convertedValue;
else if (convertedValue < smallest) smallest = convertedValue;
}
As Nathan mentioned, #include <limits> for the limits.

Picking up good values for a temperature program

I'm studying C++ by 2 months using the book : Programming principles and practice using C++. Now I'm reading the chapter about errors, and in a section the author speaks of logic errors. Before using a program as an example, and then a revised version to understand the error. This is the first program :
#include "std_lib_facilities.h"
// find the highest, lowest and averega temperature
int main()
{
vector<double> temps;
for (double temp; cin >> temp;)
temps.push_back(temp);
double sum = 0;
double high_temp = 0;
double low_temp = 0;
for (double x : temps)
{
if (x > high_temp) high_temp = x;
if (x < low_temp) low_temp = x;
sum += x;
}
cout << "Highest temperature : " << high_temp << '\n';
cout << "Lowest temperature : " << low_temp << '\n';
cout << "Average temperature : " << sum / temps.size() << '\n';
}
As you can see if I enter, for example, a set of temperatures relating to the month of August I will get wrong ouput results because I will enter just positive values but low_temp will remain 0.0 unless one of the temperatures in the data was below zero (impossible in summer !).
So the author revised this program :
#include "std_lib_facilities.h"
int main()
{
vector<double> temps;
double high_temp = -1000; // initialize to impossibly low
double low_temp = 1000; // initialize to impossibly high
double sum = 0;
int no_of_temps = 0;
for (double temp; cin >> temp;) {
++no_of_temps;
sum += temp;
if (temp > high_temp) high_temp = temp;
if (temp < low_temp) low_temp = temp;
}
cout << "Highest temperature : " << high_temp << '\n';
cout << "Lowest temperature : " << low_temp << '\n';
cout << "Average temperature : " << sum / no_of_temps<< '\n';
}
My problem lies in the exercise that the author asks me to do :
Look it up. Check some information sources to pick good values for the min_temp("the minimum temperaure") and max_temp("the maximum temperature") constants of our program. Those values will determine the usefulness of our program.
What does you mean the author with this exercise? the program still needs some improvements in your opinion? What values you would give to min_temp and max_temp ? What are the problems related to the solution used to write such a program ?
You should start with such constants such that the first comparison will always succeed. Picking +/-1000 is OK, but then you're artificially limiting yourself when you don't have to. A better solution might be to pick constants that are definitely at the extremes:
double high_temp = -std::numeric_limits<double>::infinity();
double low_temp = std::numeric_limits<double>::infinity();
Any temperature you enter will be higher than -inf and lower than inf, so that comparison will succeed and do the right thing.