for the life of me...

This is a discussion on for the life of me... within the C Programming forums, part of the General Programming Boards category; i cannot figure out how to input the square root of this problem. i just started a class a few ...

  1. #1
    vwy
    vwy is offline
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    for the life of me...

    i cannot figure out how to input the square root of this problem. i just started a class a few days ago at university and my teacher made the assumption that we all have a hale background in computer science:
    To test our knowledge of loops and if statements, we shall write a program which will prompt the user to enter an integer n. If n is positive, the user should print out a table of squares, cubes and square roots of the numbers between 1and n (inclusive). If the number is zero or negative, the user should print an appropriate error message.

    i've had to largely teach myself with online courses, so i've been hacking away at this for a few hours. it must be noted again that i am a complete novice, so please be liberal with the layman's terms.

    this is what i've got so far:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    int n;
    int r=(n-1);
    printf("Enter an Integer: ");
    scanf("%d", &n);
    if (n>0)
    {
    printf("%d\n",n*n*n);
    printf("%d\n",n*n);
    printf("%f\n",sqrt(n));
    }
    while (r>1)
    {
    printf("%d",r);
    r=r*r*r;
    r=r*r;
    printf("%f",r=sqrt(r));
    }
    else
    {
    printf("Sorry, try inputting a positive number next time.\n");
    }
    return 0;
    }



    thanks in advance for being reasonable.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    i think you have a general idea of how to write the program but alittle bit stuck about using the keywords.

    1. the format of an if and else statement is shown below (yours is wrong- you can't place the while statement in between the } and else)
    Code:
    if (condition) {
        statements;
    }
    else {
        statements;
    }
    2. might want to use a for statement (similar to while) to output your square roots and so on. general form shown below.
    Code:
    for(initiation; condition; increment) {
        statement;
    }
    here is what i would come up with using some of your code (if you input something that is not a number error will occur)
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void){
        int n;
        
        //get your integer
        printf("Enter an Integer: ");
        scanf("%d", &n);
        
        if (n>0) {
            // prints the table of squares, cube and square root
            printf("number\tsquare\tcube\tsquareroot\n");
            for (n; n >0; --n) {
                printf("%d \t%d \t%d \t%.2f\n", n, n*n, n*n*n, sqrt(n));
            }    
        }
        else {
            printf("Sorry, try inputting a positive number next time.\n");
        }
        return 0;
    }
    Last edited by peterchen; 01-24-2006 at 05:04 AM.

  3. #3
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    it looks like youve done a pretty good job getting the basics down. now, you need to get yourself into a cold, computerized mindset.

    first of all, youve tried to initialize r even though n hasnt been initialized yet. after the user inputs n, then you can go ahead and set r to equal n-1.

    secondly, you need to nest the while into the if block. the way you currently have your code, the while executes no matter what, probably printing out garbage values in the process.

    thirdly, your while loop needs to be rewritten since it looks like its going to do something really crazy anyways. ive rewritten it for you, so it is similar to the code you had right after if. it will print the cube, square, and sqrt of the numbers in order from 1 to n. ive used a for loop, so if you havent learned it yet, ill explain it to you later.

    in the process, ive made your variable r obsolete, but im going to leave it in as an example, even if its not needed.

    ive also indented your code for you so you can read it easier and understand how the nesting works. afterwards, you can go ahead and format your output as you wish, since it looks like its going to be a jumble of numbers.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        int n;
        int r;  //not used anymore
        int i;
    
        printf("Enter an Integer: ");
        scanf("%d", &n);
        r = (n-1);  //initialize r after n
    
        if (n>0)
        {
            for(i=1;i<=n;i++) //cycle through 1 to n
            {
                printf("%d\n",i*i*i); //cube
                printf("%d\n",i*i); //square
                printf("%f\n",sqrt(i)); //square root
            }   //set of...
        }   //...nested brackets
    
        else
        {
            printf("Sorry, try inputting a positive number next time.\n");
        }
    
        return 0;
    }
    Last edited by Bigbio2002; 01-24-2006 at 05:04 AM.
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    "should print out a table of squares, cubes and square roots of the numbers between 1 and n (inclusive)" your program Bigbio2002 does not do this. instead it prints a column of numbers alternating between cube, square and square root of n.

  5. #5
    vwy
    vwy is offline
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    thank you so much, guys. i truly am grateful for your prompt and helpful replies!
    what is the a function?
    and the square root function is in the math library, right? so what would i type to have it produce square roots?
    i've tried the tutorials on this site and have searched for square root, but to no avail.
    thanks again. i really appreciate your help.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    "what is the a function?
    and the square root function is in the math library, right? so what would i type to have it produce square roots?"

    squareroot = sqrt(your_number);

    sorry i don't understand - i can't help if i don't understand provide more info

  7. #7
    vwy
    vwy is offline
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    i think i've now got it.
    thanks again.

  8. #8
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38
    Quote Originally Posted by peterchen
    "should print out a table of squares, cubes and square roots of the numbers between 1 and n (inclusive)" your program Bigbio2002 does not do this. instead it prints a column of numbers alternating between cube, square and square root of n.
    i know thats not the exact output that he wanted. i said that hed have to format it to make it look nice, but thats the gist of what the program is supposed to do.
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  9. #9
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    ok, sorry about that, i think i missed that bit

    anyway it's a good thing vwy what he/she was looking for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Artificial Life: Where to Start?
    By Mr.Sellars in forum General AI Programming
    Replies: 11
    Last Post: 09-22-2007, 03:03 AM
  2. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 03:17 PM
  3. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 08:53 PM
  4. The more intelligent risks you take in life, the more you'll achieve
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-20-2003, 06:23 PM
  5. Life, The Universe, and everything else
    By ZooTrigger1191 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 03-29-2003, 05:33 PM

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