Thread: Function not working

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17

    Function not working

    Hello,

    First my code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    double reken (double vplus, double vth, double rxbeta, double voutold, double nextvin)
    {
    	int i=1;
    	double verschilvout=1;
    	double voutnew;
    			
    	while ((i <= 51) && (verschilvout>0.000001) && (voutold > 0))
    	{
    		voutnew = vplus -0.5*rxbeta*((nextvin-vth)*(nextvin-     vth))*tanh(2*voutold/(nextvin-vth));
    		verschilvout = abs(voutold - voutnew);
    		voutold = voutnew;
    		i=i++;
    	}
    	return voutold;
    }
    
    void main (void)
    {
    	double vplus;
    	double vth;
    	double rxbeta;
    	double nextvin;
    	double voutold;
    	double voutprint;
    	int u=0;
    				
    	printf("Solving vout for vin from 0 to V+\n");
    	printf("Enter Vplus, Vth, Rxbeta :\n");
            scanf ("%lf %lf %lf", &vplus, &vth, &rxbeta);
    	printf("Now enter an initial guess for vout:\n");
    	scanf("%lf", &voutold);
    	printf("\n");
    
    	for (u=0;u<=11;u++)
    	{
    		nextvin = vplus*(u/12);
    		voutprint = reken(vplus, vth, rxbeta, voutold, nextvin);
    		
    		printf("\n");
    		printf("Solution: vout = %.15lf \n", voutprint);
    	}
    	
    	
    	
    }
    It is supposed to give vout for values of nextvin between 0 and vplus in steps of vplus/12. So I put a function "reken" in a for loop that runs 12 times. In the function there is an iteration that should stop if it exceeds 50 times or the values differ less than 0.000001 or the value is negative. Then it should return the end value to the for loop and print it.

    This code does compile but when i run it it gives me the same answer 12 times. What is wrong?

    Thanks for your help,
    Tom

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    First, the warnings, that you should fix:
    Code:
    voutprint.c: In function ‘reken’:
    voutprint.c:13: warning: implicit declaration of function ‘abs’
    voutprint.c:15: warning: operation on ‘i’ may be undefined
    voutprint.c: At top level:
    voutprint.c:21: warning: return type of ‘main’ is not ‘int’
    Secondly, what is your code supposed to accomplish?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    I work with visual c++ and I don't get these warnings. To be honest I dont really know how to solve the warnings you wrote.

    The program is supposed to solve non-lineair equations numerically.
    The function should find a limit for vout by running the while loop till voutold-voutnew is less than 0.000001 or vout becomes negative or the loop runned 50 times. This vout answer should be printed.
    I want to print 12 different vouts for values of nextvin between 0 and vplus (vplus/12). Thats why i made the for loop.

    Since i'm new to using function maybe i messed up with variables or calling the function. I checked it already but maybe you see it.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Tom, the mistake you made, was in putting all the terms and equations into your function, and *THEN* seeing that it doesn't work.

    No da!

    What you need to do is remark out several of those lines of code, and several of those variables, and then either put in print statements, or (better yet), follow their values in your IDE, as you step through the code.

    When you code, you do a little, and test it, do a little more, and test it. You never ever want to be looking at 20, 40, 60 lines of untested code, with 15 different variables.

    At this point, you're the best person to debug this program, because you're the person who knows these variables the best.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Location
    Netherlands
    Posts
    17
    I found it! Should be u/12.0. With 12 it returns 0.00000 everytime.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>voutprint.c:13: warning: implicit declaration of function ‘abs’
    You are calling a function without a proper declaration of the function. If it is a custom function you made, then make sure to write a prototype for it and include it in the calling code. If it is a standard function, then make sure you include the proper standard header.

    >>voutprint.c:15: warning: operation on ‘i’ may be undefined
    This is due to the i = i++ line. You are assigning and modifying the same variable, but it is not guaranteed to work since compilers can do any part of the line first, perhaps resulting in different results on different compilers. But i++ already increments the variable, so there is no need for any assignment.

    >>voutprint.c:21: warning: return type of ‘main’ is not ‘int’
    This one is super easy. It is just as the warning says clearly. The return type of main is not int. So make it return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    hi!...can u help me for this problem?...

    Code:
    pls i really need ur help, nobody can help me in making this prigram..tnx a lot...
    
    Maximum Sub-Array
    For an array of n integers, the maximum sub-array can be defined as that subset of consecutive
    elements of the array, which on addition, yield the maximum sum. For example, if there is an array
    A = {8, -10, 7, 4, -3, 5, -9, 2, -4, 3 } ,
    then the maximum sub-array whose elements add up to produce maximum sum is { 7, 4, -3, 5 }
    with the sum being 13.
    Task
    Given a set of arrays, your task is to find out their respective maximum sub-arrays, and report the
    sum of the elements of each sub-array
    Input
    The first line of the input will be a positive integer t (1 <= t <= 100000). t lines will follow
    representing each of the t test cases. Each of these lines will contain 25 integers, the elements of
    each array.
    Output
    The output should be of t lines, with exactly one number on each line, the sum of the elements of
    the corresponding array's maximum sub-array.
    Example
    Sample input:
    3
    8 20
    22 97
    38
    15 0 40 0 75 4 50
    7 78
    52 12 50
    77 91 80
    49 49
    99 40
    17
    18
    8 57 60
    87 17 40 98 43
    69 48 4
    56 62 0 81 49
    31 73
    55
    79 14
    29 93
    7
    40 67 53 88
    30 3 49
    13 36 65
    52 70
    95 23
    4
    60 11
    42 69
    24 68 56 1 32
    5
    Sample output:
    214
    516
    261

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Man, don't hijack threads.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are some few things you should learn first.
    First off, do please start a new thread about your problem. Do not hijack other threads.
    Secondly, do NOT post your message inside code tags. ONLY post code inside the code tags.
    Thirdly, simply saying "help me do X" and dumping the entire problem on us is not going to help. You need to explain what it is you do not understand, so we can better help. We are not going to write the code for you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM