Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C program I build today that calculates sqrt.  (Read 10074 times)

0 Members and 1 Guest are viewing this topic.

learning_cmd

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    C program I build today that calculates sqrt.
    « on: January 13, 2012, 11:35:47 AM »
    Hey everyone,

    A few days ago I started learning C. And today I gave myself the assignment for this KILLLER program  ;D

    It can calculate the sqrt of any given number, apart from any number higher than 95100. And 89765 doesn't seem to work either.

    I wonder if you would have a look at it, and give me some tips, or inspiration for new assignments.

    Thanks  :)


    #include <stdio.h>

    int main(void)
    {
       int i;
       float fsquare;
       float fstart;
       float fremember;
       float flower;
       float fhigher;
          
       printf("Enter a number to sqrt and hit enter:\n\n");
       scanf("%d", &i);
       
       int fsquare = i;
       fremember = 0;
       flower = fsquare;
       fhigher = 0;
          
       while((flower*flower) > fsquare)
       {
          if((flower*flower) > fsquare)
          {
             fhigher = flower;
          }
          flower = flower / 2;
          
          printf("flower = %f   ", flower);
          printf("fhigher = %f\n\n", fhigher);
       }
       printf("flower = %f   ", flower);
       printf("fhigher = %f\n\n", fhigher);
          
       while(((fremember-fsquare) > .001) || (-(fremember-fsquare) > .001))
       {
          fstart = (flower + fhigher)/2;
          fremember = fstart * fstart;
          
          if (fremember < fsquare)
          {
             flower = fstart;
          }
          
          if (fremember > fsquare)
          {
             fhigher = fstart;
          }
          
          printf("fremember = %f  ", fremember);
          printf("flower = %f  ", flower);
          printf("fhigher = %f\n\n", fhigher);
       }
       
       printf("\nThe number you asked to sqrt was: %d\n\n", i);
       printf("The sqrt lies between %f and %f\n\n", flower, fhigher);
       
       return 0;

    }
    “you don’t need a beret to be creative.”

    Creativity is a dirty job.

    It is something that requires passion, long hours, and banging your head against the wall. It requires you to make a ton of mistakes along the path of creating something awesome.

    Merlin Mann

    Linux711



      Mentor

      Thanked: 59
      • Yes
      • Programming Blog
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: C program I build today that calculates sqrt.
    « Reply #1 on: January 13, 2012, 02:55:04 PM »
    Make a text adventure with ASCII graphics.
    YouTube

    "Genius is persistence, not brain power." - Me

    "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

    learning_cmd

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: C program I build today that calculates sqrt.
      « Reply #2 on: January 13, 2012, 03:08:01 PM »
      uhh  :o
      “you don’t need a beret to be creative.”

      Creativity is a dirty job.

      It is something that requires passion, long hours, and banging your head against the wall. It requires you to make a ton of mistakes along the path of creating something awesome.

      Merlin Mann

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: C program I build today that calculates sqrt.
      « Reply #3 on: January 13, 2012, 03:09:50 PM »
      Quote
      KILLLER program
      Huh?
      You post a program that does not work and call it a killer?
      Is this homework?

      There is a specific classical algorithm for doing square root. Are you claiming that your program does any root?

      There are are C libraries for doing logarithms. Using  it one can do almost any power or root of any pragmatic real number.

      Exponents and Logarithms - The GNU C Library

      learning_cmd

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: C program I build today that calculates sqrt.
        « Reply #4 on: January 13, 2012, 03:29:10 PM »
        I compiled it with tcc, maybe that helps.

        I know that there are basic functions for doing sqrt, but I made this to get into the programming language.

        No it's not homework :)
        “you don’t need a beret to be creative.”

        Creativity is a dirty job.

        It is something that requires passion, long hours, and banging your head against the wall. It requires you to make a ton of mistakes along the path of creating something awesome.

        Merlin Mann

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: C program I build today that calculates sqrt.
        « Reply #5 on: January 13, 2012, 03:43:53 PM »
        OK. It is to improve you skis. Great!  8)
        Have you every done the classical algorithm on a simple calculator that has not sqrt functions.
        How to calculate a square root without a calculator

        The above tells how to do it with pencil and paper, but I like to do it with a small calculator without the sqrt key.

        Once I calculated the 12™ root of 2. But I forgot to write it down!

        Once you are very familiar with the logic of the algorithm, it makes it easier to check your work will the C code you have.

        learning_cmd

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Unknown
          Re: C program I build today that calculates sqrt.
          « Reply #6 on: January 13, 2012, 05:01:44 PM »
          Looked into that algorithm on how to calculate sqrt on paper.

          No wonder old people are cranky most of the time  ;D
          “you don’t need a beret to be creative.”

          Creativity is a dirty job.

          It is something that requires passion, long hours, and banging your head against the wall. It requires you to make a ton of mistakes along the path of creating something awesome.

          Merlin Mann

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: C program I build today that calculates sqrt.
          « Reply #7 on: January 13, 2012, 05:23:33 PM »
          Code: [Select]
          #include <stdio.h>
          #include <stdlib.h>
          double absolute(double value)
          {
          return value>0?value:-value;

          }
          double sqrt(double value)
          {
              if (value < 0) return 0;
                  double atolerance = 1E-15;
                  double t = value;
                  while (absolute(t - value/t) > atolerance*t)
                      t = (value/t + t) / 2.0;
                  return t;


          }

          int main(char* argv[], int argc)
          {
             double enteredvalue;
             printf("Enter Value:\n");
             scanf("%lf", &enteredvalue);
             printf("sqrt of %5.2f is %5.2f\n",enteredvalue,sqrt(enteredvalue));

          }
          Newtons method ftw. Figured I may as well make a cheap abs() equivalent as well for no reason.

          I was trying to dereference Null Pointers before it was cool.

          Salmon Trout

          • Guest
          Re: C program I build today that calculates sqrt.
          « Reply #8 on: January 14, 2012, 01:40:40 AM »
          Looked into that algorithm on how to calculate sqrt on paper.

          No wonder old people are cranky most of the time  ;D

          We are mainly cranky with kids who think you can get everything you want just by pressing a button.

          Rob Pomeroy



            Prodigy

          • Systems Architect
          • Thanked: 124
            • Me
          • Experience: Expert
          • OS: Other
          Re: C program I build today that calculates sqrt.
          « Reply #9 on: January 14, 2012, 08:18:49 AM »
          We are mainly cranky with kids who think you can get everything you want just by pressing a button.

          You can, if it's this button:

          Only able to visit the forums sporadically, sorry.

          Geek & Dummy - honest news, reviews and howtos

          WillyW



            Specialist
          • Thanked: 29
          • Experience: Experienced
          • OS: Windows XP
          .



          Geek-9pm


            Mastermind
          • Geek After Dark
          • Thanked: 1026
            • Gekk9pm bnlog
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: C program I build today that calculates sqrt.
          « Reply #11 on: January 14, 2012, 10:56:41 AM »
          I hit it twice...
          What happens if I do three times?

          kpac

          • Web moderator


          • Hacker

          • kpac®
          • Thanked: 184
            • Yes
            • Yes
            • Yes
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 7
          Re: C program I build today that calculates sqrt.
          « Reply #12 on: January 14, 2012, 10:58:59 AM »
          It goes on for about 200 presses and then repeats itself.

          Bennieboj



            Rookie

          • time flies, ur the pilot
            • Yes
          • Computer: Specs
          • Experience: Familiar
          • OS: Windows 7
          Re: C program I build today that calculates sqrt.
          « Reply #13 on: January 15, 2012, 03:13:03 AM »
          to OP: here are some nice "assignments": http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerE5.html  ;D

          bwilcutt



            Greenhorn

            • Curious Internet
          • Certifications: List
          • Experience: Guru
          • OS: Unknown
          Re: C program I build today that calculates sqrt.
          « Reply #14 on: February 16, 2012, 04:10:43 PM »
          learningcmd,

          Go to www.swboneyard.com and look for the "Calc" program.  This is a full featured algebraric parser I wrote in C a while back, does everything you need.  Might make for a good example.

          Bryan Wilcutt
          Dr. Bryan Wilcutt, DC.S.