What is this code trying to accomplish? Explain. #include using namespace std; int sum(int(*) (int), int); int square(int); int cube(int); int main() { cout << sum(square, 4) << endl; cout << sum(cube, 4) << endl; system("pause"); } int sum(int(*pf) (int k), int n) { int s = 0; for (int i = 1; i <= n; i++) s = s + (*pf) (i); return s; } int square(int k) { return k*k; } int cube(int k) { return k*k*k; } Results: 30 100
What is this code trying to accomplish? Explain.
#include <iostream>
using namespace std;
int sum(int(*) (int), int);
int square(int);
int cube(int);
int main()
{
cout << sum(square, 4) << endl;
cout << sum(cube, 4) << endl;
system("pause");
}
int sum(int(*pf) (int k), int n)
{
int s = 0;
for (int i = 1; i <= n; i++)
s = s + (*pf) (i);
return s;
}
int square(int k)
{
return k*k;
}
int cube(int k)
{
return k*k*k;
}
Results:
30
100
Given:
What is this code trying to accomplish? Explain.
#include <iostream>
using namespace std;
int sum(int(*) (int), int);
int square(int);
int cube(int);
int main()
{
cout << sum(square, 4) << endl;
cout << sum(cube, 4) << endl;
system("pause");
}
int sum(int(*pf) (int k), int n)
{
int s = 0;
for (int i = 1; i <= n; i++)
s = s + (*pf) (i);
return s;
}
int square(int k)
{
return k*k;
}
int cube(int k)
{
return k*k*k;
}
Results:
30
100
Step by step
Solved in 2 steps