1.

Solve : C program I build today that calculates sqrt.?

Answer»

Hey EVERYONE,

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

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

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;

}Make a text adventure with ASCII graphics.uhh  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 LibraryI 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 OK. It is to improve you skis. Great! 
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.Looked into that algorithm on how to calculate sqrt on paper.

No wonder old people are cranky most of the time  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.

Quote from: 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 

We are mainly cranky with kids who think you can get everything you want just by pressing a button.
Quote from: Salmon Trout on January 14, 2012, 01:40:40 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:

Quote from: Rob Pomeroy on January 14, 2012, 08:18:49 AM
You can, if it's this button:


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


I hit it twice...
What happens if I do three times?It goes on for about 200 presses and then repeats itself.to OP: here are some nice "assignments": http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerE5.html  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


Discussion

No Comment Found