Here is a C program that uses traditional C function syntax:
/* Compute a table of cubes. */
#include <stdio.h>
#define N 15
#define MAX 3.5
int main(){
int i;
double x, cube();
printf("\n\nINTEGERS\n");
for (i = 1; i <= N; ++i)
printf("cube(%d) = %d\n", i, cube(i));
printf("\n\nREALS\n");
for (x = 1; x <= MAX; x += 0.3)
printf("cube(%f) = %f\n", x, cube(x));
}
double cube(x)
double x;{
return (x * x * x);
}
It gives the wrong answers for the integer arguments, and it does so because integer arguments are passed as if their bit representation were
double
. It is unacceptable as C++ code.
Re-code this program as a C++ program that uses a proper function prototype. Test your program by compiling it using a C++ compiler.
C++ compilers enforce type compatibility on function argument values. Therefore, the integer values are properly promoted to double
values.
Paste the source code of your program below and click the Submit button when you are ready to submit this exercise.