#define an input number in C - c

I need to get the user to input a number into a program and then need to be able to use that number in many other parts functions in the program. Any way to do that?
Here is the code:
#include <stdio.h>
#include <math.h>
#define W 8.
#define H 4.
double ellipse(double);
typedef double (*DfD) (double);
double simpsons_int (DfD, double, double, int);
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}
double ellipse(double y)
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
double simpsons_int(DfD f, double y0, double y1, int n)
{
double y, sum, dy = (y1 - y0)/n;
sum = f(y1) + f(y0);
for(y = y0; y <= y1-dy; y += dy)
sum += 2.0 * f(y+dy) + 4.0 * f(y + dy/2);
return sum * dy / 6.0;
}
but I need H and W to be number that are input by the user not 8 and 4.

You can either pass it as argument of the function, or declare it as global variable. I'd rather use the first, depending on the application.
1) passing as parameter. Your function should be:
double ellipse(double y, double W, double H )
{
double x;
double A=W/2.;
double B=H/2.;
x=2*sqrt((1-(y*y)/(B*B))*(A*A));
return x;
}
And then you declare and scanf W and H within main()
2) Just declare W and H before main();
double W,H;
int main()
{
double len, w, h, volume;
printf("Please enter a length, width and height (in meters) of the an elliptical storage tank \n");
scanf("%lf %lf %lf", &len, &w, &h);
scanf("%lf %lf",&W,&H);
double a = h/2.*-1., r;
for (double depth=10; depth<=400; depth=depth+10)
{
r=a+(depth/100);
volume = len*simpsons_int(ellipse, a, r, 10000);
printf("depth is %.1f, volume is %f\n", depth, volume);
}
}

Preprocessor directives like #define must be known at compile-time. Think of them as constants: you can set them, but as soon as you run the program they're set in stone.
You should be using your variables to do this; you could possibly define w and h to be global variables, but better practice would be to pass them in as parameters to the ellipse function.

Related

Specifying float or double yet getting int value

#include <stdio.h>
int diameter_fn(int r)
{
return (2 * r);
}
void circumference_fn(int r)
{
float pie = 22 / 7;
float circum = (2 * pie * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r)
{
float pie = 22 / 7;
float area = (22 * r * r / 7);
printf(" & the Area = %f", area);
}
int main()
{
printf("\nName = Parth_Agrawal & UID = 22BCS10924\n");
int radius;
printf("Enter the Radius of Circle:\t\t");
scanf("%d", &radius);
printf("\nDiameter = %d", diameter_fn(radius));
circumference_fn(radius);
area_fn(radius);
return 0;
}
I want to calculate Circumference, diameter and area of circle using functions yet I get non-perfect Circumference and area values.
I already tried replacing the float with double, %f with %lf etc but I am always getting the Circumference and area in xxx.0000 format,I.e, similar to Int converted to float format.
Like the area for 4 unit radius is 50.27 but it is giving me 50.000000 which is too much annoying.
This is the Result I am getting
whereas this is the Result which I should get
... but it is giving me 50.000000 ...
OP is using integer math in many places where floating point math is needed.
void circumference_fn(int r) {
float pie = 22 / 7; // Integer math!!
float circum = (2 * pie * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r) {
float pie = 22 / 7; // Integer math!! pie not used
float area = (22 * r * r / 7);// Integer math!!
printf(" & the Area = %f", area);
}
Instead use FP math.
Scant reason to use float. Use double as the default FP type.
Rather than approximate π with 22/7, use a more precise value.
#define PIE 3.1415926535897932384626433832795
void circumference_fn(int r) {
double circum = (2 * PIE * r);
printf(", Circumference = %f", circum);
}
void area_fn(int r) {
double area = PIE * r * r;
printf(" & the Area = %f", area);
}
Other
All three functions should take a double argument and return a double.
Use "%g" for printing. It is more informative with wee values and less verbose with large ones.
#Shawn is right: you are using integer math to calculate Pi. You should #include <math.h> and use M_PI instead of trying to calculate it yourself.

How to insert a value to a variable while also trying to check if that value is zero

I'm having an assignment to complete where I need to find the zero body bias threshold voltage,based on the value of Vsb. Though this problem is based on the solution of another exercise, I give to you below some screenshots of what I need to calculate:
Where the Vthreshold formula is:
So I want to implement this:
My problem is that I don't know how to insert the Vsb value to the final formula in a way so that it can affect the final value of the zero body bias.The code that I've written returns only the value of the zero body bias based on the vthreshold only
Here's the code that I've written so far:
#include <stdio.h>
#include <math.h>
double vFB (double fgs, double B);
double vT (double A, double C);
double vA (double D);
double vSP (double J);
double gamwt (double A, double F);
double roulis( double H, double L);
double thres_volt(double vFB, double vSP, double vT, double vA);
double zero_body_bias_threshold_voltage(double thresh_volt, double gamma, double roulis);
int main(){
double eps0 = 8.854E-14; //Dielectric constant, void, [F/cm]
double epsox = 3.9*eps0; //Oxide dielectric constant, [F/cm]
double epsSi = 11.2*eps0; //Si dielectric constant, [F/cm]
double q = 1.6E-19; // expressed in Coulomb
double ni = 1.45E10; // intrincic carrier concentration expressed in cm^-3
double Na = 1.0E15; // expressed in cm^-3
double Ndpoly = 1.0E19; // expressed in cm^-3
double xox = 100.0; //Oxide thickness,[A]
double DI = 2.0E12; // number of implanted ions/cm^2 expressed in cm^-2
double kTq = 0.026; // expressed in Volt
double Qf = q*1.0E11; //expressed in C/cm2
double Vsb;
double Cox = (epsox)/(xox*1.0E-8); //Oxide capacitance per unit area, [F/cm2]
double A = 1.0/Cox;
double G = ((Na*Ndpoly))/((ni*ni));
double fgs = -((kTq)*log(G));
double B = A*Qf;
double T = Na/ni;
double J = 2*kTq*log(T);
double K = 2*q*epsSi*Na*J;
double C = sqrt(K);
double W = (q*DI)*(1.0/(Cox));
double D = W;
double U = 2*q*epsSi*Na;
double F = sqrt(U);
double ginomeno1 = J + Vsb;
double tetragwniki_tou_ginomenou1 = sqrt(ginomeno1);
double H = tetragwniki_tou_ginomenou1;
double ginomeno2 = J;
double tetragwniki_tou_ginomenou2 = sqrt(ginomeno2);
double L = tetragwniki_tou_ginomenou2;
double v1 = vFB(fgs, B);
double v2 = vSP(J);
double v3 = vT(A,C);
double v4 = vA(W);
double v5 = gamwt(A,F);
double v6 = roulis(H,L);
double v7 = thres_volt(v1,v2,v3,v4);
double v8 = zero_body_bias_threshold_voltage (v7,v5,v6);
double FocmtofFoum = 1.0E11; //Conversion factor of F/cm to fF/um
double Coxnew = Cox*FocmtofFoum;
printf ("Cox = %e [F/cm2]\n",Cox);
printf ("Coxnew = %f [fF/um]\n",Coxnew);
printf ("VFB = %f [V]\n", v1);
printf ("Vsp = %f [V]\n", v2);
printf ("Vt =%f [V]\n", v3);
printf ("Va = %f [V]\n", v4);
printf ("gamma = %f [V ^1/2]\n", v5);
printf ("Give me a number for Vsb..");
scanf ("%f", &Vsb);
if (Vsb==0)
ginomeno1 = J;
printf ("V_body_bias = %f [V]\n", v8);
getch();
return 0;
}
double vFB (double fgs, double B)
{
return (fgs-B);
}
double vT (double A, double C){
return (A*C);
}
double vA (double D){
return (+D);
}
double vSP (double J)
{
return (+J);
}
double gamwt (double A, double F)
{
return (A*F);
}
double roulis( double H, double L)
{
return (H-L);
}
double thres_volt(double vFB, double vSP, double vT, double vA)
{
return (vFB+vSP+vT+vA);
}
double zero_body_bias_threshold_voltage(double thresh_volt, double gamwt, double roulis)
{
return (thresh_volt + gamwt*roulis);
}

How to use exp and sqrt properties correctly

-use double precision
-use sqrt() and exponential function exp()
-use * to compute the square
-do not use pow()
I am getting values they are just not anything as to what I expected. I tried making them all signed but it didn't change anything and I've tried printing out with 12 decimal places and nothing seems to be working.I have linked the math library and defined it as well.
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double raise = 1.0/2.0*((x-mu)/sigma);
double func1 = func * exp(raise);
double comp_func = (func1 * func1);
return comp_func;
}
int main(void)
{
// create two constant variables for μ and σ
const double sigma, mu;
//create a variable for x - only dynamic variable in equation
unsigned int x;
//create a variable for N values of x to use for loop
int no_x;
//scaniing value into mu
printf("Enter mean u: ");
scanf("%lf", &mu);
//scanning value into sigma
printf("Enter standard deviation: ");
scanf("%lf", &sigma);
//if sigma = 0 then exit
if(sigma == 0)
{
printf("error you entered: 0");
exit(0);
}
//storing number of x values in no_x
printf("Number of x values: ");
scanf("%d", &no_x);
//the for loop where i am calling function normal N times
for(int i = 1; i <= no_x; i++)
{
//printing i for the counter in prompted x values
printf("x value %d : ", i);
// scanning in x
scanf("%lf", &x);
x = normal(x,sigma,mu);
printf("f(x) = : %lf.12", x);
printf("\n");
}
return 0;
}
C:>.\a.exe
Enter mean u: 3.489
Enter std dev s: 1.203
Number of x values: 3
x value 1: 3.4
f(X) = 0.330716549275
x value 2: -3.4
f(X) = 0.000000025104
x value 3: 4
f(X) = 0.303015189801
But this is what I am receiving
C:\Csource>a.exe
Enter mean u: 3.489
Enter standard deviation: 1.203
Number of x values: 3
x value 1 : 3.4
f(x) = : 15086080.000000
x value 2 : -3.4
f(x) = : 15086080.000000
x value 3 : 4
f(x) = : 1610612736.000000
Insert these lines:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Change:
const double sigma, mu;
to:
double sigma, mu;
Change:
unsigned int x;
to:
double x;
Replace the definition of the normal function with:
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double t = (x-mu)/sigma;
return func * exp(-t*t/2);
}
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
#include<math.h>
#include<stdio.h>
#include <stdlib.h>
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double t = (x-mu)/sigma;
return func * exp((-0.5*t)* t);
}
I Finally got this code above working after tweaking with it literally all day lol, C math can be rather tricky, thank you for the help above as well.

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.

Why am I getting an output of 0

Why am I getting an output of 0? I think there's something wrong about my angle conversion and possibly my equation, yet fiddling around with it and moving some stuff always gives me the same result.
My goal is to write a C code that will compute the angle θ for any two given vectors u and v.
#include <stdio.h>
#include <math.h>
int main()
{
double ux, uy;
double vx, vy;
double inner_product(double vx, double vy, double ux, double uy);
double v;
double u;
double i;
double x;
double k;
double pi;
double angle;
double p;
ux = 1.0;
uy = 1.0;
vx = 1.0;
vy = 1.0;
printf ("input value for ux\n", ux);
scanf_s ("%f", &ux);
printf ("input value for uy\n", uy);
scanf_s ("%f", &uy);
printf ("input value for vx\n", vx);
scanf_s ("%f", &vx);
printf("input value for vy\n", vy);
scanf_s ("%f", &vy);
u = ux * vx;
v = uy * vy;
i = u * v;
x = u * u;
k = v * v;
pi = acos(-1.0);
p = acos(i / (sqrt(x * k)));
angle = ((p * 180) / pi); //converting from radians to degrees
printf("%f", angle);
return;
}
The math error is in the following:
p = acos(i / (sqrt(x * k)));
Change it to:
p = acos((ux*vx + uy*vy) / (sqrt(ux*ux + uy*uy) * sqrt(vx*vx + vy*vy)));
That's just the dot product divided by the two lengths.
You equation is wrong.
The correct is:
|U|=√[Ux^2+Uy^2]
|V|=√[Vx^2+Vy^2]
U*V=(Ux,Uy)(Vx,Vy)=Ux*Uy+Vx*Vy
cos=U*V/[|U|*|V|]
Not %f but %lf specifier have to be used to read data having type double via scanf() family.
Note that %f should be used to print data having type double via printf() family because float will be automatically converted to double for variable number arguments.

Resources