Computer Hope
Software => Computer programming => Topic started 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;
}
-
Make a text adventure with ASCII graphics.
-
uhh :o
-
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)
-
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 :)
-
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.
-
Looked into that algorithm on how to calculate sqrt on paper.
No wonder old people are cranky most of the time ;D
-
#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.
-
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.
-
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)
-
You can, if it's this button:
http://www.85qm.de/up/BigRedButton.swf
:D
-
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 (http://www.doc.ic.ac.uk/~wjk/C++Intro/RobMillerE5.html) ;D
-
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