I am writing a pretty simple piece of code for school homework, but assigning one of the functions to a variable throws an undefined refence error.
Here is the code:
#include <iostream>
#include <fstream>
using namespace std;
void skaitimas (double KKa[], int KKi [],int & n); // KKa - kainos, KKi - kiekiai
void pilnoskainos (double KKa[], int KKi[], double PK[], int & n); // PK - pilnos knygu kainos
double suma (double PK[], int & n);
int kiekisfunkcija (int KKi[] , int & n);
void rasymas (double KKa[], int KKi [], double PK[], double, int, int & n); // sumaats - visu knygu kaina, kiekisats - kiek knygu daugiau negu 5
int main()
{
cout << "Programos Pradzia" << endl;
int x;
ifstream fn ("Duomenys1.txt");
fn>>x;
fn.close();
int KKi[x], kiekisats, n;
double KKa[x], PK[x], sumaats;
skaitimas (KKa, KKi, n);
pilnoskainos (KKa, KKi, PK, n);
sumaats = suma(PK, n);
kiekisats = kiekisfunkcija(KKi, n);
rasymas (KKa, KKi, PK, sumaats, kiekisats, n);
return 0;
}
void skaitimas (double KKa[], int KKi [], int & n)
{
ifstream fn ("Duomenys1.txt");
int i;
fn>>n;
for (i=0;i<n;i++)
{
fn>>KKa[i]>>KKi[i];
}
fn.close();
}
void pilnoskainos (double KKa[], int KKi[], double PK[], int & n)
{
int i;
for (i=0;i<n;i++)
{
PK[i]=KKa[i]*KKi[i];
}
}
double suma (double PK[], int & n)
{
double sumaats;
int i;
for (i=0;i<n;i++)
{
sumaats = sumaats + PK[i];
}
return sumaats;
}
int kiekis (double PK[], int & n)
{
int daugiaunegu5 = 0, i;
for (i=0;i<n;i++)
{
if (PK[i]>5) daugiaunegu5++;
}
return daugiaunegu5;
}
void rasymas (double KKa[], int KKi [], double PK[], double sumaats, int kiekisats, int & n)
{
ofstream fr ("Rezultatai1.txt");
int i;
for (i=0;i<n;i++)
{
fr<<KKa[i]<<" "<<KKi[i]<<" "<<PK[i]<<endl;
}
fr<<sumaats;
fr<<kiekisats;
}
And here is the error:
|23|undefined reference to `kiekisfunkcija(int*, int&)'|
Can anyone please help me
The function is declared
int kiekisfunkcija (int KKi[] , int & n);
but is not defined. You forgot to define the function.
Related
I need help... appropriate questions have been asked in the comments. The programs has zero compiler errors and warnings!! I have concerns with calling a member function from another member function using function pointers. (To be precise, setMatrixto() is trying to call setElement() function using function pointer)
Plus somehow the "hello there" is not being printed to the console. I was expecting it to show up as output.Maybe the setMatrixto() is not getting called at all!!
Header File definition
#ifndef MATRIXOPERATIONS_H
#define MATRIXOPERATIONS_H
class MatrixOperations;
typedef int (MatrixOperations::*INTFUNC)(int,int);
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void displayMatrixOf(INTFUNC f);
void setMatrixTo(VOIDFUNC f);
int getElement(INTFUNC from, int i, int j);
void setElement(VOIDFUNC to,int i ,int j, int value);
int fromDiagonalArray(int i, int j);
void toDiagonalArray(int i, int j, int value);
protected:
private:
int size;
int* a;
};
#endif // MATRIXOPERATIONS_H
CPP Implementation File
#include "MatrixOperations.h"
#include <iostream>
using namespace std;
MatrixOperations::MatrixOperations()
{
//ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{
//ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
///////////////////FUCNTION POINTER SECTION///////////////////////////////////
int MatrixOperations::getElement(INTFUNC from, int i, int j)
{
return (this->*from)(i,j);
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
int MatrixOperations::fromDiagonalArray(int i, int j)
{
if(i==j)
{
return a[i];
}
else
{
return 0;
}
}
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::displayMatrixOf(INTFUNC f)
{
for(int i{0}; i < size; i++)
{
for(int j{0}; j < size; j++)
{
cout << getElement(f,i,j) << "\t"; //is this the correct way to send the function pointer?
}
cout << endl;
}
}
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!"; //not getting this output.. whats wrong??
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value); //is this the correct way to send the function pointer?
}
}
///////////////////////////////////////////////////////////////////////////////
Main File
#include <iostream>
#include "MatrixOperations.h"
typedef MatrixOperations MATRIX;
using namespace std;
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray); //was expecting a "Hello there!" but i am not getting that output either
return 0;
}
EDIT2: I added all the class definitions and main function in one single file. SURPRISINGLY!! this works . I am confused??!!!
#include <iostream>
using namespace std;
class MatrixOperations;
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
typedef MatrixOperations MATRIX;
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void setMatrixTo(VOIDFUNC f);
void setElement(VOIDFUNC to,int i ,int j, int value);
void toDiagonalArray(int i, int j, int value);
private:
int size;
int* a;
};
MatrixOperations::MatrixOperations()
{ //ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{ //ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!" << endl;
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value);
}
}
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray);
return 0;
}
There is nothing wrong with the code in both cases. Its just my debugger was not running in admin mode. I got error code 740. So I launched my IDE in admin mode and voila it worked.
I have the following problem with my search function.
It expects 3 parameters, and one of them is something like const rational_t* v. I want to pass a vector through that parameter but it doesnt seems to work..
Code:
#include <iostream>
#include <cmath>
#include <vector>
#include "rational_t.hpp"
using namespace std;
bool search(const rational_t* v, const int n, const rational_t& x)
{
for(int i = 0; i < n; i++) {
if(v[i].value() == x.value()) {
return true;
} else {
return false;
}
}
};
int main()
{
rational_t a(1, 2), b(3), c, d(1, 2);
vector<rational_t> v;
v.push_back(a);
v.push_back(b);
v.push_back(c);
cout << "a.value()= " << a.value() << endl;
cout << "b.value()= " << b.value() << endl;
cout << "c.value()= " << c.value() << endl;
cout << search(v, v.size(), d); // Problem here
return 0;
}
I´ve also tried cout << search(v&, v.size(), d); with the reference &.
Any ideas? Thank You.
The class :
#pragma once
#include <iostream>
#include <cassert>
#include <cmath>
#define EPSILON 1e-6
using namespace std;
class rational_t
{
int num_, den_;
public:
rational_t(const int = 0, const int = 1);
~rational_t() {}
int get_num() const
{
return num_;
}
int get_den() const
{
return den_;
}
void set_num(const int n)
{
num_ = n;
}
void set_den(const int d)
{
assert(d != 0), den_ = d;
}
double value(void) const;
rational_t opposite(void) const;
rational_t reciprocal(void) const;
bool equal(const rational_t &r, const double precision = EPSILON) const;
bool greater(const rational_t &r, const double precision = EPSILON)
const;
bool less(const rational_t &r, const double precision = EPSILON) const;
bool cero_equal(const double precision) const;
void write(ostream &os = cout) const;
void read(istream &is = cin);
};
The first argument of search should be a rational_t* but you're passing a vector<rational_t>.
You want
search(v.data(), v.size(), d)
instead of
search(v, v.size(), d)
But I'd write this like this which is cleaner IMO:
bool search(vector<rational_t> & v, const rational_t& x)
{
for (int i = 0; i < v.size(); i++) {
if (v[i].value() == x.value()) {
return true;
}
else {
return false;
}
}
}
...
cout << search(v, d);
after searching i had found amazing code for integration by
quadrature boost library.
rather than
log(x)/(1+x)
want to integrate
(poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x). But, i do not
know how to insert the vector
poly
to
struct f
or even how to call these operators from main function. The code :
#include<iostream>
#include<boost/math/constnats/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
struct f
{
double operator()(double x) const {
return (log(x)/(1+x); }
};
int main()
{
vector<cpp_dec_float_50> poly(0);
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(f(),0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
cpp_dec_float_50 polynomial(int k ,int n)
{
.
.
.
}
Also, when changing the double operator, to cpp_dec_float_50 operator in
struct f
many problems arise. and the later type is necessary in my project. Any one can fix that ?
EDIT
i tried this, but i do sth wrong
#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
{ const cpp_dec_float_50 s=0;
vector<cpp_dec_float_50> poly;
cpp_dec_float_50 sum()const{
for(int i=0;i<=poly.size();i++)
s+=poly[i];
return s
}
double operator()(double x) const {
return
s*log(x)/(1+x); }
};
int main()
{
int n=2;
f fun;
cpp_dec_float_50 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;
}
Edit
when using Patstew suggestion
Two errors occur
Try something along the lines of:
struct f
{
vector<cpp_dec_float_50> poly;
double operator()(double x) const {
return (poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x); }
};
int main()
{
f fun;
cpp_dec_float_50 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;
}
EDIT: RE you own answer, you never call sum (and s is const so you couldn't change it if you did) so s is always 0 and you will always get 0 as your answer. Also you are iterating all the way up to poly.size() in sum(), but poly[poly.size()-1] is the last element. I think you really want your sum function to calculate a polynomial? Try this:
#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) {
double s = poly[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) {
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;
}
My code is not working because I have a problem with the call of my two functions when I use a structure. I think I didn't call it correctly but I'm not sure if the problem is here or in the definition itself.
Here is my code:
#include <stdio.h>
#define NUM 3
struct student{
char name[20];
int kor;
int math;
int eng;
int sum;
double avg;
double avg2;
double k, m, e;
};
void average(student* st)
{
int i, sum = 0;
for(i=0;i<NUM;i++) {
st[i].sum= st[i].kor + st[i].math + st[i].eng;
st[i].avg= st[i].sum / NUM;
}
}
void average2(student* st)
{
int i, sum = 0;
double K, M, E;
for(i=0;i<NUM;i++) {
K+= st[i].kor;
M+= st[i].math;
E+= st[i].eng;
}
}
int main(void)
{
student stu[NUM]={{"Tom"},{"Jane"},{"Eddy"}} ;
int i;
int max;
double K, M, E;
printf("Input scores.\n");
for(i=0;i<NUM;i++)
{ printf("\n<%s>\n",stu[i].name);
printf("Korean:");
scanf("%d",&stu[i].kor);
printf("Math:");
scanf("%d",&stu[i].math);
printf("English:");
scanf("%d",&stu[i].eng);
}
printf("\nName\tKorean\tMath\tEnglish\tSum\tAverage\n");
average(stu);
for(i=0;i<NUM;i++)
printf("%s\t%d\t%d\t%d\t%d\t%.2f\n",stu[i].name,stu[i].kor,stu[i].math,stu[i].eng,stu[i].sum,stu[i].avg);
average2(stu);
printf("Average %.2lf\t%.2lf%.2lf\n", k/3, m/3, e/3);
}
Thank you in advance for your answers, Coco
for loops should have { and } to enclose more than one line in c++.
for(i=0;i<NUM;i++)
{
st[i].sum= st[i].kor + st[i].math + st[i].eng;
st[i].avg= st[i].sum / NUM;
}
Also in your function average2 it is not clear what you are exactly doing.
You are declaring same variable in main and average2 double K, M, E; so the function will take local variable only.
For your second printf here is the logic.,
for(i=0;i<NUM;i++) {
K+= st[i].kor;
M+= st[i].math;
E+= st[i].eng;
}
printf("Average %.2lf\t%.2lf%.2lf\n", K/3, M/3, E/3);
I am getting the inconsistent error while i am trying to execute the following program.can anyone tell me where i am doing wrong...
int a,b=0;
int getvalue(int c)
{
int n=0;
a=c;
if(n<c)
n=a+b;
return n;
}
int newvalue(int c)
{
int n=0;
int a=c;
if(n<getvalue(c))
n=a+b;
return n;
}
voidmain()
{
int j=1;
int b=newvalue(j);
cout<<a+b+j<<end1;
return 0;
}
Try this (end1 should be endl) and I fixed the main signature.
#include <iostream>
int a,b=0;
int getvalue(int c)
{
int n=0;
a=c;
if(n<c)
n=a+b;
return n;
}
int newvalue(int c)
{
int n=0;
int a=c;
if(n<getvalue(c))
n=a+b;
return n;
}
int main()
{
int j=1;
int b=newvalue(j);
std::cout<<a+b+j<<std::endl;
return 0;
}
Without testing I suspect that a space between void and main could help you. voidmain() -> void main(). However if you already can compile the code my advice won't help.