Trying to create a cos(x) calculating function [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am coding a cos(x) function in C++ but the result I am getting is infinity, except that it should be -1/3.
Here is my code:
#include <iostream>
#include <math.h>
using namespace std;
The factorial function:
int factorial(unsigned int n)
{
unsigned long factorial = 1;
for(int o=1;o<=n;o++)
{
factorial *= o;
}
}
int main()
{
double x;
double answre;
double input;
cin>>input;
for(int i=0;i<2;i++)
{
double y=2*(i)+2;
I declared y here instead of implementing it's value directly, since I thought it is dividing by factorial instantaneously and that is the reason for all the parentheses as well.
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x;
}
answere=1+x;
cout<<answere<<endl;
return 0;
}

You are not returning anything in factorial

x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x; // ???
If you want to add to x, just write x += .... What you did was assign one (!) summand to x and then set x to itself. You are never adding anything.

Related

'cin' was not declared in this scope [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I run my code and it gives an error 'cin' was not declared in this scope
My code
#include <bits/stdc++.h>
int m,a,b,c,d,e,f;
main()
{
cin>>m>>a>>b>>c>>d>>e>>f;
double g=a%b;
double h=c%d;
double k=e%f;
if (g<h && g<k){
int i=g;
}
if (h<g && h<k) {
int i=h;
}
if (k<g && k<h) {
int i=g;
}
double s=i*m;
cout<<s;
}
`
i think i wrote it right,help me
Use C++ standard library includes
#include <iostream>
then
std::cin
and
std::cout
If you do this, then you are writing portable and easy to read C++.
Even better, check that the data have been successfully read:
if (std::cin >> m >> a >> b>> c>> d>> e >> f){
// success - go on from here
}

Why can't the global variable delta be used in method of class? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm a beginner of C++. In this sample, I want to use the global variable delta in method update_v() of class neuron. But it can't be used. Could you tell me why if you know?
#include<iostream>
#include<cmath>
using namespace std;
unsigned long nextt=1;
long clock=0;
long delta=0;
class neuron{
public:
double a,b,c,d;
double current_v,current_u;
double previous_v,previous_u;
double accumulate;
void update_v(){
current_v=previous_v+delta(0.04*pow(previous_v,2)+previous_v)+accumulate;
}
void update_u(){
current_u=previous_u+delta*a*(b*previous_v-previous_u);
}
};
In void update_v(){, you do delta(0.04*pow(previous_v,2)+previous_v), so it makes the compiler thinks that you are calling a function named delta. But there's none, so it throws a error.
It looks like you forget to use the * operator:
void update_v(){
current_v = previous_v + delta * (0.04 * pow(previous_v,2) + previous_v) + accumulate;
}

why error showing: too few arguments to function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Even on passing parameters to the function log, error showing too few arguments to function.
#include <iostream>
using namespace std;
int log(int n, int x){
return (n>1) ? 1 + log(n/x) : 0;
}
int main() {
int n,x;
cin>> n>> x;
cout<< log(n,x);
}
I expect the output of log10(1000) to be 3, but few arguments error is shown.
You forgot the second argument to your log function in the recursive step.
return (n>1) ? 1 + log(n/x, x) : 0;
By the way, you should name your variables something descriptive. For instance instead of using n perhaps use base.

Why this simple C++ program is giving unexpected result? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I made a simple C++ program. I expect a floating point output of 5/9, but is is zero. Can someone comment, why output is unexpected i.e. zero?
#include<iostream>
using namespace std;
void fun(double *ptr);
int main (int argc, char* argv[])
{
double a;
fun(&a);
cout<<a<<endl; // why not floating point 5/9??
}
void fun (double *ptr)
{
*ptr=(5/9);
}
5/9 will result in 0 because it is int division.
You would need to do
void fun (double *ptr)
{
*ptr = (5.0 / 9.0);
}
You need to use the correct type literals:
5 // int
5.0 // double
5.0f // float
5u // unsigned int
5l // long
5ul // unsigned long

Confusion about passing arrays [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Can someone help me with my program? I need to pass the three arrays into the function called calc_volts and then calculated the volts and then display the values. I keep getting errors that say "unreferenced local variable" or "undeclared idebtufier" for the variables; i, j, k, and volts.
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
double calc_volts(double, double, double, int);
int main()
{
const int max = 10;
int i; double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
int j; double volts[max];
int k; double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};
}
double calc_volts(double current[],double volts[], double resistance[], int max)
{
for (j = 0, j<max, j++)
volts[j] = current[i]*resistance[k];
return volts[j];
}
You have many problems:
Your function declaration is wrong:
double calc_volts(double, double, double, int);
It should be:
double calc_volts(double[], double[], double[], int);
You must invoke the function in order to use it:
int main()
{
const int max = 10;
double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};
double volts[max];
double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};
calc_volts(current, volts, resistance, max); // call the function to execute it
}
The variables i, j and k do not exist inside calc_volts because they were declared inside main. Variables declared inside a function can only be used inside that function.
To fix the problem just put the declarations inside calc_volts.