How to get second output as NO - c

I am writing code to find if a person breaks world record in running race.
The first input is number of test cases( t ), then the next inputs are conditions faced while running ( k1, k2, k3, v)
If final speed is equal to or greater than 9.58 the output should be no.
While if final speed is less than 9.58 the output should be yes.
But in following test case I am getting wrong output for second case:
3
1.0 1.0 1.0 10.45
1.0 1.0 1.0 10.44
1.0 1.0 0.9 10.44
I get Output :
YES
YES
NO
I want output :
YES
NO
NO
int main(void)
{
int t;
scanf("%d", &t);
float k1, k2, k3, v, speed_with_fact[t], final_speed[t];
for (int i = 0; i < t; i++)
{
scanf("%f %f %f %f", &k1, &k2, &k3, &v);
speed_with_fact[i] = 100 / (k1 * k2 * k3 * v);
speed_with_fact[i] = (int)(speed_with_fact[i] * 100 + 0.5);
// printf("%f\n " ,speed_with_fact[i]);
final_speed[i] = speed_with_fact[i] / 100;
// printf("%f\n " ,final_speed[i]);
}
for (int j = 0; j < t; j++)
{
//printf("%f\n " ,final_speed[j]);
if (final_speed[j] < 9.58)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}

The value of final_speed[j] being 9.58 is an aproximation, if you test print it with printf("%.10f\n " ,final_speed[j]); you'll see that the value is not exactly 9.58 (I got 9.5799999237). Floating-point arithmetic is very inexact and is the source of many problems like your own.
In your particular case, since the type of the literal 9.58 will be double by default it means that you are comparing float with double, this is fixed if you use double variables.
You could force a float literal using 9.58f as very accurately pointed out by MOhem and it would also work, but using double is advised.
int main(void)
{
int t;
scanf("%d", &t);
double k1, k2, k3, v, speed_with_fact[t], final_speed[t]; //<-- here
for (int i = 0; i < t; i++)
{
scanf("%lf %lf %lf %lf", &k1, &k2, &k3, &v); //<-- here
speed_with_fact[i] = 100 / (k1 * k2 * k3 * v);
//...
}
Another good suggestion by MOehm's will also work and is a good option, maybe even better as it simplifies the comparison to int vs int.

Related

getPositiveValues won't return the values

What is not working:
In the code below, the values input in scanf under getPositiveValue will not return. They return as 0 no matter what the input is.
I have no clue how to get around this. Can someone show me why it is not working?
What I have tried:
I tried using return CHAN; and even return CHAN.n; and all the other members but that did not work.
My code:
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines
typedef struct CHANNEL_ //Structure CHANNEL
{
char name[9];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;
double computeVelocity(CHANNEL, double);
int main(void)
{
CHANNEL CHAN;
void getPositiveValue(CHANNEL);
void displayTable(CHANNEL);
//Function declarations
printf("Enter the name of the channel: ");
fgets(CHAN.name, 9, stdin);
getPositiveValue(CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}
void getPositiveValue(CHANNEL CHAN)
{
int Flag; //sentinel
do
{
Flag = FALSE;
printf("Give the coefficient for roughness, slope, width, and maxdepth: ");
scanf("%lf %lf %lf %lf", &CHAN.n, &CHAN.S, &CHAN.B, &CHAN.D);
if(CHAN.n < 0 || CHAN.S < 0 || CHAN.B < 0 || CHAN.D < 0) //sentinel checkpoint
{
Flag = TRUE;
printf("The values must be positive.\n");
}
} while(Flag == TRUE);
}
void displayTable(CHANNEL CHAN)
{
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N]; //N is used to avoid magic numbers when defining array size
double arrayvelocity[N]; //N is used to avoid magic numbers when defining array size
int i; //using separate integers for the two different arrays just so it looks better and less confusing
for ( i = 0; i < N; i++)
{
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}
double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2 / 3));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));
return U;
}
The input problem you are having is because of the fact that functions are call by value in C. This means that when you pass a struct to a function, it is a copy of the struct that is worked with in the function, not the original. Any changes made to the struct within the getPositiveValue() function are not visible once control returns to main().
To fix this problem, pass a pointer to the structure. Use the -> operator to dereference the pointer and access members in one shot. Here is a modified version of your code. I also took the liberty of moving your function declarations to the top of the program.
There is also an error in the call to the pow() function found in computeVelocity():
U = pow(U, (2 / 3));
should be:
U = pow(U, (2.0 / 3.0));
The expression 2 / 3 performs integer division, with the result zero, so after this call to pow(), U is always 1. This can be easily fixed by forcing floating point division, as in the second line above.
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines
typedef struct CHANNEL_ //Structure CHANNEL
{
char name[9];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;
double computeVelocity(CHANNEL, double);
void getPositiveValue(CHANNEL *);
void displayTable(CHANNEL);
int main(void)
{
CHANNEL CHAN;
printf("Enter the name of the channel: ");
fgets(CHAN.name, 9, stdin);
getPositiveValue(&CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}
void getPositiveValue(CHANNEL *CHAN)
{
int Flag; //sentinel
do
{
Flag = FALSE;
printf("Give the coefficient for roughness, slope, width, and maxdepth: ");
scanf("%lf %lf %lf %lf", &CHAN->n, &CHAN->S, &CHAN->B, &CHAN->D);
if(CHAN->n < 0 || CHAN->S < 0 || CHAN->B < 0 || CHAN->D < 0) //sentinel checkpoint
{
Flag = TRUE;
printf("The values must be positive.\n");
}
}while(Flag == TRUE);
}
void displayTable(CHANNEL CHAN)
{
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N]; //N is used to avoid magic numbers when defining array size
double arrayvelocity[N]; //N is used to avoid magic numbers when defining array size
int i; //using separate integers for the two different arrays just so it looks better and less confusing
for ( i = 0; i < N; i++)
{
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}
double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2.0 / 3.0));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));
return U;
}
Sample program interaction:
Enter the name of the channel: chan
Give the coefficient for roughness, slope, width, and maxdepth: 0.035 0.0001 10 4.2
Channel data for chan
Coefficient of roughness: 0.035000
Slope: 0.000100
Width: 10.000000
Maximum depth: 4.200000
Depth Average Velocity
0.168000 0.917961
0.336000 0.566077
0.504000 0.423161
0.672000 0.342380
0.840000 0.289368
1.008000 0.251450
1.176000 0.222759
1.344000 0.200172
1.512000 0.181859
1.680000 0.166669
1.848000 0.153840
2.016000 0.142843
2.184000 0.133301
2.352000 0.124935
2.520000 0.117535
2.688000 0.110939
2.856000 0.105020
3.024000 0.099677
3.192000 0.094829
3.360000 0.090410
3.528000 0.086363
3.696000 0.082644
3.864000 0.079214
4.032000 0.076040
4.200000 0.073095
There are many compiler error in your code. Here is my first try to fix it
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
#define N 25 //number of lines
typedef struct CHANNEL_ {
char name[50];
double n;//roughness coefficient
double S;//channel slope
double B;//width
double D;//maxDepth
} CHANNEL;
double computeVelocity(CHANNEL, double);
void getPositiveValue(CHANNEL);
void displayTable(CHANNEL);
int main(void) {
CHANNEL CHAN;
printf("Enter the name of the channel: ");
fgets(CHAN.name, 50, stdin);
getPositiveValue(CHAN);
printf("Channel data for %s\n Coefficient of roughness: %lf\n Slope: %lf\n Width: %lf\n Maximum depth: %lf\n", CHAN.name, CHAN.n, CHAN.S, CHAN.B, CHAN.D);
printf("Depth Average Velocity\n");
displayTable(CHAN); //function call to display the table with values
}
void getPositiveValue(CHANNEL CHAN) {
int Flag; //sentinel
do {
Flag = FALSE;
printf("Give the coefficient for roughness: \n Give the slope: \n Give the channel width: \n Give the maximum depth of the channel: ");
scanf("%lf %lf %lf %lf", &CHAN.n, &CHAN.S, &CHAN.B, &CHAN.D);
if(CHAN.n < 0 || CHAN.S < 0 || CHAN.B < 0 || CHAN.D < 0) {
Flag = TRUE;
printf("The values must be positive.\n");
}
} while(Flag == TRUE);
}
void displayTable(CHANNEL CHAN) {
double increment = CHAN.D/N;
double H = 0; //depth
double arraydepth[N];
double arrayvelocity[N];
int i;
for ( i = 0; i < N; i++) {
H += increment;
arrayvelocity[i] = computeVelocity(CHAN, H);
arraydepth[i] = H;
printf("%lf %lf\n", arraydepth[i], arrayvelocity[i]);
}
}
double computeVelocity(CHANNEL CHAN, double H)
{
double U;
U = CHAN.B / H;
U = U / (CHAN.B + (2 * H));
U = pow(U, (2 / 3));
U = U / CHAN.n;
U = U * (sqrt(CHAN.S));
return U;
}
The first error would be struct definition. In C, you can define the struct and at the same time define a variable. But you should not use the same name to confuse yourself and the compiler. Also you need to understand void function does not return a value and cannot be on the right side of an = expression.
Use typedef can save you to type struct keyword each time you need it. You also need to use %s to output a string. Also typos here and there.

Exponential Taylor Series

This is the code I have so far, which is a little messy since I am still trying to figure out how to set it up, but I cannot figure out how to get the output. This code is supposed to take a Taylor Series polynomial of an exponential, and check the amount of iterations it takes to get the approximation.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*Prototype for functions used*/
double factorial (int);
int main()
{
double input = 0;
double exp_val;
double delta = 1;
int f =0;
int n = 0;
double taylor;
int total;
printf("Plese enter the exponent to check for convergence:\n");
scanf("%lf", &input);
exp_val = exp(input);
printf(" # Iter e^X Sum Diff\n");
printf("---- ------ ------- ----- --------");
while(delta > 0.00001)
{
f = factorial(n);
taylor = ((pow(input,n))/ f);
delta = (exp_val - taylor);
printf("%d %f %f %f/n", (n+1), exp_val, taylor, delta);
n++;
}
system("pause");
}
double factorial (int n)
{
int r = 0;
int sum = 1;
int total = 0;
if (n == 0)
return total =1;
else
{
for(r; r<n; r++)
{
sum = sum * r;
total = sum + 1;
}
return total;
}
}
Here, I have fixed it, without changing your approach, except for the parts I really had to. One thing we have to clarify before the code is how Taylor Polynomials are made. It is not the first term plus the nth term, rather the sum of all terms from the first term till the nth term. So you definitely have to increase the taylor variable by the current nth term instead of the other way.
Here's the code, with brief comments in it as the explanation:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*Prototype for functions used*/
unsigned long long factorial( int ); // <-- made it return unsigned long long
int main( )
{
double input = 0;
double exp_val;
double delta = 1;
unsigned long long f = 0; // <-- changed its type
int n = 0;
double taylor = 0; // <-- initialized with 0
printf( "Plese enter the exponent to check for convergence:\n" );
scanf( "%lf", &input );
exp_val = exp( input );
printf( " # e^X Sum Diff\n" ); // <-- made some cosmetic changes
printf( "--- --------- --------- ---------\n" ); // <-- added \n
while ( delta > 0.00001 )
{
f = factorial( n );
taylor += ( ( pow( input, n ) ) / f ); // += instead of =
delta = ( exp_val - taylor );
printf( "%2d %12f %12f %12f\n", ( n + 1 ), exp_val, taylor, delta ); // <-- replaced / with \ before the n
n++; // and made some edits to make it look better
}
system( "pause" );
return 0; // <-- better add this
}
unsigned long long factorial( int n ) // <-- made it return unsigned long long
{
int r = 0;
unsigned long long sum = 1; // <-- changed its type
if ( n == 0 )
return sum; // <-- this
else
{
for ( r; r<n; r++ )
{
sum *= r + 1; // <-- changed this
}
return sum; // <-- and this
}
}
You have to keep in mind that you may not input too high values to it. Anything higher than input == 4 kind of breaks it, because, you see, even with 4, it can reduce the error delta beneath the threshold first only with the 19th cycle. The programme seemingly fails with n == 5 due to inaccurate calculation of pow( 5, 21 ) / factorial( 21 ) when n reaches 21:
0.000034 // the result this programme finds
0.0000093331055943447405008542892329719 // the result Calculator finds
So, yeah... If you want this programme to work with bigger input values, you'll need a better approach. Not calculating the nth term from scratch and calculating it from the (n - 1)th term instead could help until somewhat bigger input values, as the others had said.
A couple issue:
Change int r = 0; ... for(r; r<n; r++) to int r; ... for(r=1; r<=n; r++) or int r = 1; ... for(; r<=n; r++)
Change printf("%d %f %f %f/n" to printf("%d %f %f %f\n" Add \n
Change "... --------" to "... --------\n"
Change delta = (exp_val - taylor); to delta = fabs(exp_val - taylor);
Change to double taylor = 0.0; Initialize it.
Change to taylor += ((pow(input,n))/ f); Note: +=
Minor: "Please" not "Plese".
Minor: Drop int total;

Numerical Differentiation

How can I calculate the numerical second derivative of a function involving an exponential and a singularity at infinity. Unfortunately, the numerical derivative by Ridder's methods provided in "Numerical Recipes in C" can only calculate the first derivative (It requires analytical expression of the function beforehand.) Furthermore I have tried Chebyshev approximation and differentiating the function afterwards but the values given were way off the actual values. I have also tried some finite difference algorithms provided in a mathematical paper yet they were error prone too. The function is e^(x/2) / x^2. I would appreciate any help on the matter.
Thanks in advance
Latest Edit: The issue was solved the FADBAD libraries available in C++ did an extremely good job. They are available via http://www.fadbad.com/fadbad.html
EDIT:
// The compilation command used is given below
// gcc Q3.c nrutil.c DFRIDR.c -lm -o Q3
#include <stdio.h>
#include <math.h>
#include "nr.h"
#define LIM1 20.0
#define a -5.0
#define b 5.0
#define pre 100.0 // This defines the pre
/* This file calculates the func at given points, makes a
* plot. It also calculates the maximum and minimum of the func
* at given points and its first and second numerical derivative.
*/
float func(float x)
{
return exp(x / 2) / pow(x, 2);
}
int main(void)
{
FILE *fp = fopen("Q3data.dat", "w+"), *fp2 = fopen("Q3results.dat", "w+");
int i; // Declaring our loop variable
float x, y, min, max, err, nd1, nd2;
// Define the initial value of the func to be the minimum
min = func(0);
for(i = 0; x < LIM1 ; i++)
{
x = i / pre; // There is a singularity at x = 0
y = func(x);
if(y < min)
min = y;
fprintf(fp, "%f \t %f \n", x, y);
}
fprintf(fp, "\n\n");
max = 0;
for(i = 0, x = a; x < b; i++)
{
x = a + i / pre;
y = func(x);
nd1 = dfridr(func, x, 0.1, &err);
//nd2 = dfridr((*func), x, 0.1, &err);
fprintf(fp, "%f \t %f \t %f \t %f \n", x, y, nd1);
if(y > max)
max = y;
}
fprintf(fp2, "The minimum value of f(x) is %f when x is between 0 and 20. \n", min);
fprintf(fp2, "The maximum value of f(x) is %f when x is between -5 and 5. \n", max);
fclose(fp);
fclose(fp2);
return 0;
}
EDIT: Chebyshev
// The compilation command used is given below
//gcc Q3.c nrutil.c CHEBEV.c CHEBFT.c CHDER.c -lm -o Q3
#include <stdio.h>
#include <math.h>
#include "nr.h"
#define NVAL 150 // Degree of Chebyshev polynomial
#define LIM1 20.0
#define a -5.0
#define b 5.0
#define pre 100.0 // This defines the pre
/* This file calculates the func at given points, makes a
* plot. It also calculates the maximum and minimum of the func
* at given points and its first and second numerical derivative.
*/
float func(float x)
{
return exp(x / 2) / pow(x, 2);
}
int main(void)
{
FILE *fp = fopen("Q3data.dat", "w+"), *fp2 = fopen("Q3results.dat", "w+");
int i; // Declaring our loop variable
float x, y, min, max;
float nd1, nd2, c[NVAL], cder[NVAL], cder2[NVAL];
// Define the initial value of the func to be the minimum
min = func(0);
for(i = 0; x < LIM1 ; i++)
{
x = i / pre; // There is a singularity at x = 0
y = func(x);
if(y < min)
min = y;
fprintf(fp, "%f \t %f \n", x, y);
}
fprintf(fp, "\n\n");
max = 0;
// We make a Chebyshev approximation to our function our interval of interest
// The purpose is to calculate the derivatives easily
chebft(a,b,c,NVAL,func);
//Evaluate the derivatives
chder(a,b,c,cder,NVAL); // First order derivative
chder(a,b,cder,cder2,NVAL); // Second order derivative
for(i = 0, x = a; x < b; i++)
{
x = a + i / pre;
y = func(x);
nd1 = chebev(a,b,cder,NVAL,x);
nd2 = chebev(a,b,cder2,NVAL,x);
fprintf(fp, "%f \t %f \t %f \t %f \n", x, y, nd1, nd2);
if(y > max)
max = y;
}
fprintf(fp2, "The minimum value of f(x) is %f when x is between 0 and 20. \n", min);
fprintf(fp2, "The maximum value of f(x) is %f when x is between -5 and 5. \n", max);
fclose(fp);
fclose(fp2);
return 0;
}
That function is differentiable so using a numeric method is likely not the best. The second derivative is:
6*exp(x/2)/(x^4)-2*exp(x/2)/x^3 + exp(x/2)/(4*x^2)
The above can be simplified of course to speed up computation. Edit: had original formula wrong the first time.
If you want a 100% numeric approach then look at the numerical recipes for a cublic spline interpolation (Charter 3.3). It will give you the 2rd derivative at any location.
call spline() with x and y values to return the 2nd derivatives in y2. The second derivative varies linearly within each interval. So if for example you have
x y y2
0 10 -30
2 5 -15
4 -5 -10
then the 2nd derivative at x=1 is y2=-22.5 which is in-between -30 and -15.
you can also make a new splint() function to return the 2nd derivative a*y2a[i]+b*y2a[i+1]

natural logarithm in C using for loop

I can't understand how the following program creates a natural logarithm (I wonder if it does). I found it on a blog. If it does not create a natural logarithm how would I make one?
void main()
{
int x,i,j;
float sum=0,power=1;
printf("enter x for sum upto 7th term: ");
scanf("%d",&x);
for(i=1;i<=6;i++)
{
power=1;
for(j=0;j<=i;j++)
{
power = power * ((x-1.0)/2.0);
}
sum = (1.0/2) * power + sum;
}
sum=sum + (float)(x-1.0)/x;
printf("%f",sum);
}
The program might be trying to calculate the natural logarithm, but it has lots of problems. Corrections below keeping the OP style
The formula for ln(x) when (x > 0.5) follows
ln(x) = (x-1)/x + (1/2)((x-1)/x)^2 + (1/3)((x-1)/x)^3 + ...
void main() {
int i, j;
float sum = 0.0f;
float power;
float x;
printf("enter x for sum up to 7th term: ");
scanf("%f", &x); // let x be a float rather than limited to int
for (i = 1; i <= 7; i++) { // do all 7 terms here
power = 1.0f;
for (j = 0; j < i; j++) {
power = power * ((x - 1.0f) / x); // need to /x not 2.0
}
sum += (1.0f / i) * power; // need to /i not 2.0
}
//sum = sum + (float) (x - 1.0f) / x; not needed as done in above 1st iteration
printf("ln(%f) = \n%f\n%lf\n", x, sum, log(x));
}
"Taylor Series Centered at 1" in the below link appears wrong as I suspect its terms should have alternating signs.
http://math2.org/math/expansion/log.htm

Problem finding the local maximum of a function in C

I'm designing an algorithm to define a simple method able to find the local maximum of a function f (x) given in an interval [a, b]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.141592653
float funtion_(float a, float x){
float result=0;
result = a * (sin (PI*x));
return result;
}
int main (){
double A = 4.875; //average of the digits of the identification card
double a = 0.0, b =1.0; //maximum and minimum values of the interval [a, b]
double h=0;
double N;
double Max, x;
double sin_;
double inf;
printf ("input the minux value: ");
scanf ("%lf", &inf);
printf ("input the N value: ");
scanf ("%lf", &N);
h= (b-a)/N;
printf("h = %lf\n", h);
x=a-h;
Max = -inf;
do {
x = x+h;
sin_ = funtion_(A, x);
if (sin_>=Max){
Max = sin_;
}
}while (x==b);
printf ("Maximum value: %lf.5", Max);
return 0;
}
The algorithm implements the function f (x) = A * sin (pi * x), where A is the average of the digits of my ID, and inf variable is assigned a number sufficiently greater than the values ​​reached by the function in the interval [a, b] = [0.1].
The algorithm must find the local maximum of the function but it is the maximum returns always zero. do not understand why. What problem may be the logic of my solution?, this problem can be solved by this simple algorithm or some optimization by backtracking is necessary ? Thanks for your responses.
Several problems with this code; probably the most glaring is:
int a = 0, b = 1;
float Max, x;
/* ... */
do {
/* ... */
} while (x == b);
You cannot compare an int and a float for equality. It might work once in a great while due to dumb luck :) but you cannot expect this code to function reliably.
I strongly recommend changing all your int variables to double, all your float variables to double, and all the scanf(3) and printf(3) calls to match. While you can combine different primitive number types in one program, and even in one expression or statement, subtle differences in execution will take you hours to discover.
Furthermore, comparing floating point formats for equality is almost never a good idea. Instead, compare the difference between two numbers to a epsilon value:
if (fabs(a-b) < 0.001)
/* consider them equal */
You might want to scale your epsilon so that it matches the scale of your problem; since float really only supports about seven digits of precision, this comparison wouldn't work well:
if (fabsf(123456789 - 123456789.1) < 0.5)
/* oops! fabsf(3) used to force float */
/* and float can't tell the difference */
You might want to find a good introduction to numerical analysis. (Incidentally, one of my favorite classes back in school. :)
update
The core of the problem is your while(x == b). I fixed that and a few smaller problems, and this code seems to work:
#include
#include
#include
#define PI 3.141592653
float funtion_(float a, float x)
{
float result = 0;
result = a * (sin(PI * x));
return result;
}
int main()
{
float A = 4.875; //average of the digits of the identification card
float a = 0.0, b = 1.0; //maximum and minimum values of the interval [a, b]
float h = 0;
float N;
float Max, x;
float sin_;
float inf;
printf("\ninput the inf value: ");
scanf("%f", &inf);
printf("\ninput the N value: ");
scanf("%f", &N);
h = (b - a) / N;
x = a - h;
Max = -inf;
do {
x = x + h;
sin_ = funtion_(A, x);
if (sin_ >= Max) {
Max = sin_;
printf("\n new Max: %f found at A: %f x: %f\n", Max, A, x);
}
} while (x < b);
printf("Maximum value: %.5f\n", Max);
return 0;
}
Running this program with some small inputs:
$ ./localmax
input the inf value: 1
input the N value: 10
new Max: 0.000000 found at A: 4.875000 x: 0.000000
new Max: 1.506458 found at A: 4.875000 x: 0.100000
new Max: 2.865453 found at A: 4.875000 x: 0.200000
new Max: 3.943958 found at A: 4.875000 x: 0.300000
new Max: 4.636401 found at A: 4.875000 x: 0.400000
new Max: 4.875000 found at A: 4.875000 x: 0.500000
Maximum value: 4.87500
$
You are doing your calculations, in particular the initialisation of h, with integer arithmetic. So in the statement:
h = (b-a) / N;
a, b, and N are all integers so the expression is evaluated as an integer expression, and then converted to a float for assignment to h. You will probably find that the value of h is zero. Try adding the following line after the calculation of h:
printf("h = %f\n", h);
After you've fixed that by doing the calculations with floating point, you need to fix your while loop. The condition x = b is definitely not what you want (I noticed it was originally x == b before your formatting edit, but that's not right either).
Should the while condition be: while(x <= b)
while (x = b);
There is no way to exit the loop. b is always 1.

Resources