sqrt() question...

This is a discussion on sqrt() question... within the C Programming forums, part of the General Programming Boards category; Ok, so im still pretty new to C programming and i was reading the stickies and i found a post ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    sqrt() question...

    Ok, so im still pretty new to C programming and i was reading the stickies and i found a post in the C Book Recomendations sticky that has some assignments. The assignment was to print out the square root of the numbers of 1-10. It sounded pretty simple and so i decided to do it. I came up with this:
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
            int i=1;
            int square = 0;
            while(i <= 10){
                    square = sqrt(i);
                    printf("%d\n", &square);
                    i++;
                    }
    }
    The first problem i came across was the compiler spitting out some wierd warning at me, but i found out that there was a special flag that i had to use to link the math.h library.
    Code:
    gcc -o newsqrt newsqrtc -lm
    which i assume means ''Link Math'' (please correct me if im wrong i couldnt find it in the documentation that i found). So i run the program and it doesnt work like i thought it would.
    Code:
    Mike@laptop:~> ./newsqrt
    -1078179784
    -1078179784
    -1078179784
    -1078179784
    -1078179784
    -1078179784
    -1078179784
    -1078179784
    -1078179784
    -1078179784
    I assume that its memory addresses or something.
    i recompiled the proggram using
    Code:
    gcc -o -ggdb newsqrt newsqrt.c
    and i run it through gdb and i get this:
    Code:
    (gdb) l
    1       #include <stdio.h>
    2       #include <math.h>
    3       int main()
    4       {
    5               int i=1;
    6               int square = 0;
    7               while(i <= 10){
    8                       square = sqrt(i);
    9                       printf("%d\n", &square);
    10                      i++;
    (gdb) b 7
    Breakpoint 1 at 0x8048433: file newsqrt.c, line 7.
    (gdb) r
    Starting program: /home/Mike/newsqrt
    
    Breakpoint 1, main () at newsqrt.c:7
    7               while(i <= 10){
    (gdb) s
    8                       square = sqrt(i);
    (gdb) s
    9                       printf("%d\n", &square);
    (gdb) s
    -1075492664
    10                      i++;
    and then it goes through it all again.
    My big questions are; what am i forgetting and what (if anything) can i do to make this code better?
    i hope that this isnt too much for my first thread. I searched the forum for people with a similar problem but i didnt find it.
    thanks in advance for the help.
    -Mikey_ickey

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    printf("%d\n", &square);
    Try losing the & operator. As far as I've noticed, you use variable for output such as printf, and &variable for input such as scanf.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int square = 0;
    Also sqrt return a double, so you want:
    double square = 0;

    > printf("%d\n", &square);
    And here %f
    printf("%f\n", square);

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    gcc -W -Wall -ansi -pedantic -O2 newsqrt.c
    would be a revelation!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Use %g instead of %d in your printf() function, because %d means an integer.
    Also, don't you think this would be easier:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void){
        for(int i=1;i<=10;i++){
            printf("%g\n",sqrt(i));
        }
        return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Quote Originally Posted by maxorator
    Use %g instead of %d in your printf() function, because %d means an integer.
    Also, don't you think this would be easier:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void){
        for(int i=1;i<=10;i++){
            printf("%g\n",sqrt(i));
        }
        return 0;
    }
    OH YEAH! I guess that would be easier. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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