Like Tree3Likes
  • 2 Post By anduril462
  • 1 Post By rmcl

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. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    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;
            printf("Please enter a number: \n");
            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. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    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.
    dennis.cpp and msmithng like this.

  3. #3
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    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
    Last edited by rmatze; 10-20-2011 at 12:22 PM.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    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.

  5. #5
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Repost your updated code.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    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;
            printf("Please enter a number: \n");
            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. #7
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Last edited by dennis.cpp; 10-20-2011 at 01:16 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    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. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Oh wow, bad mistake. I get it now though, thank you everyone!
    dennis.cpp likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 03-30-2011, 02:44 AM
  2. C Help. square and cube a user inputed number
    By Beedee5889 in forum C Programming
    Replies: 5
    Last Post: 03-11-2010, 01:53 PM
  3. line-square and circle-square intersections
    By tomast in forum Game Programming
    Replies: 1
    Last Post: 11-16-2008, 08:12 AM
  4. Sorting a table and saving a table help!!!
    By MrMe913 in forum C Programming
    Replies: 4
    Last Post: 04-18-2007, 01:19 PM
  5. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21