473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

function pointer as function argument

I need help writing a program which will display a menu with options Square, Cube, Fourth Power and Quit. Accept S,C,F,Q from the user non case sensitive. Use integers between 0 and 100 and tabulate the selected function for all integers within this interval. I need to use a C++ function with a function pointer

Thanks a lot in advance

Need answers in next 4 days
Apr 13 '10 #1

✓ answered by newb16

Passing function pointer as a parameter will look like this.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. typedef int (*funptr)(int);
  3. int tabb(funptr f) {
  4.   // for all in range.....
  5.   printf("%d ",f(2));
  6. }
  7.  
  8. int cubefun(int x) {
  9. return x*x*x;
  10. }
  11.  
  12. int pow4fun(int x) {
  13. return x*x*x*x;
  14. }
  15.  
  16. int main() {
  17.   funptr userfun;
  18.   //userfun = cubefun;
  19.   userfun = pow4fun;
  20.   tabb(userfun);
  21.  

8 2671
jkmyoung
2,057 Expert 2GB
Didn't we already answer this?.. hold on looking for the thread. (perhaps it was deleted)
----
Show what you've done so far.
Apr 13 '10 #2
@HighlightReel44
hey i havnet been able to locate the thread for this because i guess i asked this question twice any luck locating it

Thanks
Apr 18 '10 #3
Banfa
9,065 Expert Mod 8TB
The original thread was deleted because it did not meet our posting guidelines.

In particular you need to make sure that when you post that you
  • Give as much detail as possible When you post a question or problem, express the situation clearly and concisely and include all relevant information, code used, data used, result expected, result achieved and any error codes or messages that you get as a result.
  • Use Clear English to write your question in if possible, try to avoid using abbreviations
  • Make use of the available formatting tags in the forum Use CODE tags around your code unless you are posting a single line of code in which case it is acceptable to omit them:
    * [code]..code goes here..[/code]

I would say this thread is also at risk of deletion.
Apr 18 '10 #4
I need to write a program which will display a menu with the options Square, Cube, Fourth Power and Quit. Accept S, C, F or Q from the user, non-case sensitive. Do not forget to validate the user input. When a valid option is selected, the program will begin tabulating the selected function in an interval. Ask the user for two integers in the range 0 to 100, to be used as interval boundaries. Tabulate the selected function (Square, Cube, or Fourth Power) for all integers within the interval. To implement the tabulation, use a C++ function with a function pointer argument (so that you can pass to it function square, cube, or fourthPower as an actual argument). Provide well formatted, easily readable output.

Thanks
Apr 18 '10 #5
Banfa
9,065 Expert Mod 8TB
In the time you have spent asking us for help you could have already written your program and started ironing out the problems.

We WILL NOT write your code for you. Write some yourself and post problems you are having with it.
Apr 19 '10 #6
newb16
687 512MB
Passing function pointer as a parameter will look like this.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. typedef int (*funptr)(int);
  3. int tabb(funptr f) {
  4.   // for all in range.....
  5.   printf("%d ",f(2));
  6. }
  7.  
  8. int cubefun(int x) {
  9. return x*x*x;
  10. }
  11.  
  12. int pow4fun(int x) {
  13. return x*x*x*x;
  14. }
  15.  
  16. int main() {
  17.   funptr userfun;
  18.   //userfun = cubefun;
  19.   userfun = pow4fun;
  20.   tabb(userfun);
  21.  
Apr 19 '10 #7
donbock
2,426 Expert 2GB
Refer to the qsort man page for an example.
Apr 19 '10 #8
@newb16
Thanks a lot this was very helpful
Apr 20 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: John Harrison | last post by:
Can anyone explain why this code fails to compile? #include <iostream> void func() { std::cout << "success!\n"; } struct S
11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
3
by: Thomas Barth | last post by:
Hi, I dont understand the third argument of this function. This function expect the address of a function, but what does "void *(*funktion)(void *)," mean? I am confused by so many "wildcards" :-)...
51
by: Richard Hengeveld | last post by:
Hi all, I'm trying to understand how pointers for function parameters work. As I understand it, if you got a function like: void f(int *i) { *i = 0; }
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
6
by: Charles Sullivan | last post by:
I define and initialize an array of structures like the following, (where the <verbiage within angle bracketsis just meant to be explanatory): int func1(<argument prototypes>); int...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.