# Square, Cube, Quartic, Quintic Table

This is a discussion on Square, Cube, Quartic, Quintic Table within the C Programming forums, part of the General Programming Boards category; Hey, my homework is that I have to write functions for square(), cube(), quartic(), and quintic(), and then use those ...

1. ## Square, Cube, Quartic, Quintic Table

Hey, my homework is that I have to write functions for square(), cube(), quartic(), and quintic(), and then use those to create a program that creates a table like below, for an inputted 'n' values. I got the first part I think, but then every time I try to execute it, I get a segmentation fault. Any idea what I'm doing wrong?
 Integers Square Cube Quartic Quintic 1 1 1 1 1 2 4 8 16 32 3 9 27 81 243 n n2 n3 n4 n5

Code:
```#include <stdio.h>
int square(int n)
{
int square_of_n;
square_of_n = n * n;
return square_of_n;
}
int cube(int n)
{
int n_cubed;
n_cubed = n * n * n;
return n_cubed;
}
int quartic(int n)
{
int quartic_n;
quartic_n = n * n * n * n;
return quartic_n;
}
int quintic(int n)
{
int quintic_n;
quintic_n = n * n * n * n * n;
return quintic_n;
}

int main ()
{
int n, i;
i=0;
scanf("%d", n);
while (i<n)
{
printf("\nNumber\tSquare\tCube\tQuartic\tQuintic\n");
printf("%d\t%d\t%d\t%d\t%d\n");
}
return 0;
}```

2. You're not compiling with the warning level turned all the way up, or you're ignoring your compiler's warnings:
Code:
```\$ gcc -Wall -o powers powers.cpowers.c: In function ‘main’:
powers.c:32: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
powers.c:36: warning: too few arguments for format
powers.c:32: warning: ‘n’ is used uninitialized in this function```
Line 32: You need to provide the address of where to store the variable. Put an & in front of the n.
Line 36: printf doesn't have magical mind-reading powers. You need to tell it what values to print, specifically n and the values returned by square, cubic, etc.

3. Also, you need to call the functions from main. They can't execute if you don't call them.

And increment i in your while loop

4. Ok, I fixed those problems, thanks for that. But now when I execute it, the output for the square column is 67392, all the way down. And then the same for the cube, quartic, and quintic but with 67440, 67508, and 67596, respectively.

6. Code:
```#include <stdio.h>

int square(int n)
{
int square_of_n;
square_of_n = n * n;
return square_of_n;
}

int cube(int n)
{
int n_cubed;
n_cubed = n * n * n;
return n_cubed;

}

int quartic(int n)
{
int quartic_n;
quartic_n = n * n * n * n;
return quartic_n;
}

int quintic(int n)
{
int quintic_n;
quintic_n = n * n * n * n * n;
return quintic_n;
}

int main ()
{
int n, i;
i=0;
scanf("%d", &n);
while (i<n)
{
printf("\nNumber\tSquare\tCube\tQuartic\tQuintic\n");
printf("%d\t%d\t%d\t%d\t%d\n", i, square, cube, quartic, quintic);
i++;
}
return 0;
}```

7. You still don't call your functions.

EDIT: Sorry, I didn't look close enough. Seemingly you think you called them. But you didn't. A function call involves the brackets. Plus you defined your functions to take an argument, which, when you call any of the functions, has to be passed to it. Review the usage of functions.

8. Just using the name of the function makes it a function pointer (i.e. it gives you the address of the function). You need to call it by using () after the function name and putting any parameters inside the (), like square(n).

9. Oh wow, bad mistake. I get it now though, thank you everyone!