Computer Hope

Software => Computer programming => Topic started by: learning_cmd on January 13, 2012, 11:35:47 AM

Title: C program I build today that calculates sqrt.
Post by: learning_cmd 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;

}
Title: Re: C program I build today that calculates sqrt.
Post by: Linux711 on January 13, 2012, 02:55:04 PM
Make a text adventure with ASCII graphics.
Title: Re: C program I build today that calculates sqrt.
Post by: learning_cmd on January 13, 2012, 03:08:01 PM
uhh  :o
Title: Re: C program I build today that calculates sqrt.
Post by: Geek-9pm 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 (http://www.gnu.org/s/hello/manual/libc/Exponents-and-Logarithms.html)
Title: Re: C program I build today that calculates sqrt.
Post by: learning_cmd 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 :)
Title: Re: C program I build today that calculates sqrt.
Post by: Geek-9pm 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 (http://www.homeschoolmath.net/teaching/square-root-algorithm.php)

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.
Title: Re: C program I build today that calculates sqrt.
Post by: learning_cmd 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
Title: Re: C program I build today that calculates sqrt.
Post by: BC_Programmer 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.

Title: Re: C program I build today that calculates sqrt.
Post by: Salmon Trout 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.
Title: Re: C program I build today that calculates sqrt.
Post by: Rob Pomeroy 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:

(http://www.acus.org/files/images/bluewire%20media%204%208%2011%20launch-button.jpg)
Title: Re: C program I build today that calculates sqrt.
Post by: WillyW on January 14, 2012, 09:01:04 AM
You can, if it's this button:


http://www.85qm.de/up/BigRedButton.swf


:D
Title: Re: C program I build today that calculates sqrt.
Post by: Geek-9pm on January 14, 2012, 10:56:41 AM
I hit it twice...
What happens if I do three times?
Title: Re: C program I build today that calculates sqrt.
Post by: kpac on January 14, 2012, 10:58:59 AM
It goes on for about 200 presses and then repeats itself.
Title: Re: C program I build today that calculates sqrt.
Post by: Bennieboj 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 (http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerE5.html)  ;D
Title: Re: C program I build today that calculates sqrt.
Post by: bwilcutt 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