I don’t quite see the point of another file under the same project just to print powers of three, but that’s possibly your homework.
Anyway, I think the whole point is to see how to include a file, so I also isolated the power function in another file.
power_calculator.c will accept two parameters, {number} and {power} respectively.
Here is how you can do it:
power_calculator.c
// power_calculator {n} {k}
// Calculates {n}^{k}
#include <stdio.h>
#include <stdlib.h>
// #include <math.h>
#include "math_power.h"
int main (int argc, char *argv[])
{
// two parameters should be passed, n, and k respectively - argv[0] is the name of the program, and the following are params.
if(argc < 3)
return -1;
// you should prefer using math.h's pow function - in that case, uncomment the #import <math.h>
//printf("%f\n", power(atof(argv[1]), atof(argv[2])));
// atof is used to convert the char* input to double
printf("%f\n", math_power(atof(argv[1]), atof(argv[2])));
return 0;
};
math_power.h
#ifndef _MATH_POWER_H_
#define _MATH_POWER_H_
double math_power(double number, double power);
#endif
math_power.c
#include "math_power.h"
double math_power(double number, double power){
double result = 1;
int i;
for( i = 0; i < power; i++ ){
result*=number;
}
return result;
}
powers_of_three.c
#include <stdio.h>
#include "math_power.h"
int main (int argc, char *argv[])
{
int i;
// here is your range 0-9
for(i = 0; i < 10; i++)
printf("%f\n", math_power(3, i));
return 0;
};
To compile either powers_of_three.c or power_calculator.c, remember to include math_power.h and math_power.c.
solved Expected identifier or ‘(‘