C, compiler - write a table from a program

  1. Hi,
    I am trying to write a program on a C compiler and Im not too sure how to do it.

    The question asks to write a full program to calculate the squares and cubes of the numbers from 0 to 10, which uses tabs (\t) to illustrate the data in the table.

    Click on the uploaded document to view the question.

    "Ques Example.jpg"

    Also I have given an example of a square root I have calculated for a number in
    document,

    "Example of.jpg"

    However Im not too sure which instructions are needed to show the information in a table.

    Please help, thanks very much !
     

    Attached Files:

  2. jcsd
  3. D H

    Staff: Mentor

    Some questions:
    • What work have you done so far?
    • What is the general layout of your program?
    • Do you know how to do the math, how to print?
    • Do you know how to do this for each integer from 0 to 10?
     
  4. Well I only know how to add, subtract and multiply groups of numbers together and find the average of them.

    I know how to print instructions on the screen, for instance, phrases like "Enter two integers", "Answer/sum ="

    Thats about it
     
  5. D H

    Staff: Mentor

    You have to compute the square and cube of a number. You already computed the cube. Print the data using printf, one line at a time. If formatted correctly you will get the desired pretty table. You already used "\n" to generate an end of line. Tab is just another special character: "\t" instead of "\n".

    Have you been taught about looping yet?
     
  6. How do you prevent the numbers from showing up.
    Shouldnt it be so that when you type in the number "3" under the number title (for example) all of its possible combinations will show up such as "9" and "27" under the squared and cubed titles.

    I think we havent been taught looping yet?
    Any example of this?
     
  7. Hang on,
    Do I just type the correct values under each header instead, without it actually being a flowing program.
     
  8. D H

    Staff: Mentor

    Looping in 'c' is done with the for or while statement.

    I think you are supposed to do the calculations. You could easily write a one statement program that produces the table (just print the table), but that would violate the spirit of the assignment and would probably result in some point deductions.
     
  9. haha.
    Yes thats right.

    Um... I have only really mannaged to seperate the three columns. But when I type in the numbers it wont show properly, as Im missing out a large part of the program.
     
  10. Can you post your code? You can post your code on rafb.net/paste - you will be provided with a link - please paste that here.

    Also remember to make sure "C" formatting is on when you're on that website.
     
  11. Yes I have it here now.
    It automaticaly calculates the square and cube roots of the first number typed in.

    However the first number you type appears in the table as well and will not let me type in more numbers for the next line.
    A loop in the program is needed here ????

    http://rafb.net/p/CBphkL43.html
     
    Last edited: Oct 8, 2007
  12. yes a loop is needed here.

    EDIT sorry I thought you didnt want to input numbers to appear for some reason.

    Pay attention to the carriage return that will make your table look bad after scanf.
     
    Last edited: Oct 8, 2007
  13. This code is fairly simple as long as your know about the math header and how to use loops. I commented this code fairly well, so you shouldn't have any trouble understanding. Make sure you know what's going on in every line though. This is some VERY BASIC stuff that is extremely vital in computer programming. So it's important for you to learn about this stuff now, rather then later.

    Code (Text):
    #include <stdio.h>
    #include <math.h>

    int main(void)
    {
        /* declare your variables */
        double idx, square, cube;
       
        /* initialize your variables */
        idx = 0;
        square = 0;
        cube = 0;
       
        /* first row of table aka titles */
        printf("number\tsquare\tcube\n");
        printf("------\t------\t-----\t\n");
       
        /* main for loop. prints table */
        for(idx; idx <= 10; idx++){
            /* raise each increment to power of 2 */
            square = pow(idx, 2);
            /* raise each increment to power of 3 */
            cube = pow(idx, 3);
            /* print your floating point variables to zero decimal */
            printf("%.0f\t%.0f\t%.0f\n", idx, square, cube);  
        }
       
        printf("\nPress Enter to continue...");
        getchar();
        return 0;
    }
     
  14. Thanks very much for your help!
    I have been changing parts in the program to get a better understanding of how it works.

    I see that you added in maths header and how the "<= 10" part is there to decide on how many numbers (increasing from 0) are required in the table, as the numbers must be less than or equal to 10. Also the word "double" is an instuction that must be enterd just like "printf, scanf, int" e.t.c for it to be performed.

    And then what ever you replace "n" with in "A = pow(x, n);" will determine the power in the coloumn labeld as "A".
    The ".0f" after the answer given by "%d" is needed for the answers to appear in that coloumn.
    "getchar();" is needed for the black screen to appear on the screen until the user closes it.

    But Im not sure why the "++" part is needed after the "<= 10; ix" bit.
    ???
     
  15. ++ is to increment variable idx so after it deals with one number say 3 in the loop and adds it to the table, it will move to 4 and so on.

    idx++ means idx=idx+1. If this was not put then you will get stuck in an infinite loop and have a program crash.
     
  16. Ah cool, I understand now.

    Thanks once again !

    Also I tried running the program without the "void" in between the brackets "int main( )" and it seemed to work fine, what is the reason for this?
     
    Last edited: Oct 11, 2007
  17. I have another question and managed to answer it, as it seems like it is working correctly.

    Th program below is designed to state whether the integer entered is an Even or Odd number. I just thought there might be a better and more proffesional way of writting it?

    Here it is:

    http://rafb.net/p/uKmqqQ58.nln.html
     
  18. Hey sorry for the late response.

    "int main(void)" is just a c99 standard which means it's universally accepted by any compiler...

    although almost every compiler accepts "int main()" since void is assumed because no command line arguments are needed..

    "int main(int argc, char **argv)" is also the appropriate c99 standard way of using command line arguments...

    The link you posted doesn't work. But THE BEST way to check for even and odd numbers without going outside the C standard library is to divide by 2 using modulo... module is just "%" and it returns the remainder... so odd will return 1 and even will be 0..

    here's a quick example
    Code (Text):
    #include <stdio.h>

    int main(void)
    {
        int idx = 0;

        for(idx; idx <= 10; idx++){
            if((idx % 2) == 0) {
                printf("%d = even\n", idx);
            }else{
                printf("%d = odd\n", idx);
            }
        }
       
        getchar();
        return 0;
    }
    anyways im just coming off a 3 hour shroom trip n im still feeling a bit magical at this point.. so if this post was confusing it was the magic ;)
     
  19. Hey NoUse, why is it that when you add .0, or even . in front of f in the 2nd to last printf statement it removes the 0's after the decimal place for each number? I noticed that if you take out the .0, the output will be 0.000000, 1.000000, etc. And if you add the . or .0 before f it will just print 0, 1, etc. Also, I notcied that if you use int, instead of double the program just outputs all 0's, why is double needed? I know double is for double precision and that's it. Thanks!
    -Matt
     
    Last edited: Feb 4, 2009
Know someone interested in this topic? Share a link to this question via email, Google+, Twitter, or Facebook

Have something to add?