Getting error message "Not declared in scope" [duplicate] - c++

This question already has answers here:
Why is my HelloWorld function not declared in this scope?
(11 answers)
Closed 8 years ago.
I am working on a C++ project and while compiling, I am receiving the error messages:
error: mean was not declared in this scope
error: standard_dev was not declared in this scope
My code is
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int N(0);
char filename[100];
double m, stdev;
string temp;
double next;
int count = 0;
cout << "Enter name of file: ";
cin >> filename;
ifstream myfile;
myfile.open(filename);
while (myfile >> next)
{
count++;
}
N = count;
double* mydata;
mydata = new double[N];
for (int i = 0; i < N; i++)
{
myfile >> mydata[i];
}
m = mean(mydata, N);
stdev = standard_dev(mydata, m, N);
cout << "The standard deviation is:" << stdev << endl;
myfile.close();
delete[] mydata;
return 0;
}
double mean(double* mydata, double N)
{
double sum(0), m;
for (int i = 0; i < N; i++)
{
sum += mydata[i];
}
m = (sum / (double) N);
return (m);
}
double standard_dev(double* mydata, double m, int N)
{
double* mydata2 = new double[N];
for (int i = 0; i < N; i++)
{
mydata2[i] = pow((mydata[i] - m), 2);
}
double sum(0), S, X;
for (int i = 0; i < N; i++)
{
sum += mydata2[i];
}
X = sum / N;
S = sqrt(X);
return (S);
}

Those functions have not been seen yet when you try to use them; the compiler doesn't know what they are which leads to the error. Either move them before main() or prototype them, such as:
double mean(double * mydata, double N);
double standard_dev(double * mydata, double m, int N);
int main()
{
...
This will give the compiler its expectations for those symbols, so when it sees them in use it knows what to do with them.

Any name must be defined before its using (with rare exceptions relative to template). You use name mean in statement
m = mean(mydata, N);
but name mean is not yet defined.
Place function declarations before main
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
int main()
{
//...
Or before their usage.
int main()
{
double mean(double *mydata, double N);
double standard_dev(double *mydata, double m, int N);
//...
m = mean(mydata, N);
//...

You need to have prototypes for your functions mean() and std_dev() at the top of your file. Otherwise, they don't 'exist' in the main function. Either copy the entire functions to the space above your main function, or make prototypes for them. Good luck!

Related

why is the answer not showing up?

I am doing a code in c++ where I am supposed to be finding the series and I build the function for the series myself yet and I call the function I don't find my answer
here is my code
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
harmonicSeries(n);
}
double harmonicSeries(int n) {
for (int i = 1; i <= n; i++) {
float s;
float sum = 0.0;
s = 1 / n;
sum += s;
return sum;
}
}
I will be thankful for any help
See I have made the changes in your code,this works fine in this finding numbers and adding to get their sum.You should use return outside the function and basically harmonic series is of form 1/n which can be any float number or double number so I use s as double and i has float(which by this).
s=1/i(double=1/float,gets converted to double)
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
cout<<harmonicSeries(n);
}
double harmonicSeries(int n) {
double sum=0.00;
double s;
for (float i = 1; i <= n; i++) {
s = 1 / i;
sum += s;
}
return sum;
}
If you find anything wrong do ask for sure:)

Issue in finding power of a number using user defined functions

MY CODE along with the output
The following code is not working. It has no errors, am doing some mistake in the logic I think. I want to find power of a number using functions. How to make this code work?
The code:
#include<iostream>
using namespace std;
int pow(int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x);
cout<<ans;
return 0;
}
int pow(int)
{
int a=1,i,p,x;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Here is working code:
#include<iostream>
using namespace std;
int pow(int, int);
int main()
{
int x,p,ans;
cout<<"Enter a number";
cin>>x;
cout<<"Enter the power of the number";
cin>>p;
ans=pow(x, p);
cout<<ans;
return 0;
}
int pow(int x, int p)
{
int a=1,i;
for(i=0;i<=p;i++)
{
a=a*x;
}
return a;
}
Ideone
You have to pass the local variables into the function instead of defining new ones with the same name. What you are doing should give you warnings about unused variables (x and p in main) and it also invokes undefined behavior in pow because of ininitialized reads of the variables defined there.
Also your function was wrong. You were just multiplying 1 with a value a bunch of times, which stays 1 forever.
Your function must have the parameters names specified (not just the types):
int pow(int) -> int pow(int b, int p)
You iterate once more than necessary:
for (i = 0; i <= p; i++) -> for (i = 0; i < p; i++)
You can shorten some arithmetic operations:
a=a*x -> a *= x;
The final function:
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
You call it by passing the variables precedently declared:
pow(x, p)
So your final code be like:
#include <iostream>
int pow(int b, int p)
{
int a = 1, i;
for (i = 0; i < p; i++)
a *= b;
return a;
}
int main()
{
int x, p, ans;
std::cin >> x >> p;
ans = pow(x, p);
std::cout << ans << std::endl;
return 0;
}

Using boost multi precision operator instead of a const double one

I have the following code for integration by boost library. Iam tried to change the double operator to cpp_dec_float_50 operator.
#include <iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature = boost::numeric::quadrature;
double polynomial(int k , int n);
struct f
{
vector<double> poly;
double polysum(double x) const {
double s = 0;
double p = 1;
for (int i = 1; i < poly.size(); i++) {
p = p * x;
s += p * poly[i];
}
return s;
}
double operator()(double x)const {
return polysum(x) * log(x) / (1 + x);
}
};
int main()
{
int n = 2;
f fun;
double p = 0;
for (int i = 0; i <= n; i++)
{
p = polynomial(i, n);
fun.poly.push_back(p);
}
double answer, error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun, 0., 1., answer, error_estimate);
cout << "ans" << answer << endl;
return 0;
}
double polynomial(int k , int n)
{
return k;
}
if it changed to :
cpp_dec_float_50 operator()(cpp_dec_float_50 x) const {
and change all the related thing to cpp_dec_float_50, then a list of errors appear see them here
can any one fix that ?
for users who does not have Boost Quadrature library, u can download it from here https://github.com/coolfluid/coolfluid3/tree/master/include/boost/numeric

C++, function doesn't return double [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 7 years ago.
my code doesn't return double value z, instead returns only 1, why?
#include <iostream>
#include <fstream>
using namespace std;
double road(int s, int v, int max )
{
double t;
t = (s/v);
return t;
}
int main()
{
int s[2]={0};
int v[2]={0};
int max;
double z; // result of function
ifstream fd;
fd.open("u1.txt");
fd >> max;
for (int i = 0; i < 2; i++)
{
fd >> s[i] >> v[i];
z = road( s[i], v[i], max );
cout << z << " ";
}
fd.close();
return 0;
}
Try to change your method like
double road(int s, int v, int max )
{
double t;
t = (s/(double)v);
return t;
}
int/int will result in a integer. So you need to cast the numerator or denominator as double.

Get the SUM of array (C++)

I need to calculate numbers from the array.
I have a code written but I don't know how exactly I would need to write that I could get a summation of the numbers in the array.
If You would recommend some good material to learn something like so of, I would be thankful.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int n;
int array_1[20];
const char D[]= "Data.txt";
const char R[]="Rezults.txt";
void to_read ( int &n, int array_1[])
{
ifstream fd(D);
fd>>n;
for (int i=0; i<n; i++)
fd>>array_1[i];
fd.close();
}
int to_sum()
{
int m=0;
for (int i=0; i<n; i++)
m=m+array_1[i];
return m;
}
void to_print(int n, int mas_1[])
{
int sum=0;
ofstream fr(R);
fr<<n<<endl;
for (int i=0; i<n; i++)
fr<<array_1[i]<<" ";
fr<<endl;
sum=to_sum();
fr<<sum<<endl;
fr.close();
}
int main()
{
to_read(n, array_1);
to_sum();
to_print(n, array_1);
return 0;
}
I rewritten your code, removed global variables, changed formatting for easier reading, rename some variables to more explain for what they are and add function prototypes. Hope this will help you a little.
There are still lot of places which should be changed, but i want to keep as close to your original code as possible.
If you have any questions feel free to ask.
#include <iostream>
#include <fstream>
using namespace std;
//functions prototypes (these will be moved to header file if you wanna use this code from another file)
void to_read(int &len, int * array, const char * name); //added name parameter to avoid glogal variables
void to_print(int len, int * array, const char * name);
int to_sum(int len, int * array); //added parameters to avoid global variables
int main()
{
int lenght = 20; //moved to here, try to avoid global variables
int array_1[lenght]; //preconfigured size depend on variable
const char D[] = "Data.txt";
const char R[] = "Rezults.txt";
to_read(lenght, array_1, D);
//to_sum(lenght, array_1); //not needed here, not storing/processing result
to_print(lenght, array_1, R);
return 0;
}
void to_read(int &len, int * array, const char *name)
{
int lenght;
ifstream fd(name); //you should check if opening was successful
fd >> lenght; //you should check if reading was successful
if (lenght < len) len = lenght; //to prevent overflow of array, read max 20
for (int i=0; i<len; i++){
fd >> array[i];
}
fd.close();
}
int to_sum(int len, int * array)
{
int sum=0;
for (int i=0; i<len; i++){
sum += array[i]; //changed sum = sum + value; to sum += value; its same but this is more often used
}
return sum;
}
void to_print(int len, int * array, const char *name)
{
int sum = to_sum(len, array);
ofstream fr(name);
fr << len << endl;
for (int i=0; i<len; i++){
fr << array[i] << " ";
}
fr << endl;
fr << sum << endl;
fr.close();
}