

InterviewSolution
1. |
Solve : C program I build today that calculates sqrt.? |
Answer» Hey EVERYONE, KILLLER programHuh? 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. 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 |
|