what is wrong with the function parameter? - c++

#include <iostream>
using namespace std;
template<class Type>
void Knapsack(Type *v,int *w,int c,int n,Type **m)
{
int i,j;
int jMax=max(w[n]-1,c);
for(j=0;j<=jMax;j++)
m[n][j]=0;
for(j=w[n];j<=c;j++)
m[n][j]=v[n];
for(i=n-1;i>1;i--)
{
for(j=0;j<=w[i]-1;j++)
m[i][j]=m[i+1][j];
for(j=w[i];j<=c;j++)
{
m[i][j]=max(m[i+1][j],m[i+1][j-w[i]]+v[i]);
}
}
m[1][c]=m[2][c];
if(c>=w[1])
m[1][c]=max(m[2][c],m[1][c-w[1]]+v[1]);
}
template <class Type>
void TrackBack(Type **m,int *w,int c,int n,int *x){
for(int i=1;i<=n;i++)
{
if(m[i][c]==m[i+1][c])
x[i]=0;
else
x[i]=1;
}
}
int main()
{
int m[101][101]={0};
int x[101];
int n=5;
int c=10;
int w[5]={2,2,6,5,4};
int v[5]={6,3,5,4,6};
Knapsack(v,w,c,n,m);
return 0;
}
I an writing the algorithm of 01 Knapsack problem.
my Xcode says "No matching function for call to 'Knapsack' "
I am stumbled by the red alarm.
I'm confused for passing arguments.
Is there anyone can help me? Thanks a lot:)

This is not a valid conversion:
int m[101][101]
...
Knapsack(v,w,c,n,m);
// ^-- expects a Type **m
m can decay to type "pointer to array of 101 ints", but no further.

At least type of argument
int m[101][101]={0};
is not equivalent to T ** where T is int.
When this array is passed by value as an argument it is implicitly converted to pointer to its first element and has type int ( * )[101]
Take into account that in any case this function is invalid. For example argument for second parameter w has 5 elements.
int w[5]={2,2,6,5,4};
The valid range of indices for it is [0,4]. n in the function call is equal to 5.
So this statement
int jMax=max(w[n]-1,c);
has undefined behaviour because you are using inadmissible index equal to 5.
template<class Type>
void Knapsack(Type *v,int *w,int c,int n,Type **m)
{
int i,j;
int jMax=max(w[n]-1,c);
//...

You a little bit wrong with definition of functions
template < class Type>
void Knapsack(Type *v,int *w,int c,int n,Type m[][101])
template < class Type>
void TrackBack(Type m[][101],int *w,int c,int n,int *x)

Related

Pointing to an element of an array

Below is the cluttered version of my code that shows only the basic structure.
I'm just trying to use a pointer to an element of an array in a function but
I constantly get
C:\Users\whale\Desktop\20_Pay2.cpp:4:45: error: expected ',' or '...' before 'money'
but I still have no clue what I'm missing. Any help is appreciated!
#include<stdio.h>
#include<stdlib.h>
void pay_amount (int *dollars, int k, int *money[k]);
int main(void)
{
int dollars=180, i=1, a[4]={20,10,5,1};
pay_amount (&dollars, i, &a[i] );
return 0;
}
void pay_amount (int *dollars, int k, int *money[k])
{
printf("functions");
}
The problem is that the parameter list you're using (int k, int *money[k]) is not valid C++ syntax. Unfortunately g++ produces a less than helpful message for this.
The good news is that you don't want this anyway: It would declare money to be an array of k pointers to int. What you actually want is just a pointer to int:
void pay_amount(int *dollars, int k, int *money);
a is an array of int, a[i] is a single int, so &a[i] is a pointer to an int, int *.
Here :
pay_amount (&dollars, i, &a[i] );
The third parameter you pass is a pointer to a integer.
But in your function declaration, you have that :
void pay_amount (int *dollars, int k, int *money[k]);
int *money[k] as a function parameter does not seems correct.
You may want to do that :
void pay_amount (int *dollars, int k, int *money);
Here, money is a pointer to an integer

c++ classes not working for me

I have written this program but it doesn't work. It gives an error that x and y was not declared and expected primary expression before int on line 17.
#include<iostream>
using namespace std;
class shapes
{
int width, height;
public:
int getvalue();
void decideshape(int l, int b);
};
main()
{
cout<<"to find what type of shape you have input the measurements"<<endl;
shapes toy;
toy.getvalue();
toy.decideshape();
}
int shapes::getvalue()
{
int l, b;
cout<<"length = ";
cin>>l;
cout<<"breath = ";
cin>>b;
}
void shapes::decideshape(x, y)
{
if(x==y)
cout<<"This is square"<<endl;
else
cout<<"This is rectangle"<<endl;
}
how should i return 2 values from function getvalue
Arguments are required to have types in C++. Write your definition of shapes::decideshape as
void shapes::decideshape(int x, int y)
You don't return a value from shapes::getvalue.
You pass too few (actually none) parameters to shapes::decideshape. Two ints are expected to be supplied.
You need to tell the compiler what a function returns explicitly. Add the int return value to main.
You are missing the type of x and y in the parameter list:
void shapes::decideshape(int x, int y)

How to call function pointer in STL

I am curious about how to call function pointer in a map structure. Here is the details:
#include<iostream>
#include<map>
#include<vector>
#include<string.h>
using namespace std;
class FuncP;
typedef int(FuncP::*func) (int, int);
class FuncP
{
public:
map<int, func> fmap;
map<int, string> fstring;
public:
FuncP(){}
void initial();
int max(int x, int y);
int min(int x, int y);
int call(int op, int x, int y)
{
return (this->*fmap[op])(x, y);
}
};
void FuncP::initial()
{
fmap[0] = &FuncP::max;
fmap[1] = &FuncP::min;
fstring[0] = "fdsfaf";
}
int FuncP::min(int x, int y)
{
return (x<y)?x:y;
}
int FuncP::max(int x, int y)
{
return (x<y)?y:x;
}
int main()
{
func h = &FuncP::max;
FuncP *handle = new FuncP();
handle->initial();
cout<< handle->call(0, 1, 4); //1
cout<< (handle->FuncP::*fmap)[0](1,5); //2
return 0;
}
For the number 2 (handle->FuncP::*fmap)0; The compiler gives a error:
‘fmap’ was not declared in this scope
I am not sure why it happened. What the difference of the number 1 and 2 call methods?
As commented by Piotr, a correct way would be
(handle->*(handle->fmap[0]))(1, 5);
Explanation:
handle->fmap[0] gives you the function pointer. To call it, you need to dereference it, giving *(handle->fmap[0]) (parentheses optional)
and call it on the respecting object (handle), leaving us with the expression above.
This is essentially the same as your above statement (this->*fmap[op])(x, y) except of handle->fmap[0]instead offmap[op].

Template size multidimensional arrays C++

I would like to use template to specify sizes in functions. Let me give you an example.
I can do this:
int sub (int a[2][2]) {
}
int main () {
int a[2][2];
sub(a);
}
I can do this:
template<int size2>
int sub (int a[][size2]) {
}
int main () {
int a[2][2];
sub(a);
}
But what I would like is this:
template<int size1, int size2>
int sub (int a[size1][size2]) {
}
int main () {
int a[2][2];
sub(a);
}
How could I?
The better option is probably to use std::array, if you have C++11 support, or std::vector, if not. If you really want to use C-style arrays, you can pass them by reference, using the following syntax.
template<size_t size1, size_t size2>
int sub (int (&a)[size1][size2]) {
// ...
}
int main() {
int a[2][2];
sub(a);
}
As Brian Bi already wrote, the best way to go is to use std::array.
But to answer your question, it is halfway possible to do what you want.
When you use a C-style 2D array as an argument to a function, it decays to a pointer to an array of the second dimension. For example int a[6][6] decays to int (*a)[6], so the first dimension is hidden and means nothing to the compiler. The second dimension however, can be used as template parameters and can be deduced.
For example:
template<int S>
void func(int (*a)[S])
{
cout << S << endl;
}
and then:
int a[66][67];
func(a);
prints "67".
Here is another way (sub<2,2>:
template<int size1, int size2>
int sub (int a[size1][size2]) {
}
void main(int argc, char **argv)
{
int a[2][2];
sub<2,2>(a);
}

Create a function pointer which takes a function pointer as an argument

How to create a function pointer which takes a function pointer as an argument (c++)???
i have this code
#include <iostream>
using namespace std;
int kvadrat (int a)
{
return a*a;
}
int kub (int a)
{
return a*a*a;
}
void centralna (int a, int (*pokfunk) (int))
{
int rezultat=(*pokfunk) (a);
cout<<rezultat<<endl;
}
void main ()
{
int a;
cout<<"unesite broj"<<endl;
cin>>a;
int (*pokfunk) (int) = 0;
if (a<10)
pokfunk=&kub;
if (a>=10)
pokfunk=&kvadrat;
void (*cent) (int, int*)=&centralna; // here i have to create a pointer to the function "centralna" and call the function by its pointer
system ("pause");
}
you will find it easier to typedef function pointers.
typedef int (*PokFunc)(int);
typedef void (*CentralnaFunc)(int, PokFunc);
...
CentralnaFunc cf = &centralna;
You need to use the function pointer type as the type of the parameter:
void (*cent) (int, int (*)(int)) = &centralna
void (*cent)(int,int (*) (int))=&centralna;