Hello all,
I am trying to learn C++ on my spare time and am following the book "c++ primer plus" right now.. In one of the exercises I want to be able to use a function as one of the arguments to an other function. However, the function inside of the other functions arguments is not being called.. here is a piece of the code...
The prototype of the two functions:
The function call:Code:int average_score(int ar[], int limit); void display_scores(const int ar[], int limit, int (*avg_score)(int*, int));
and the definitions:Code:display_scores(golf_scores, num_of_scores, average_score);
I know that it is not necessary to do it like this, but I would like to know why it doesn't work. I will attach the entire piece of code.. Thanks to anyone who can help me.. I worked on this problem for several hours tonight and could not figure it out.Code:int average_score (int ar[], int limit) { int average = 0; int x; for (x = 0; x < limit; x++) { average = average + ar[x]; } average = average / limit; cout << "This is function average_score() " << endl ; return average; } void display_scores(const int ar[], int limit, int (*avg_score)(int*, int)) { int x; for (x = 0; x < limit; x++) { cout << ar[x] << " "; } cout << endl << "The average score is " << avg_score << endl; }



LinkBack URL
About LinkBacks


