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.
Related
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;
}
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.
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 5 years ago.
Improve this question
I read on another stackoverflow post (variable length array error when passing array using template deduction) that the following should be possible:
#include <iostream>
int input() {
int a;
std::cin>>a;
return a;
}
int main()
{
const int b = input();
int sum[b];
std::begin(sum);
}
Except that it doesn't seem to work, I still get an similar error.
In function 'int main()':
16:17: error: no matching function for call to 'begin(int [b])'
16:17: note: candidates are:
Followed by information on possible templates it could fit.
You can use std::begin(sum) only when sum is regular array, not when it is a variable length array.
The following is OK.
const int b = 10;
int sum[b];
std::begin(sum);
In your case, b is not known at compile time. For arrays whose length are not known at compile time, it's better to use std::vector instead of relying on a compiler specific extension. The following is OK.
const int b = input(); // You can use int b, i.e. without the const, also.
std::vector<int> sum(b);
std::begin(sum);
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
This is a code that takes a series of 3 numbers in a number pattern and figures out the difference between them. everything seems to be right but my compiler keeps telling me I need an initializer before int i? sorry, I'm new to C++ so I'm sure my code is horrible.
using namespace std;
void add(int a, int b, int c)
int i;
for (a+i!=b;b+i!=c)
{i=0; i<100; i++;}
else {cout i;}
};
int main()
{
int x, y, z;
cin>>x;
cin>>y;
cin>>z;
add(x, y, z);
}
Many things, first you're missing a curly braze after your add function.
Also, you have one extra ; in your for declaration.
Also, after your function add there shouldn't be a ;