# Thread: Function not working

1. ## 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. 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. 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. 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. I found it! Should be u/12.0. With 12 it returns 0.00000 everytime.

6. >>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.

7. ## 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. Man, don't hijack threads.

9. 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.

Popular pages Recent additions