Project 1: Pi Rather than putting all your code in main() , use functions to per
ID: 3782815 • Letter: P
Question
Project 1: Pi
Rather than putting all your code in main(), use functions to perform the calculation. Embed your program in a loop so that the calculation can be repeated multiple times.
Project 2: Tailor
The purpose of this C++ program is to determine how much cloth in square inches is needed to make a certain type of garment. You should base your calculations on the following table:
Rather than putting all your code in main(), use functions to perform the calculation. Embed your program in a loop so that the calculation can be repeated multiple times. Use constants to hold the literal values in the above table, for example:
const double PANTS_WAIST_FACTOR = 2 + 1.0/2;
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: P
Gimme your waist size in inches: 30
Gimme your height size in inches: 72
Pleaded front? [Y/N]: Y
Baggy Look? [Y/N]: N
For your pants, you'll need 114 square inches of fabric!
Try again? [Y/N]: Y
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: S
Gimme your waist size in inches: 32
Gimme your height size in inches: 50
Long sleeves? [Y/N]: Y
Gimme your arms length in inches: 25
For your shirts, you'll need 168.2222 square inches of fabric!
Try again? [Y/N]: Y
Tailor Fabric Calculator:
Whaddya want? [P]ants or [S]hirts or shor[T]s: T
Gimme your waist size in inches: 35
Gimme your height size in inches: 72
Pockets? [Y/N]: Y
For your shorts, you'll need 73.025 square inches of fabric!
Try again? [Y/N]: N
Explanation / Answer
Project 1: PI
// Calculating Pi for specified Iterations
// Using Leibinz’s Formula
#include <iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
// Function to calculate Pi using Leibinz Formula
double cal_pi_leibniz(int iter)
{
int j=0;
double sum=0;
for(j=0; j<=iter; j++)
{
sum+= -1/(double)(2*j+1);
sum*=-1;
}
return sum;
}
int main()
{
int no_of_iteration;
cout << “Enter number of iterations to calculate: “;
cin >> no_of_iteration;
cout << "The value of Pi after " << no_of_iteration << " iterations is approximately: " << cal_pi_leibniz(no_of_iteration) * 4 << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.