Sponsored Content
Top Forums Programming Programming Challenges - A List Post 302122630 by Octal on Wednesday 20th of June 2007 11:10:21 PM
Old 06-21-2007
Challenge: Write your own square root function.

Difficulty: Easy

My Implementation:
Code:
#include <stdio.h>
#include <stdlib.h>

float root(float);

main(int argc, char *argv[]) {
        if (argc < 2) {
                fprintf(stderr, "usage: %s [number]", argv[0]);
                exit(0);
        }

        while (*++argv) {
                printf("%f\n", root(atof(*argv)));
        }
}
float root(float o) {
        float r = 0.5*(o/2);

        while ((r*r) != o) {
                r = 0.5*(o/r+r);
        }
        return r;
}

Try entering a number like 10. It doesn't exactly work. So I made a new program:
Code:
#include <stdio.h>
#include <stdlib.h>

float root(float);

main(int argc, char *argv[]) {
	if (argc < 2) {
		fprintf(stderr, "usage: %s [number]", argv[0]);
		exit(0);
	}

	while (*++argv) {
		printf("%.2f\n", root(atof(*argv)));
	}
}
float root(float o) {
	float r = 0.5*(o/2);
	int i = 0;

	while ((r+1)*(r+1) <= o) {
		r++;
	}
	while (i < 4) {
		r = 0.5*(r+o/r);
		i++;
	}

	return r;
}

 

3 More Discussions You Might Find Interesting

1. AIX

AIX 6.1 IDSLDAP Installation Challenges

Please bare with me, since I am new to AIX and LDAP. I am attempting to install idsldap server on our AIX 6.1 NIM server. I installed the following packages: root@nim(/)# lslpp -l|grep ldap db2_08_01.ldap 8.1.1.80 COMMITTED DB2 LDAP Support idsldap.clt64bit61.rte 6.1.0.17 COMMITTED... (6 Replies)
Discussion started by: ecollins
6 Replies

2. UNIX for Advanced & Expert Users

Challenges in finding and copying the block

Hi, I have a below challenging task where iam unable to find the block and copy the same into a file. I tried my luck,howver iam unable to reach the first and second step..Can anyone help me with a clue or with the commands so that i can give a try. 1. search the <number>9966993366</number>... (2 Replies)
Discussion started by: cskumar
2 Replies

3. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies
obd2kml(1)						      General Commands Manual							obd2kml(1)

NAME
obd2kml - Convert obdgpslogger(1) logs to Google Earth kml files SYNOPSIS
obd2kml [ options ] DESCRIPTION
Convert obdgpslogger(1) logs to Google Earth kml files OPTIONS
-o|--out <output filename> Output to this .kml file -a|--altitude <altitude> In the kml file, normalise the graph to this height -d|--db <database> Work from logs stored in this database file -n|--name <folder name> Everything in this output file is wrapped in a folder named this -v|--version Print out version number and exit. -h|--help Print out help and exit. NOT OPTIONS
These options aren't intended for end-users, they're for the GUI. -p|--progress Print out progress. It will occasionally print a number in the range [0..100], indicating progress percentage. SEE ALSO
obdgpslogger(1), obd2csv(1), obd2gpx(1), obdsim(1), obdgui(1), obdlogrepair(1) AUTHORS
Gary "Chunky Ks" Briggs <chunky@icculus.org> obd2kml(1)
All times are GMT -4. The time now is 05:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy