This was the instructions: Write a program that starts out asking the user for the mean u and
standard deviation s for the normal distribution
(see the wiki article http://en.wikipedia.org/wiki/Normal_distribution
The program then asks for an N, and then asks for N values x. For each x it
writes out f(x) to the screen.
Note that the program asks the user for u, s, and N just once. After that it
asks for N values for x, one by one. After each value x it writes out the
corresponding value of the function.
Of course, use double precision for this and use the square root and
exponential function from the standard math library.
This is my code so far but I can't get the N to work.
#include <stdio.h>
#define PI 3.141592653589793238462643383
#define E 2.7182818284590452353602874713526624977572470937
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s,N,x,math1, math2, math3,n,;
printf("Enter Mean: \n");
scanf("%d", &u);
printf("Enter Deviation: \n");
scanf("%d", &s);
n=1/2;
math1 =1/(u*sqrt(2*PI));
math2= (x-u)/s * (x-u)/s;
math3= E * exp(n);
x = math1 * exp(math3)*exp(math2);
printf("%d \n", x);
system("Pause");
}
n=1/2;
This will be equal to 0, since 1 is an integer, 2 is an integer and 1 divided by 2 is 0 in integer math.
Try 1.0/2.0
Make sure every other division has a double on one or both sides, otherwise it will be done as integer math
Related
I am making a triangle area calculator, but it only show 0 instead of answer, It must be the formula 1/2 * ab sin c , can someone tell me what should I change to make it work.
#include <stdio.h>
#include <math.h>
float main ()
{
float a,b,c,pi ;
printf("Enter a : ");
scanf("%f",&a);
printf("Enter b : ");
scanf("%f",&b);
printf("Enter c :");
scanf("%f",&c);
printf("%f\n",(a*(1/2)*b*(c*pi)/180));
return 0;
}
The sub-expression (1/2) is always equal to 0 due to the integer arithmetic. You need to write for example (1.0f/2).
Apart from this the variable pi is not initialized
float a,b,c,pi ;
Also according to the C Standard the function main without parameters shall be declared like
int main( void )
My professor uses this site (e-olymp.com) that automatically grades your solution in %
For this homework we have to use pointers to solve these tasks. I had a problem with this one:
The array of real numbers is given. Calculate the twice value of the minimum element in array.
Input
First line contains the number n (n ≤ 100) of elements in array. Second line contains n real numbers - the elements of array. Each value does not exceed 100 by absolute value.
Output
Print the twice value of the minimum element in array with 2 decimal digits.
My solution, works perfectly fine in compiler but gives 0%, idk where is the mistake, could you take a look at this one?
#include<stdio.h>
#include <malloc.h>
int z, x;
double fx;
int main(void){
double *c = (double *)malloc(x*sizeof(double));
scanf("%d", &x);
fx=100;
for(z=0; z<x; z++){
scanf("%lf", c+z);
if(fx>*(c+z)) fx=*(c+z);
}
printf("%.2lf", fx*2);
free(c);
return 0;
}
I'm writing a program that calculates the value of the normal distribution function given to me here:
The program is supposed to ask the user for the mean μ and the Standard Deviation σ for the normal distribution showed above. The program then asks for N values of x and then asks for each value of x, one by one. After each value x it writes out the corresponding value of the function.
This is my code so far:
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
int main() {
int j;
double u, stddev, N, result, x;
printf("Enter u and stddev for the Normal Distribution:\n");
scanf("%lf %lf",&u, &stddev);
printf("Enter how many values of x (N) for the Normal Distribution:\n");
scanf("%lf",&N);
for (j=0; j<N; j++) {
printf("Enter a value for x: \n");
scanf("%lf",&x);
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(1/2)*((x-u)/stddev)*((x-u)/stddev));
printf("%.6lf\n", result);
}
}
I'm basically done but the answers the program is giving me are wrong when compared to my answers from my calculator. For instance, when I make N = 3 no matter what I put for the 3 values of x, the answer it gives me for each are the same when they shouldn't be.
So I know my issue lies in this line of code:
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(1/2)*((x-u)/stddev)*((x-u)/stddev));
Am I just writing the function wrong in the program? I must be, for it not to work.
Your code is clean and functional. Good work so far.
You were correct about which line needed changing - The issue is that using integer division results in an integer, so 1 / 2 results in a value of 0. This can be remedied by using a single double value: 0.5, or by dividing using doubles 1.0 / 2.0.
result = ((1)/(stddev*sqrt(2*M_PI)))*exp(-(0.5)*((x-u)/stddev)*((x-u)/stddev));
I tested out your code after making these changes try it online, and they match up perfectly with the formula on wolfram alpha, as well as what you said in the comments.
For example:
μ=2, σ=3, x=7 results in 0.033159
μ=3, σ=7, x=0 results in 0.051991
μ=4, σ=4, x=4 results in 0.099736
I ran it, and everything seems to be fine--except that it keeps giving me a margin of error of 1. Why is it doing this?
The program is supposed to prompt the user to input an estimation for the cube root of 3, and it uses Newton's method of approximation to show how many attempts it took to get to the approximation. After 500 attempts or a margin of error less than 0.000001, it's supposed to exit the loop. Why, though, doesn't the margin of error change?
Here's my code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
float a, i, e; //declare float variables
printf("Consider the function f(x) = x^3 - 3 = 0.\n");
printf("Simplifying, we get x^3 = 3.\n");
printf("Simplifying it further, we get x = 3^(1/3).\n");
printf("Enter your estimate of the root: ");
scanf("%f", &a); //prompt user to guestimate
printf("So you're saying that x = %f.\n", a);
i=0; //initiate attempt counter
e=abs((a-pow(3, (1/3)))/pow(3, (1/3))); //margin of error formula
while (e>=0.000001 && i<=500) //initiate while loop with above expressions
{
if (a!=pow(3, (1/3)))
{
printf("Attempt %f: ", i);
a = a - (pow(a, 3) - 3)/(3*pow(a, 2));
printf("%f, ", a);
printf("%f margin of error\n", e);
i=i+1;
}
else
break;
}
}
abs() deals with ints and will return an int, you need fabsf().
In the same way, pow() is for doubles, you should use powf().
Another mistake is writing 1/3 and expecting 0.333... as a result. 1 and 3 are int literals, so the operation performed is integer division. You need to use float literals, such as 1.0f/3.0f.
That's it for type compatibility. I can see another error however : you expect e to somehow remember its formula and reapply it automagically. That's not how imperative languages work : when you write e = something, "something" is calculated and stored in e once and for all. You're doing it correctly for a, now just bring e=abs(...); inside the while loop to update it each time.
I've got this math formula which is used to find the position of the sun d = 23.45p / 180 * sin ( 2p * ( 284 + n ) / 365 ) (where d - Declination of the sun, p-PI=3.14159 and n-Number of days)
I used the following coding in C to find what answer i'll get if I enter a any number of 'n', but it comes up with error when compiling.
The Code
#include <stdio.h>
#include <math.h>
#define PI=3.1415
main ()
{
float a,b,c,d,n,answer,PI;
//const float PI=3.1415;
printf ("\nEnter today's number (between 1 and 365 days in a year)\n");
printf ("to locate the sun's position:\n");
scanf ("%f", &n);
d = ((23.45*M_PI)/180.00)*sin(2.00*M_PI(284.00+&n)/365.00);
printf ("\nDeclination = %f\n", d);
}
This:
#define PI=3.1415
turns this line:
float a,b,c,d,n,answer,PI;
into this:
float a,b,c,d,n,answer,3.1415;
... which is, most likely, the compile error (or at least one of them...).
M_PI is an undefined variable. That's probably causing the error. Also, &n means address of n, so don't write (284.00 + &n). Instead just write (284.00 + n). Make sure you have included the right header file for the sin function. Also, as the other poster pointed out, don't define PI again.
I at least made it compile:
#include <stdio.h>
#include <math.h>
#define PI 3.1415
main ()
{
float a,b,c,d,n,answer; // remove PI here.
printf ("\nEnter today's number (between 1 and 365 days in a year)\n");
printf ("to locate the sun's position:\n");
scanf ("%f", &n);
d = ((23.45*PI)/180.00)*sin(2.00*PI*(284.00+n)/365.00);
printf ("\nDeclination = %f\n", d);
}
you can define PI as #define or as float var, but not both at the same time.
A #define don't need a =, it's not a assignment.
The d=... was not OK.
in scanf you take the address of your result variable (that is &n), but you simply use n in the calucaltion.
I have no clue if the results are OK, maybe you have some test data. (BTW: this year, 2012, has one day more...)
You don't need the & here
284.00+&n
You do the sum with just the value of n, you use &n in the scanf because scanf needs to update the value of n.
also use something like
#define PI 3.1413f
define does not need an =. If you have defined PI then you don't need to declare it
float a,b,c,d,n,answer,PI;
should be
float a,b,c,d,n,answer;