How do you write a c function to produce a table of the numbers from 1 to their squares and their cubes?
#include<stdio.h>
const unsigned int rows = 10;
const unsigned int cols = 3;
int table[rows][cols];
int square (const int n) {
return n * n;
}
int cube (const int n) {
return n * n * n;
}
void initialise_table (void) {
int x, y, val;
for (x=0; x<rows; ++x) {
val = x+1;
table[x][0] = val;
table[x][1] = square (val);
table[x][2] = cube (val);
}
}
int main (void) {
int x, y;
initialise_table ();
printf ("Value\tSquare\tCube\n");
for (x=0; x<rows; ++x) {
printf("%d\t%d\t%d\n", table[x][0], table[x][1],
table[x][2]);
}
return 0;
}