Can someone tell me what is wrong with this code?!
visual studio tells the operand of * must be a pointer...
(in line that we call operation)...
can someone tell how exactly declaring an array of pointer to functions is?
I'm really confused.
#include<iostream>
#include<conio.h>
using namespace std;
int power(int x)
{
return(x*x);
}
int factorial(int x)
{
int fact=1;
while(x!=0)
fact*=x--;
return fact;
}
int multiply(int x)
{
return(x*2);
}
int log(int x)
{
int result=1;
while(x/2)
result++;
return result;
}
//The global array of pointer to functions
int(*choice_array[])(int)={power,factorial,multiply,log};
int operation(int x,int(*functocall)(int))
{
int res;
res=(*functocall)(x);
return res;
}
int main()
{
int choice,number;
cout<<"Please enter your choice : ";
cin>>choice;
cout<<"\nPlease enter your number : ";
cin>>number;
cout<<"\nThe result is :"<<operation(number,(*choice_array[choice](number)));
}
This call
operation(number, (*choice_array[choice](number)))
is invalid.
You have to supply a pointer to a function as second argument. Either write
operation(number, choice_array[choice] )
or
operation(number, *choice_array[choice] )
The problem is that (*choice_array[choice](number)) isn't a function itself but a result of function call.
Did you mean (*choice_array[choice])?
operation takes a function as argument, but (*choice_array[choice](number)) is an int, cuz it's applying choice-array[choice] to number
just do operation(number, choice_array[choice])
EDIT : don't want to say something wrong, but it seems to me that
*(choice_array[choice])
(choice_array[choice])
are the same, (meaning pointer to the function IS (can be used as a call to) the function, and you cant "dereference" it)
Related
Below is the code, when I am executing. No output in the console. Not sure why.
I am testing one example of function overloading in C++.
Function Overloading. Created 2 functions of the same name with different arguments. When tried to pass integer value in main during the function call. It worked fine. But when passed as float number, neither error nor output is coming.
#include <iostream>
using namespace std;
int max(int x,int y)
{
cout<<"Entered in max int"<<endl;
if(x>=y)
return x;
else
return y;
}
float max(float x,float y)
{
cout<<"Entered in max float"<<endl;
if(x>=y)
return x;
else
return y;
}
int main()
{
float x;
x=max(5.9,6.7);
}
First you are not outputting the result of the function and 2nd your function is not being called because you are using using namespace stdso when you call max std::max is being called.
To call your version ofmax you have to access the global scope. Like this ::max(x, y).
there is some implicit conversion (float to int), when call float type max function.So you need specify that.
max(5.9f, 6.7f)
I would like to call a recursive function only once. I tried it as we do for a static variable but it is throwing an error. Can anybody suggest a solution?
Say I have function recur(int j):
void recur(int x){
if(x==0)
return;
//want x to be xth fibnocci number,but only at initialization.
x=fib(x);
cout<<x<<" ";
recur(x-1);
}
The output of recur(5) should be {5,4,3,2,1} and not {5,3,1}. I want to do it inside the function only.
Any time you want to have a variable start a certain way in function (whether it be recursive or not), you should handle this through the function arguments. The function is already set up to have an initialization stage.
Example:
void recur(int x){
if(x==0)
return;
cout<<x<<" ";
recur(x-1);
}
int main() {
recur(fib(x));
return 0;
}
#include<iostream.h>
#include<conio.h>
void square(double);
void main()
{
clrscr();
double x;
x=123.456;
cout<<"\nThe value of i before calling square(), is :"<<x;
cout<<endl;
square(&x);
cout<<"The value of i after calling square(), is :"<<x;
cout<<endl;
getche();
}
void square(double* x)
{
*x=*x**x;
}
It is not working, why?
It is also not compiling.
I am calling a function which is called by reference but there is an error which is
can not convert double * to double
Your declaration and definition of square differ in their parameter types:
void square(double);
// ...
void square(double* x) { /* ... */ }
square(&x) tries to call void square(double) with a double* - that explains your error.
You're also using an ancient C++ compiler - iostream.h and conio.h are non-standard and outdated.
Your use of pointers is also non-idiomatic - square should take double by value or by const lvalue reference.
Following code for max-heap implementation
#include<iostream>
#include<math.h>
using namespace std;
#define maxn 1000
int x[maxn];
int parent(int i){
return int(i/2);
}
int left(int i){
return 2*i;
}
int right(int i){
return 2*i+1;
}
void max_heap(int x[],int i,int size){
int largest;
int l=left(i);
int r=right(i);
if (l<=size && x[l]>x[i]){
largest=l;
}
else
{
largest=i;
}
if (r<=size && x[r]>x[largest]){
largest=r;
}
if (largest!=i) { int s=x[i];x[i]=x[largest];x[largest]=s;}
max_heap(x,largest,size);
}
int main(){
x[1]=16;
x[2]=4;
x[3]=10;
x[4]=14;
x[5]=7;
x[6]=9;
x[7]=3;
x[8]=2;
x[9]=8;
x[10]=1;
int size=10;
max_heap(x,2,size);
for (int i=1;i<=10;i++)
cout<<x[i]<<" ";
return 0;
}
When I run it, it writes such kind of warning:
1>c:\users\datuashvili\documents\visual studio 2010\projects\heap_property\heap_property\heap_property.cpp(36): warning C4717: 'max_heap' : recursive on all control paths, function will cause runtime stack overflow
Please tell me what is wrong?
The message tells you exactly what's wrong. You haven't implemented any checks to stop the recursion. One smart compiler.
max_heap function doesn't have base case, i.e., a return statement. You are just recursively calling the function but never saying when to break another successive call to the max_heap.
Also, in your example you are just calling the function with out satisfying any condition. Usually recursion is done or not done when a case is satisfied.
please tell me what is wrong?
Another problem that I see is that the size of your array x is 10. But the indices that you are using to set values are 1-10.
Put
max_heap(x,largest,size);
inside last check, like this:
if (largest!=i)
{
int s=x[i];
x[i]=x[largest];
x[largest]=s;
max_heap(x,largest,size);
}
and you're done!
There are many other problems with your code, but to answer your specific question, above change would do!
Here I have written some code to get the square of a number from a function, but the return statement is not working as desired by me, it is giving me the same number which I have entered, I want to know the reason behind this, please if any one can explain this to me...
#include<iostream>
#include<conio.h>
using namespace std;
int square(int &i);
int main()
{
cout<<"enter the number whose square you want to find";
int a;
cin>>a;
square(a);
cout<<"the square of the number is"<<a;
_getch();
return 0;
}
int square(int &i)
{
return i*i;
}
You're ignoring the returned value. You should store it as:
int value = square(a);
cout<<"the square of the number is "<< value;
Also, as the type is just integral type, passing by reference doesn't give you much advantage. I would suggest to use pass by value for its readability sake:
int square(int i)
{
return i*i;
}
--
Or in case if you're experimeting with reference, and trying to learn it, then in that case I would say that you've to store the result of product in the argument itself, as:
int square(int &i)
{
i = i * i; //this updates i here, and at the call site as well
return i;
}
Or simply do this:
int square(int &i)
{
return i = i*i; //multiply, update, and return - all in one statement!
}
You do not obtain the result.
Your line should be:
a = square(a);
to fetch the result from the function.
The other possibility would be to write in the function
int square(int &i)
{
i = i * i;
return i;
}
The latter will alter the variable you passed to the function which justifies passing a reference.
To make it clear you want to alter the variable do something like:
void square(int &i)
{
i = i * i;
}
You see there is no return involved but it will alter the variables value.
You have a choice:
Modify the parameter you pass in, or
Return a value and assign it to something in the calling scope.
What you are doing in square is the second option. You seem to want the first.
If what you really want is to modify the passed-in value, then what you need is this:
void square(int &i)
{
i = i*i;
}
Either do it this way:
a = Square (a) ; // in main()
...
int Square (int i) // Pass by value -- doesn't change a in main
{
return i * i ;
}
or do it this way:
Square (a) ; // in main()
...
void Square (int& i) // Pass by reference -- changes a in main
{
i = i * i ; // No need for a return value
}
Make sure you understand the difference before you program anything else!
Judging by your comments on the answers, you've misunderstood what passing by reference does OR you've misunderstood return.
I'm assuming you're thinking that the variable i will be updated in your program. However, this is not the case. If you did something like...
i = i*i;
then yes, you would be correct. However, you did not assign any value to i, you simply multiplied it by itself and returned the result. Also, if you truly wanted to make this work based on a reference, there would be no need to return anything, as the variable would be updated via the reference.