Skip to content

Instantly share code, notes, and snippets.

@ravikiran0606
Last active July 31, 2016 08:44
Show Gist options
  • Save ravikiran0606/bd42ddb24dad5a06050e660526e84852 to your computer and use it in GitHub Desktop.
Save ravikiran0606/bd42ddb24dad5a06050e660526e84852 to your computer and use it in GitHub Desktop.
#include<iostream>
#define maxi 100
using namespace std;
int flag;
float add(float a,float b){
return (a+b);
}
float sub(float a,float b){
return (a-b);
}
float mul(float a,float b){
return (a*b);
}
float div(float a,float b){
if(b==0){
cout<<"Not Possible";
flag=1;
return -1;
}
return (a/b);
}
float calc(float a,float b,float(*fnptr)(float a,float b)){
return fnptr(a,b);
}
int main()
{
int ch,ch2;
float a,b;
cout<<"Choice : 1) Illustrating Function Pointers 2) Illustrating Pointer to Array 3) Illustrating pointer to String 4) Exit :P";
while(1){
cout<<"\nEnter your choice...";
cin>>ch;
if(ch==1){
cout<<"Choice : 1)Add 2)Subtract 3)Multiply 4)Divide";
cout<<"\nEnter the choice for calculation...";
cin>>ch2;
if(ch2==1){
cout<<"Enter any two numbers...";
cin>>a>>b;
cout<<"The result is "<<calc(a,b,add);
}
else if(ch2==2){
cout<<"Enter any two numbers...";
cin>>a>>b;
cout<<"The result is "<<calc(a,b,sub);
}
else if(ch2==3){
cout<<"Enter any two numbers...";
cin>>a>>b;
cout<<"The result is "<<calc(a,b,mul);
}
else if(ch2==4){
cout<<"Enter any two numbers...";
cin>>a>>b;
flag=0;
if(calc(a,b,div)!=-1 || flag==0){
cout<<"The result is "<<calc(a,b,div);
}
}
}
else if(ch==2){
int n,i;
cout<<"Enter the number of elements of the array..";
cin>>n;
int a[n];
cout<<"Enter the elements one by one..";
for(i=0;i<n;i++){
cin>>a[i];
}
int *ptr;
ptr=a;
cout<<"The elements of the array is..";
for(i=0;i<n;i++){
cout<<*ptr<<" ";
ptr++;
}
}
else if(ch==3){
char s[maxi];
cout<<"Enter any string..";
cin>>s;
char *ptr;
ptr=s;
cout<<"The String is..";
while(*ptr!='\0'){
cout<<*ptr;
ptr++;
}
}
else{
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment