need some C noob help

This is a discussion on need some C noob help within the C Programming forums, part of the General Programming Boards category; hi, I'm just starting out scripting in C, and while I know the theory, practice seems to be harder then ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    39

    need some C noob help

    hi,

    I'm just starting out scripting in C, and while I know the theory, practice seems to be harder then I had hoped.

    I wrote this simple script as an exercise but for some reason I don't get the expected result:

    Code:
    #include <stdio.h>
    
    float sqroot(unsigned int, float);
    float absff(float);
    
    int main()
    {
    	int first, second, nil=1;
    	while(nil){
    		printf("\ngetal 0: ");
    		scanf("%d",&nil);
    
    		printf("\nsqroot(%d)=%f\n",nil,sqroot(nil,0.1F));
    	}
    
    }
    
    float sqroot(unsigned int aa, float eps){
    
    	float res1,res2;
    
    	printf(" aa=%f\n",(float)aa);
    	printf(" eps=%f\n",eps);
    
    	res1=absff((float)aa);
    	res2=absff(-1.0F*aa );
    	printf(" absff( (float)aa )=%f\n",res1);
    	printf(" absff( -1.0F*aa  )=%f\n",res2);
    
    	return absff((float)aa);
    
    }
    float absff(float ff){
    	printf(" ff=%f\n",ff);
    	return (ff>=0.0F?ff:-1.0F*ff);
    }
    I run this script inside the console and get this:
    Code:
    getal 0: 25
     aa=25.000000
     eps=0.100000
     ff=0.000000
     ff=0.000000
     absff( (float)aa )=16384.000000
     absff( -1.0F*aa  )=30720.000000
     ff=0.000000
    
    sqroot(25)=28672.000000
    
    getal 0:
    I would expect to see this though:
    Code:
    getal 0: 25
     aa=25.000000
     eps=0.100000
     ff=25.000000
     ff=25.000000
     absff( (float)aa )=25.000000
     absff( -1.0F*aa  )=25.000000
     ff=25.000000
    
    sqroot(25)=25.000000
    
    getal 0:

    Notes:
    --------

    - I know there's a math lib available, but I'm unfamiliar with it('s use).
    - I really don't understand why the float values (ie. (float)aa) aren't passed on to the absff() function, nor why I get this result:
    Code:
     ff=0.000000
     ff=0.000000
     absff( (float)aa )=16384.000000
     absff( -1.0F*aa  )=30720.000000
    If ff==0.000000 then why does the console tell me the result is 16384.000000??

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I copy-pasted your code and ran it. I got the result you were expecting and not the one you're getting. Which compiler are you using?
    EDIT: There is a library which you can include <math.h>. The square root function works as sqrt(any_number). There is also a pow(x,y) which calculates xraised to the power y. The important point is these two function do not return int values.
    math functions
    Last edited by BEN10; 12-20-2009 at 07:34 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    39
    I just created a new empty project, copy pasted the source and ran it. it ran like I wanted it to, but I still dont know what's wrong with the other build. I checked the compiler on my files and VS2008 tells me they all "Compile as C Code (/TC)".

    I'll be trying out some more...



    EDIT:

    So I created a new project with my code, and it works fine.
    Then I created my header file, moved some of the declarations etc into it, rebuild and it still works fine.
    Then I created a new source file, moved my methods into it, rebuild and suddenly I get the same problem as before.

    What am I doing wrong in this last step? this is my current source:

    file: main.c
    Code:
    #include "head1.h"
    
    int main()
    {
    	int first, second, nil=1;
    	while(nil){
    		printf("\ngetal 0: ");
    		scanf("%d",&nil);
    
    		printf("\nsqroot(%d)=%f\n",nil,sqroot(nil,0.1F));
    	}
    
    }

    file: head1.h
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float sqroot(unsigned int, float);
    float absff(float);
    file: f1.c
    Code:
    float sqroot(unsigned int aa, float eps){
    
    	float res1,res2;
    
    	printf(" aa=%f\n",(float)aa);
    	printf(" eps=%f\n",eps);
    
    	res1=fabs((float)aa);
    	res2=fabs(-1.0F*aa );
    	printf(" absff( (float)aa )=%f\n",res1);
    	printf(" absff( -1.0F*aa  )=%f\n",res2);
    
    	return absff((float)aa);
    
    }
    float absff(float ff){
    	printf(" ff=%f\n",ff);
    	return (ff>=0.0F?ff:-1.0F*ff);
    }
    Last edited by klmdb; 12-20-2009 at 07:56 AM.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    39
    nvm, found the problem; I didn't include the right headers in the second source file...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 02:46 PM
  2. Noob in need of help.
    By gec5741 in forum C++ Programming
    Replies: 18
    Last Post: 01-23-2009, 02:25 PM
  3. I'm a noob and I need help.
    By nifear4 in forum C Programming
    Replies: 17
    Last Post: 10-14-2008, 01:20 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. simple noob question: HWND, HDC etc.
    By freedik in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2003, 03:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21