Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Super new to coding and I've been stuck for awhile on this question. I need to make a program that inputs the temperature in Celsius and an increment that is inputted for a total of 20 lines. In the process it converts to Fahrenheit, Kelvin, and Rankine. That is all fine but I can't get the values to increment by my input.
e.g., As it should look:
Enter temperature in Cel: 50
Enter increment: 5
Cel Far Kel Rank
1 - 50 ...............................
2 - 55 ..............................
3 - 60 ..............................
.
.
.
.
20 - 150 .............................
I can't get the values to increment at all. Being reading my notes and looking online to understand the issue but no luck.
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int CELS; // celsius entry
int x; // x = input value for increment
double FAH = (CELS * (9.0 / 5) + 32); // farenheit conversion
double KEL = (CELS + 273.15); // kelvin conversion
double RANK = ((CELS + 273.15) * (9.0 / 5)); // rankine conversion
cout << "Enter the temperature in celsius: ";
cin >> CELS;
while ((CELS < -273.15) || (CELS > 5000))
{
cout << "ERROR: out of range." << endl;
cout << "Enter the temperature in celsius: ";
cin >> CELS;
}
cout << "Enter the increment value: ";
cin >> x;
cout << endl;
cout << " # Cels Fahr Kel Rank" << endl;
cout << right;
for (int i = 1; i <= 20; i++)
{ //
for (double j = CELS; j <= CELS; x++)
{ //
for (double k = (CELS * (9.0 / 5) + 32);
k <= (CELS * (9.0 / 5) + 32); x++)
{
for (double m = (CELS + 273.15); m <= (CELS + 273.15); x++)
{
for (double n = ((CELS + 273.15) * (9.0 / 5));
n <= ((CELS + 273.15) * (9.0 / 5)); x++)
{
cout << setw(10) << i << setw(10) << j << setw(10) << k
<< setw(10) << m << setw(10) << n << endl;
}
}
}
}
}
}
And ignore why I have my formulas in the for loop. Formulas were not working using the declared variables so have just done this in the mean time.
You can use some user defined functions to calculate the conversion from Celsius to the other units.
Then you only need one loop:
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::setw;
// define some constant
const double zero_point = 273.15;
constexpr double factor = 9.0 / 5.0;
const double max_cels = 5000;
// farenheit conversion
double cels_to_far( double cels ) {
return cels * factor + 32.0;
}
// kelvin conversion
double cels_to_kel( double cels ) {
return cels + zero_point;
}
// rankine conversion
double cels_to_ran( double cels ) {
return ( cels + zero_point ) * factor;
}
int main()
{
double temp_cels;
double delta_t;
cout << "Enter the temperature in celsius: ";
cin >> temp_cels;
while ((temp_cels < -zero_point) || (temp_cels > 5000)) {
cout << "ERROR: out of range.\n";
cout << "Enter the temperature in celsius: ";
cin >> temp_cels;
}
cout << "Enter the increment value: ";
cin >> delta_t;
cout << "\n # Cels Fahr Kel Rank\n";
// output loop
for ( int i = 0; i < 20; ) {
cout << setw(10) << ++i << std::fixed << std::setprecision(2)
<< setw(10) << temp_cels
<< setw(10) << cels_to_far(temp_cels)
<< setw(10) << cels_to_kel(temp_cels)
<< setw(10) << cels_to_ran(temp_cels) << std::endl;
temp_cels += delta_t;
}
}
The output is as expected ( 50° Celsius and increment of 5°):
# Cels Fahr Kel Rank
1 50.00 122.00 323.15 581.67
2 55.00 131.00 328.15 590.67
3 60.00 140.00 333.15 599.67
4 65.00 149.00 338.15 608.67
5 70.00 158.00 343.15 617.67
6 75.00 167.00 348.15 626.67
7 80.00 176.00 353.15 635.67
8 85.00 185.00 358.15 644.67
9 90.00 194.00 363.15 653.67
10 95.00 203.00 368.15 662.67
11 100.00 212.00 373.15 671.67
12 105.00 221.00 378.15 680.67
13 110.00 230.00 383.15 689.67
14 115.00 239.00 388.15 698.67
15 120.00 248.00 393.15 707.67
16 125.00 257.00 398.15 716.67
17 130.00 266.00 403.15 725.67
18 135.00 275.00 408.15 734.67
19 140.00 284.00 413.15 743.67
20 145.00 293.00 418.15 752.67
You need exactly one for loop, incrementing by x. ( The nested for loops are a mistake )
Start with the one, single for loop that you need: the one that counts through the lines of the output table.
In the body of the loop, calculate everything you need for that line, and output it.
Something like this:
for( int line_count = 0;
line_count < 20;
line_count++ )
{
double line_temp_celsius =
start_celsius + line_count * celsius_increment;
// calculate the other values based line_temp_celsius
...
}
The calculation must be in the loop where you change the value of CELS.
There should be a single loop. The result looks like this. See live demo
for (int i = 1; i <= 20; i++, CELS+=x)
{
double FAR = (CELS * (9.0 / 5) + 32); // farenheit conversion
double KEL = (CELS + 273.15); // kelvin conversion
double RANK = ((CELS + 273.15) * (9.0 / 5)); // rankine conversion
cout << setw(10) << i << setw(10) << CELS << setw(10) << FAR
<< setw(10) << KEL << setw(10) << RANK << endl;
}
Related
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <sstream>
using namespace std;
int iData, tData;
void randgen(int max, int min){
srand((unsigned) time(0));
}
int main()
{
cout << "Masukkan jumlah data: ";
cin >> iData;
int jData[iData], randNum[iData], fProb[iData];
double probkei[iData], tKumul[iData],tepiA[iData], tepiB[iData];
int tData;
for(int i=1; i<=iData; i++){
cout << "Masukkan data ke-" << i << ": ";
cin >> jData[i];
tData += jData[i]; //jumlahkan seluruh data untuk mencari probabilitas tiap variabel
}system("cls");
probkei[0]=0;
cout << setw(10) << "Data ke" << setw(10) << "Frekuensi" << setw(15) << "Probabilitas" << setw(20) << "Kumulatif" << setw(10) << "Interval" << endl;
for(int i=0; i<iData; i++){
probkei[i] = (double) jData[i]/tData; //typecast integer to double for the probability
if(jData[i]==jData[1]){
tKumul[i] = probkei[i];
}else if(i<i+i){
tKumul[i] = probkei[i] + probkei[i+1]; //for cumulative sum 1 way
}
probkei[i] = round(probkei[i] * 1000.0) / 1000.0; //rounding the probability
tKumul[i] = round(tKumul[i] * 1000.0) / 1000.0;
cout << setw(10) << i+1 << setw(10) << jData[i] << setw(15) << probkei[i] << setw(20);
int temp;
cout<<"data "<<probkei[i]+probkei[i+1]; //for cumulative sum 2 way
cout << setw(10) << tKumul[i] << endl;
/*if (i == iData || jData[i] != jData[i - 1])
{
temp += count;
cout << "Cumulative frequency of " << jData[i - 1] << " in the array is: " << temp << endl;
count = 1;
}else{
count++;
}*/
}
cout << setw(20) << "Total data: " << tData << endl;
return 0;
}
I want to count cumulative frequency from my array data.
First is entering the value/total of the number of data in the array. Next is entering the value for each data one by one and then counting all probabilities of each data(the possibilities are declared in double). And then counting the cumulative which is sum the n data with the n+1 data. And the last is making the top and bottom edges for each data to be used as a random number interval.
I've done my best and finding the solution but I still confused why it's doesn't work.
I was trying to count it in 2 ways but all of them do nothing.
This is a Monte Carlo Simulation.
example Table
This:
int iData;
cin >> iData;
int jData[iData];
is using variable-length arrays, which are not standard C++. Rather use std::vector instead:
int iData;
cin >> iData;
std::vector<int> jData(iData);
The tData local variable is uninitialized:
int tData;
...
tData += jData[i];
It should be initialized to 0.
The condition i<i+i doesn't make sense.
There is something weird going on with the indexes. The input is loaded from index 1 but the second loop starts from 0. This loading from 1 is also not accounted in size of the arrays, so the last element will overflow the array.
There is something wrong with this too:
tKumul[i] = probkei[i] + probkei[i+1];
If this is supposed to be cumulative sum then tKumul should appear on the right side too.
If we load data from 0, then the second loop should look like this:
for (int i = 0; i < iData; i++) {
probkei[i] = (double) jData[i] / tData;
if (i == 0) {
tKumul[i] = probkei[i];
} else {
tKumul[i] = probkei[i] + tKumul[i-1];
}
With this code (see godbolt) the output is:
Data ke Frekuensi Probabilitas Kumulatif
1 5 0.067 0.067
2 10 0.133 0.2
3 15 0.2 0.4
4 20 0.267 0.667
5 25 0.333 1
Total data: 75
In addition I would suggest using fixed and setprecision(3) instead of manual rounding:
cout << fixed << setprecision(3);
and using algorithms instead of loops. Calculating probabilities can be replaced by std::transform and calculating cumulative sum can be replaced by std::partial_sum:
std::transform(
jData.begin(), jData.end(),
probkei.begin(),
[tData](auto elem) { return (double) elem / tData; }
);
std::partial_sum(probkei.begin(), probkei.end(), tKumul.begin());
With this code (see godbolt) the output is:
Data ke Frekuensi Probabilitas Kumulatif
1 5 0.067 0.067
2 10 0.133 0.200
3 15 0.200 0.400
4 20 0.267 0.667
5 25 0.333 1.000
Total data: 75
For some reason the values in the loop do not update. Instead of taking the new values they are assigned in the loop they stay the same as when first assigned. Why is this?
using namespace std;
int main() {
int town_A_Pop;
int town_A_Growth;
int town_B_Pop;
int town_B_Growth;
int years = 0;
cout << "Enter the polulation of town A and of town B:" << endl;
cin >> town_A_Pop >> town_B_Pop;
cout << "Enter the growth rate of town A and of town B:" << endl;
cin >> town_A_Growth >> town_B_Growth;
do {
town_A_Pop = town_A_Pop * (1 + (town_A_Growth / 100));
cout << town_A_Pop << endl;
town_B_Pop = town_B_Pop * (1 + (town_B_Growth / 100));
cout << town_B_Pop << endl;
years++;
}
while (town_A_Pop < town_B_pop);
cout << "It took " << years << " years for Town A to overtake Town B." << endl;
cout << "Town A Population: " << town_A_Pop << endl;
cout << "Town B Population: " << town_B_Pop << endl;
return 0;
}
I assume that town_A_Pop and town_B_Pop don't update.
If town_A_Growth is less than 100(and greater or same than 0) then (town_A_Growth / 100) will evaluate to 0 and (1 + (town_A_Growth / 100)) will evaluate to 1.
In that case, town_A_Pop = town_A_Pop * 1 so it will stay same.
Same goes to town_B_Pop.
To fix the problem, you can change variables into float or double, or multiply first then divide by 100 like below:
do {
town_A_Pop = (town_A_Pop * (100 + town_A_Growth)) / 100;
cout << town_A_Pop << endl;
town_B_Pop = (town_B_Pop * (100 + town_B_Growth)) / 100;
cout << town_B_Pop << endl;
years++;
}
while (town_A_Pop < town_B_pop);
It will still won't get bigger if both population and growth are too small, though.
Your problem is Type Variables Error
int town_A_Pop;
int town_A_Growth;
int town_B_Pop;
int town_B_Growth;
int years = 0;
change to
double town_A_Pop;
double town_A_Growth;
double town_B_Pop;
double town_B_Growth;
double years;
This should be ok. Because if using int to do division it not able return decimal number.
Like:
int tmp = 1;
int division = tmp / 5; //This result will return 0 , not return 0.2
I am attempting to write a program that estimates Pi based on the Monte Carlo method via a random number generator. I am attempting to estimate Pi within 1, 2, 3, 4, 5, and 6 digits of accuracy and have the program print to the screen how many points it took to get within .1 digit of Pi, then .01 digits of Pi and so forth all the way until .000001 digits of Pi. I am allowing the user to input an amount of trials they would like to run, so it will print "Trial 1, 2, 3, 4" etc. with all of the information I listed above. I am stuck on one last bit, and that is having it loop back through the calculations (it will not print more than just trial 1). Though I am not getting a message that the program has terminated, so I can not tell if it is my while loop failing or my nested for loops. Please help! :)
I have attempted switching around the for loops as well as trying different varying if statements. This is the closest I have gotten it to running the way I would like with exception of allowing the user to run multiple trials.
#include "pch.h"
#include <iostream> //need this by default for cin
#include <math.h> //includes math functions
#include <cmath> //includes basic math
#include <cfloat> //includes floating point numbers
#include <iomanip> //includes setprecision for decimal places
#include <cstdlib> //needed for rand and srand functions
#include <ctime> //needed for time function used to seed generator
using namespace std;
int main()
{
cout << "The purpose of this program is to estimate pi using the monte
carlo method and a random number generator" << endl << endl;
unsigned seed = time(0);
srand(seed);
float radius;
int trialcount = 0;
int trials;
float accuracy;
const float pi = 3.14159265;
float randpi = 0;
int squarecount = 0;
int circlecount = 0;
float x;
float y;
int n;
cout << "The value of PI can be found as the ratio of areas of a circle of radius r located within a square of side 2r" << endl;
cout << "This program runs a MonteCarlo Simulation that generates numbers located randomly within a square" << endl;
cout << "The count of values within the square and the count of numbers within the circle approximate their areas" << endl;
cout << "An input value of radius determines the size of the circle and square" << endl;
cout << "The user specifies how many trials or test runs are desired" << endl << endl;
cout << "The true value of PI to 8 decimal places is 3.14159265" << endl << endl;
cout << "Input a value for radius: ";
cin >> radius;
cout << endl;
cout << "How many trials would you like? ";
cin >> trials;
cout << endl << endl;
cout << "Square count gives the Total number of random samples (they are within the square)" << endl;
cout << "Circle count gives the number of random samples that also fall within the circle" << endl << endl;
while (trialcount != trials)
{
accuracy = .1;
cout << "Trial " << trialcount + 1 << endl;
cout << "Accuracy \t\t" << "Square Count \t\t" << "Circle Count \t\t" << "Pi" << endl << endl;
for (int j = 0; randpi >= pi - accuracy || randpi <= pi + accuracy; j++)
{
cout << setprecision(6) << fixed << accuracy << " \t\t" << squarecount << " \t\t" << circlecount << " \t\t" << randpi << endl << endl;
accuracy = accuracy / 10;
for (int i = 0; randpi >= pi + accuracy || randpi <= pi - accuracy; i++)
{
x = (float)(rand());
x = (x / 32767) * radius;
y = (float)(rand());
y = (y / 32767) * radius;
squarecount++;
if ((x * x) + (y * y) <= (radius * radius))
{
circlecount++;
}
randpi = float(4 * circlecount) / squarecount;
}
}
trialcount++;
}
}
Problems I see:
Problem 1
The first for loop does not make any sense. If you want to make sure that you use accuracies of 0.1, 0.01, 0.001, etc. you just need a simple for loop. The following should do:
for ( int j = 0; j < 6; ++j )
{
...
}
Problem 2
x and y values are computed incorrectly. You want to make sure that their values are less than or equal to radius. However, when you use:
x = (float)(rand());
x = (x / 32767) * radius;
y = (float)(rand());
y = (y / 32767) * radius;
they are not guaranteed to be less than or equal to radius. They will be more than radius more often than they will not. You need to use
x = (float)(rand() % 32768);
x = (x / 32767) * radius;
y = (float)(rand() % 32768);
y = (y / 32767) * radius;
Problem 3
You need to reset the values of randpi, squarecount, and circlecount in every iteration of the inner for loop. Otherwise, your computations will be affected by computations from the previous iteration.
The outer for loop must start with:
for (int j = 0; j < 6; j++)
{
accuracy /= 10;
randpi = 0;
squarecount = 0;
circlecount = 0;
Problem 4
The inner for loop must be constrained to only run upto a certain number of times. If for some reason the accuracy is not achieved, you want to make sure you don't overflow i. For example:
int stopAt = (INT_MAX >> 8);
for (int i = 0; (randpi >= pi + accuracy || randpi <= pi - accuracy) && i < stopAt; i++)
For machines that use 32 bit ints, which is the most common in practice today, you won't run the loop any more than 0x7FFFFF (8388607 in decimal) times.
This is the core problem in your code. Your computations don't converge some times and you don't make sure you exit after a certain number of iterations of the loop.
Further improvement
You don't need radius as a variable in your program. You can compute x and y as:
x = (float)(rand() % 32768);
x = (x / 32767);
y = (float)(rand() % 32768);
y = (y / 32767);
and change the logic to check whether this is a point within the circle to
if ((x * x) + (y * y) <= 1.0 )
You should also attempt to define variables only in the scopes where you need them. This will make sure that you don't end up using stale values from a previous run of the iteration.
Revised program
The following revised program works for me.
#include <iostream> //need this by default for cin
#include <math.h> //includes math functions
#include <cmath> //includes basic math
#include <cfloat> //includes floating point numbers
#include <iomanip> //includes setprecision for decimal places
#include <cstdlib> //needed for rand and srand functions
#include <ctime> //needed for time function used to seed generator
#include <climits>
using namespace std;
int main()
{
cout << "The purpose of this program is to estimate pi using the monte "
"carlo method and a random number generator" << endl << endl;
unsigned seed = time(0);
srand(seed);
int trialcount = 0;
int trials;
float accuracy;
const float pi = 3.14159265;
cout << "The value of PI can be found as the ratio of areas of a circle of radius r located within a square of side 2r" << endl;
cout << "This program runs a MonteCarlo Simulation that generates numbers located randomly within a square" << endl;
cout << "The count of values within the square and the count of numbers within the circle approximate their areas" << endl;
cout << "An input value of radius determines the size of the circle and square" << endl;
cout << "The user specifies how many trials or test runs are desired" << endl << endl;
cout << "The true value of PI to 8 decimal places is 3.14159265" << endl << endl;
cout << endl;
cout << "How many trials would you like? ";
cin >> trials;
cout << endl << endl;
cout << "Square count gives the Total number of random samples (they are within the square)" << endl;
cout << "Circle count gives the number of random samples that also fall within the circle" << endl << endl;
while (trialcount != trials)
{
accuracy = 0.1;
cout << "Trial " << trialcount + 1 << endl;
cout << "Accuracy \t\t" << "Square Count \t\t" << "Circle Count \t\t" << "Pi" << endl << endl;
for (int j = 0; j < 6; j++)
{
accuracy /= 10;
float randpi = 0;
int squarecount = 0;
int circlecount = 0;
int stopAt = (INT_MAX >> 8);
for (int i = 0; (randpi >= pi + accuracy || randpi <= pi - accuracy) && i < stopAt; i++)
{
float x = ((float)(rand() % 32768) / 32767);
float y = ((float)(rand() % 32768) / 32767);
squarecount++;
if ((x * x) + (y * y) <= 1.0 )
{
circlecount++;
}
randpi = float(4 * circlecount) / squarecount;
}
cout << setprecision(8) << fixed << accuracy << " \t\t" << squarecount << " \t\t" << circlecount << " \t\t" << randpi << endl << endl;
}
trialcount++;
}
}
See it working at https://ideone.com/laF27X.
Everything is working like it's supposed too, except when I output to the text file I can not figure out how to keep all the output lined up. My teacher wouldn't help me and I've literally been doing trial and error with "fixed, setprecision, left, setw(), etc" for a few hours now.
//Project #5
//Start Date: November 17th
//Due Date: November 23rd
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
double f = 3.14159;
double upperBound = 0;
double lowerBound = 0;
double increment = 0;
double tempVal = 0;
double powVal = 0;
double expVal = 0;
double fact = 0;
double sinVal = 0;
double x = 0.0;
char userCont = 'Y';
int n = 0;
int i = 0;
//Factorial function using recursion...
double factorial(const int n) {
if (n <= 1) return 1;
fact = n * factorial(n - 1);
return fact;
}
//Power function using recursion...
double power(const double x, const int n) {
if (n <= 0)
return 1;
powVal = x * power(x, n - 1);
return powVal;
}
//my_sin function using power and factorial functions
double my_sin(const double x) {
sinVal = 0;
for (int k = 0; k < 50; k++) {
sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}
//my_exp(x) Function
double my_exp(const double x) {
expVal = 0;
for (int k = 0; k < 50; k++) {
expVal += power(x, k) / factorial(k);
}
return expVal;
}
int main() {
ofstream fout("output.text");
while (userCont == 'y' || userCont == 'Y') {
cout << "Enter lower and upper bounds: ";
cin >> upperBound >> lowerBound;
cout << "Enter Increment: ";
cin >> increment;
//Checking if upper and lower bounds are in the right order...
if (upperBound < lowerBound) {
tempVal = upperBound;
upperBound = lowerBound;
lowerBound = tempVal;
}
fout << "x sin(x) my_sin(x) e(x) " <<
"my_el(x) my_exp(x)" << endl;
//Loop to display and increase x by the incrememnt
for (x = lowerBound; x <= upperBound; x = x + increment) {
fout.precision(7);
fout << setw(8) << left << x << " ";
fout << setw(8) << my_sin(x) << setw(8) << " ";
fout << setw(8) << sin(x) << setw(8) << " ";
fout << setw(8) << exp(x) << setw(8) << " ";
fout << setw(8) << exp(x) << setw(8) << " ";
fout << my_exp(x) << endl;
}
cout << "Another (y/n)? ";
cin >> userCont;
}
return 0;
}
Here's how it's supposed to look
Some cleanup and the best I could to mimick the formatting, hopefully it is of some inspiration/assistance.
Important Note how I removed all the global variables. They're bad :(
Live On Coliru
// Project #5
// Start Date: November 17th
// Due Date: November 23rd
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
double factorial(const int n) {
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
double power(const double x, const int n) {
if (n <= 0)
return 1;
return x * power(x, n - 1);
}
// my_sin function using power and factorial functions
double my_sin(const double x) {
double sinVal = 0;
for (int k = 0; k < 50; k++) {
sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}
// my_exp(x) Function
double my_exp(const double x) {
double expVal = 0;
for (int k = 0; k < 50; k++) {
expVal += power(x, k) / factorial(k);
}
return expVal;
}
int main() {
ofstream fout("output.text");
char userCont = 'Y';
while (userCont == 'y' || userCont == 'Y') {
cout << "Enter lower and upper bounds: ";
double upperBound, lowerBound;
cin >> upperBound >> lowerBound;
cout << "Enter Increment: ";
double increment = 0;
cin >> increment;
// Checking if upper and lower bounds are in the right order...
if (upperBound < lowerBound) {
std::swap(upperBound, lowerBound);
}
fout << " x sin(x) my_sin(x) e(x) my_el(x) my_exp(x)" << endl;
// Loop to display and increase x by the incrememnt
for (double x = lowerBound; x <= upperBound; x = x + increment) {
fout.precision(7);
fout << right << std::fixed << std::setprecision(7) << setw(12) << x << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << my_sin(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << sin(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x) << setw(4) << " ";
fout << right << std::fixed << std::setprecision(7) << setw(12) << exp(x) << setw(4) << " ";
fout << my_exp(x) << endl;
}
cout << "Another (y/n)? ";
cin >> userCont;
}
}
Output:
x sin(x) my_sin(x) e(x) my_el(x) my_exp(x)
0.0000000 0.0000000 0.0000000 1.0000000 1.0000000 1.0000000
0.1000000 0.0998334 0.0998334 1.1051709 1.1051709 1.1051709
0.2000000 0.1986693 0.1986693 1.2214028 1.2214028 1.2214028
0.3000000 0.2955202 0.2955202 1.3498588 1.3498588 1.3498588
0.4000000 0.3894183 0.3894183 1.4918247 1.4918247 1.4918247
0.5000000 0.4794255 0.4794255 1.6487213 1.6487213 1.6487213
0.6000000 0.5646425 0.5646425 1.8221188 1.8221188 1.8221188
0.7000000 0.6442177 0.6442177 2.0137527 2.0137527 2.0137527
0.8000000 0.7173561 0.7173561 2.2255409 2.2255409 2.2255409
0.9000000 0.7833269 0.7833269 2.4596031 2.4596031 2.4596031
1.0000000 0.8414710 0.8414710 2.7182818 2.7182818 2.7182818
1.1000000 0.8912074 0.8912074 3.0041660 3.0041660 3.0041660
1.2000000 0.9320391 0.9320391 3.3201169 3.3201169 3.3201169
1.3000000 0.9635582 0.9635582 3.6692967 3.6692967 3.6692967
1.4000000 0.9854497 0.9854497 4.0552000 4.0552000 4.0552000
1.5000000 0.9974950 0.9974950 4.4816891 4.4816891 4.4816891
1.6000000 0.9995736 0.9995736 4.9530324 4.9530324 4.9530324
1.7000000 0.9916648 0.9916648 5.4739474 5.4739474 5.4739474
1.8000000 0.9738476 0.9738476 6.0496475 6.0496475 6.0496475
1.9000000 0.9463001 0.9463001 6.6858944 6.6858944 6.6858944
2.0000000 0.9092974 0.9092974 7.3890561 7.3890561 7.3890561
2.1000000 0.8632094 0.8632094 8.1661699 8.1661699 8.1661699
2.2000000 0.8084964 0.8084964 9.0250135 9.0250135 9.0250135
2.3000000 0.7457052 0.7457052 9.9741825 9.9741825 9.9741825
2.4000000 0.6754632 0.6754632 11.0231764 11.0231764 11.0231764
2.5000000 0.5984721 0.5984721 12.1824940 12.1824940 12.1824940
2.6000000 0.5155014 0.5155014 13.4637380 13.4637380 13.4637380
2.7000000 0.4273799 0.4273799 14.8797317 14.8797317 14.8797317
2.8000000 0.3349882 0.3349882 16.4446468 16.4446468 16.4446468
2.9000000 0.2392493 0.2392493 18.1741454 18.1741454 18.1741454
3.0000000 0.1411200 0.1411200 20.0855369 20.0855369 20.0855369
3.1000000 0.0415807 0.0415807 22.1979513 22.1979513 22.1979513
3.2000000 -0.0583741 -0.0583741 24.5325302 24.5325302 24.5325302
3.3000000 -0.1577457 -0.1577457 27.1126389 27.1126389 27.1126389
3.4000000 -0.2555411 -0.2555411 29.9641000 29.9641000 29.9641000
3.5000000 -0.3507832 -0.3507832 33.1154520 33.1154520 33.1154520
3.6000000 -0.4425204 -0.4425204 36.5982344 36.5982344 36.5982344
3.7000000 -0.5298361 -0.5298361 40.4473044 40.4473044 40.4473044
3.8000000 -0.6118579 -0.6118579 44.7011845 44.7011845 44.7011845
3.9000000 -0.6877662 -0.6877662 49.4024491 49.4024491 49.4024491
4.0000000 -0.7568025 -0.7568025 54.5981500 54.5981500 54.5981500
4.1000000 -0.8182771 -0.8182771 60.3402876 60.3402876 60.3402876
4.2000000 -0.8715758 -0.8715758 66.6863310 66.6863310 66.6863310
4.3000000 -0.9161659 -0.9161659 73.6997937 73.6997937 73.6997937
4.4000000 -0.9516021 -0.9516021 81.4508687 81.4508687 81.4508687
4.5000000 -0.9775301 -0.9775301 90.0171313 90.0171313 90.0171313
4.6000000 -0.9936910 -0.9936910 99.4843156 99.4843156 99.4843156
4.7000000 -0.9999233 -0.9999233 109.9471725 109.9471725 109.9471725
4.8000000 -0.9961646 -0.9961646 121.5104175 121.5104175 121.5104175
4.9000000 -0.9824526 -0.9824526 134.2897797 134.2897797 134.2897797
5.0000000 -0.9589243 -0.9589243 148.4131591 148.4131591 148.4131591
5.1000000 -0.9258147 -0.9258147 164.0219073 164.0219073 164.0219073
5.2000000 -0.8834547 -0.8834547 181.2722419 181.2722419 181.2722419
5.3000000 -0.8322674 -0.8322674 200.3368100 200.3368100 200.3368100
5.4000000 -0.7727645 -0.7727645 221.4064162 221.4064162 221.4064162
5.5000000 -0.7055403 -0.7055403 244.6919323 244.6919323 244.6919323
5.6000000 -0.6312666 -0.6312666 270.4264074 270.4264074 270.4264074
5.7000000 -0.5506855 -0.5506855 298.8674010 298.8674010 298.8674010
5.8000000 -0.4646022 -0.4646022 330.2995599 330.2995599 330.2995599
5.9000000 -0.3738767 -0.3738767 365.0374679 365.0374679 365.0374679
6.0000000 -0.2794155 -0.2794155 403.4287935 403.4287935 403.4287935
6.1000000 -0.1821625 -0.1821625 445.8577701 445.8577701 445.8577701
6.2000000 -0.0830894 -0.0830894 492.7490411 492.7490411 492.7490411
6.3000000 0.0168139 0.0168139 544.5719101 544.5719101 544.5719101
6.4000000 0.1165492 0.1165492 601.8450379 601.8450379 601.8450379
6.5000000 0.2151200 0.2151200 665.1416330 665.1416330 665.1416330
6.6000000 0.3115414 0.3115414 735.0951892 735.0951892 735.0951892
6.7000000 0.4048499 0.4048499 812.4058252 812.4058252 812.4058252
6.8000000 0.4941134 0.4941134 897.8472917 897.8472917 897.8472917
6.9000000 0.5784398 0.5784398 992.2747156 992.2747156 992.2747156
7.0000000 0.6569866 0.6569866 1096.6331584 1096.6331584 1096.6331584
7.1000000 0.7289690 0.7289690 1211.9670745 1211.9670745 1211.9670745
7.2000000 0.7936679 0.7936679 1339.4307644 1339.4307644 1339.4307644
7.3000000 0.8504366 0.8504366 1480.2999276 1480.2999276 1480.2999276
7.4000000 0.8987081 0.8987081 1635.9844300 1635.9844300 1635.9844300
7.5000000 0.9380000 0.9380000 1808.0424145 1808.0424145 1808.0424145
7.6000000 0.9679197 0.9679197 1998.1958951 1998.1958951 1998.1958951
7.7000000 0.9881682 0.9881682 2208.3479919 2208.3479919 2208.3479919
7.8000000 0.9985433 0.9985433 2440.6019776 2440.6019776 2440.6019776
7.9000000 0.9989413 0.9989413 2697.2823283 2697.2823283 2697.2823283
8.0000000 0.9893582 0.9893582 2980.9579870 2980.9579870 2980.9579870
8.1000000 0.9698898 0.9698898 3294.4680753 3294.4680753 3294.4680753
8.2000000 0.9407306 0.9407306 3640.9503073 3640.9503073 3640.9503073
8.3000000 0.9021718 0.9021718 4023.8723938 4023.8723938 4023.8723938
8.4000000 0.8545989 0.8545989 4447.0667477 4447.0667477 4447.0667477
8.5000000 0.7984871 0.7984871 4914.7688403 4914.7688403 4914.7688403
8.6000000 0.7343971 0.7343971 5431.6595914 5431.6595914 5431.6595914
8.7000000 0.6629692 0.6629692 6002.9122173 6002.9122173 6002.9122173
8.8000000 0.5849172 0.5849172 6634.2440063 6634.2440063 6634.2440063
8.9000000 0.5010209 0.5010209 7331.9735392 7331.9735392 7331.9735392
9.0000000 0.4121185 0.4121185 8103.0839276 8103.0839276 8103.0839276
9.1000000 0.3190984 0.3190984 8955.2927035 8955.2927035 8955.2927035
9.2000000 0.2228899 0.2228899 9897.1290587 9897.1290587 9897.1290587
9.3000000 0.1244544 0.1244544 10938.0192082 10938.0192082 10938.0192082
9.4000000 0.0247754 0.0247754 12088.3807302 12088.3807302 12088.3807302
9.5000000 -0.0751511 -0.0751511 13359.7268297 13359.7268297 13359.7268297
9.6000000 -0.1743268 -0.1743268 14764.7815656 14764.7815656 14764.7815656
9.7000000 -0.2717606 -0.2717606 16317.6071980 16317.6071980 16317.6071980
9.8000000 -0.3664791 -0.3664791 18033.7449278 18033.7449278 18033.7449278
9.9000000 -0.4575359 -0.4575359 19930.3704382 19930.3704382 19930.3704382
10.0000000 -0.5440211 -0.5440211 22026.4657948 22026.4657948 22026.4657948
# include <iostream>
# include <math.h>
# include <cstdlib>
using namespace std;
double cosin_value( double value);
double sin_value( double value);
double big_degree (double value);
double big_radian (double value);
double x;
double value;
double degree;
double radian;
const double PI = 3.14159;
char choice;
char yes ;
int main()
{
cout << "Please enter an angle value => ";
cin >> value;
cout << "Is the angle in Degree or Radian?" << endl;
cout << "\t" << "Type D if it is in Degree" << endl;
cout << "\t" << "Type R if it is in Radian" << endl;
cout << "Your response => ";
cin >> choice; //degree or radian?
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
if (choice == 'D' || choice == 'd')
{
big_degree (value);
cout << " " << "sin(x) = " << "\t" << sin_value(degree) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(degree) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(degree)/cosin_value(degree) << endl;
}
else if (choice == 'R' || choice == 'r')
{
cout << " " << "sin(x) = " << "\t" << sin_value(radian) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(radian) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(radian)/cosin_value(radian) << endl;
}
return 0;
}
// Sine,cosine functions
// angle -360<value<360
double sin_value( double value)
{
int count=1;
double sine, num, dem, sign, term;
sine=0;
sign = 1;
num = value;
dem = count;
while ( count <= 20 )
{
term = (num/dem);
sine = sine + term*sign;
num = num*value*value;
count = count + 2;
dem = dem * count * (count-1);
sign = -sign;
}
return (sine);
}
double cosin_value( double value)
{
int count=0;
double cosine, num, dem, sign, term;
cosine=0;
sign = 1;
num = 1;
dem = 1;
while ( count <= 20 )
{
term = (num/dem);
cosine = cosine + term*sign;
num = num*value*value;
count = count + 2;
dem = dem * count * (count-1);
sign = -sign;
}
return (cosine);
}
double big_degree (double value)
{
int result;
const int angle=360;
if (value >= 360 || value <= -360)
{
result=value/angle;
degree=(value-(result* angle))*PI/180;
}
else
{
degree = (value*PI)/180;
}
return (degree);
}
double big_radian (double value)
{
int result;
if (value >= 2*PI || value <= -2*PI)
{
result=value/(2*PI);
radian=(value-(result* 2*PI));
}
else
{
radian = value;
}
return (radian);
}
Hi, this is basically the whole program I wrote for calculating trigonometric value using the extent knowledge I knew in C++ as a beginner. For a better view, you can refer to this link regarding my code above :codepad.org
the line starting from line 114 onwards are the function that I created. There's a problem there where how can I compute my cosx to be 0 when the value is 90 degree or pi/2 radian?
since the program will still calculate tanx for me even the value is 90 degree.
Let's say by giving value 90 degree to the program, it will give me the value of 0.0000013268 instead of 0.000000
sorry, since I'm just a beginner, the code will look weird for you guys.
I appreciate your guides!
double big_degree(double value) means when the value is >= 360 or <= -360*
I do not allocate any heap space in my brain for digits of pi, but I do remember that atan(1) == pi / 4.
Change your PI constant like so:
const double PI = atan(1) * 4;
Taking your code, making that change, I get
Please enter an angle value => 90
Is the angle in Degree or Radian?
Type D if it is in Degree
Type R if it is in Radian
Your response => d
sin(x) = 1.0000000000
cos(x) = 0.0000000000
tan(x) = 15555226593901466.0000000000
const double PI = 3.14159;
The more precise you make this definition, the more close to 0 will the value of cos PI/2 get!
If you get the input itself in radians, there also the same criteria applies.
The problem isn't your code. The input you have given is not sufficiently accurate. Calculate the proper value of pi/2, and you will get a sufficiently accurate value.Also, if you want to round off the values you can use#rounded off value=Math.Round(#old value, 4)
My soulution:
double mySin(double x)
{
if (fmod(x, std::numbers::pi) == 0)
return 0;
return sin(fmod(x, std::numbers::pi * 2.0));
}
double myCos(double x) { return mySin(x + std::numbers::pi / 2); }
myCos(std::numbers::pi / 2) == 0 //True
myCos(std::numbers::pi) == -1 //True
myCos(std::numbers::pi * 2) == 1 //True