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
CATANH(3)						     Linux Programmer's Manual							 CATANH(3)

NAME
catanh, catanhf, catanhl - complex arc tangents hyperbolic SYNOPSIS
#include <complex.h> double complex catanh(double complex z); float complex catanhf(float complex z); long double complex catanhl(long double complex z); Link with -lm. DESCRIPTION
The catanh() function calculates the complex arc hyperbolic tangent of z. If y = catanh(z), then z = ctanh(y). The imaginary part of y is chosen in the interval [-pi/2,pi/2]. One has: catanh(z) = 0.5 * (clog(1 + z) - clog(1 - z)) VERSIONS
These functions first appeared in glibc in version 2.1. CONFORMING TO
C99. EXAMPLE
/* Link with "-lm" */ #include <complex.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char *argv[]) { double complex z, c, f; if (argc != 3) { fprintf(stderr, "Usage: %s <real> <imag> ", argv[0]); exit(EXIT_FAILURE); } z = atof(argv[1]) + atof(argv[2]) * I; c = catanh(z); printf("catanh() = %6.3f %6.3f*i ", creal(c), cimag(c)); f = 0.5 * (clog(1 + z) - clog(1 - z)); printf("formula = %6.3f %6.3f*i ", creal(f2), cimag(f2)); exit(EXIT_SUCCESS); } SEE ALSO
atanh(3), cabs(3), cimag(3), ctanh(3), complex(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2011-09-15 CATANH(3)
All times are GMT -4. The time now is 09:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy