Normal Distribution/Standard Deviation - c

My 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 )
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.
What I am confused about is what the N is supposed to stand for. I assumed it was number of x's but can anyone clarify this for me?
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s, N, x1,math1, math2, math3,n, v, x;
printf("Enter Mean: ");
scanf("%lf", &u);
printf("Enter Standard Deviation: ");
scanf("%lf", &s);
printf("Enter number of x's: ");
scanf("%lf", &N);
for (v=1; v<=N; v++)
{
printf("Enter Value: ");
scanf("%lf", &x);
n=(-1/2);
printf("f(x)= ");
math1 =1/(u*sqrt(2*M_PI));
math2= (x-u)/s * (x-u)/s;
math3= M_E * exp(n);
x1 = math1 * exp(math3)*exp(math2);
printf("%lf \n", x1);
}
system("Pause");
}

N stands for number of inputs
pretty much clear from this part:
for (v=1; v<=N; v++)

If your instructions are as given, then N does indeed stand for the number of x values required.
Your program does just that, asking for N values of x.
First, it declares a variable N at the start of the program:
double u,s, N, x1,math1, math2, math3,n, v, x;
Then it prompts for input as an integer:
printf("Enter number of x's: ");
scanf("%lf", &N);
...and finally uses that integer to read in N values for x.
for (v=1; v<=N; v++)
{

Related

Can't print quotient with scanf

My objective in this exercise is to create a function names MultiTwo() that will multiply two inserted integers by the user and then print the quotient. MultiTwo has to be called inside main().
Here's my try:
#include <stdio.h>
int MultiTwo(int x, int y, int result);
int main() {
int x, y, result;
printf("Insert an integer: \n");
x = scanf("%d", &x);
printf("Insert a second integer: \n");
y = scanf("%d", &y);
result = x * y;
printf("The quotient of the two inserted integers is: %d", result);
return 0;
}
I insert two integers and the result I always get, despite the integers inserted, is 1.
x = scanf("%d", &x);
This first reads the value from input and assigns it to the variable x. Then you clobber that value by setting x to the return value of scanf(), which is the number of variables successfully read, in this case it is 1.

Turbo C gives Output as 0.000000

I have set the correct directory paths for Turbo C. But yet it gives the output as 0.000000
Following is the program:
#include <conio.h>
#include <math.h>
#include <stdio.h>
void main() {
int n;
float r, si, ci, p;
clrscr();
printf("enter principle amount\n");
scanf("%f", &p);
printf("enter rate of interest\n");
scanf("%d", &r);
printf("enter number of years\n");
scanf("%f", &n);
si = p * n * r / 100;
ci = p * (pow((1 + (r / 100)), n) - 1);
printf("simple interest=%f\n", si);
printf("compound interest=%f", ci);
getch();
}
It is supposed to give numbers instead of 0.000000
Any help?
Change:
scanf("%f",&n);
to:
scanf("%d",&n);
since n is an integer, not a float, as suggested in the comments already.
For r, which is of type float, you should use scanf("%f",&r);.
PS: Consider using a modern compiler, such as GCC.

Write a C program that computes the solutions for x and y in the linear system of equations

what's wrong with my code that i can't get the values for x and y, assume that the denominator is not zero.
i can't get constant values of x and y, most of the time its equal to zero.
a1x + b1y = c1
a2x + b2y = c2
#include <stdio.h>
#include <math.h>
int main()
{
/* Write your program code here */
int a1,b1,c1,a2,b2,c2,x,y;
printf("Enter the value for a1: \n");
scanf("%d", &a1);
printf("Enter the value for b1: \n");
scanf("%d", &b1);
printf("Enter the value for c1: \n");
scanf("%d", &c1);
printf("Enter the value for a2: \n");
scanf("%d", &a2);
printf("Enter the value for b2: \n");
scanf("%d", &b2);
printf("Enter the value for c2: \n");
scanf("%d", &c2);
x=((b2*c1)-(b1*c2))/((a1*b2)-(a2*b1));
printf("x is %d\n", x);
y=((a1*c2)-(a2*c1))/((a1*b2)-(a2*b1));
printf("y is %d\n", y);
return 0;
}
Maybe you need to change the type for x and y variables from int to float or double if you want accurate/precise results for x and y.
Using int type will not show you same results like float/double type because it won't show you numbers after decimal point instead it's truncated towards zero, yielding largest whole number which is smaller that floating point number for numbers greater than zero, and smallest whole number which is larger than floating number for numbers below zero.

Illegal use of floating point

I'm trying to write a code that calculates monthly pay for a project.
This is the formula I was given:
(Rate + Rate/((1+Rate)^Months)-1) * Principle
Rate according to this formula is Rate/1200 so as an example if the rate is 7% it would be 7/1200 which is 0.00583333333. I'm trying to get the exact number 0.00583333333 into my program but then I get the error "illegal use of floating point".
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float r;
int m, y;
int p;
//int mp;
printf("Enter Rate: ");
scanf("%d", &r);
r = r%1200;
printf("Enter number of years: ");
scanf("%d", &y);
m = y*12;
printf("%.10lf\n",r);
printf("%d",m);
return 0;
}
How do I get 0.00583333333 to be a part of my calculation in the program?
try to change scanf("%d", &r); by scanf("%f", &r); and r = r%1200 by r = r/1200

Integration by Simpson's Rule

One of my assignments was to create a c program that uses the Simpson's 1/3 rule to find the sum. I am running into issues that I am having trouble fixing. Can some one with more experience point me in the right direction?
In theory my code integrates y=ax^2+bx+c where the user selects values for a,b,c and then the user selects the upper and lower bounds [d,e]. Then the user selects the n value which splits up the area into more rectangles (the value that we will use in my class is 100, so the area is split into 100 rectangles). After which it runs through the Simpson's rule and prints out the sum.
//n is required number of iterations.
#include<stdio.h>
#include<conio.h>
#include<math.h>
double integral (int a,int b,int c,int d,int e,int n)
int main()
{
double a, b, c, d, e, n;
printf("Please select values for y=ax^2+bx+c");
printf("Please select value for a");
scanf("%d", &a);
printf("Please select value for b");
scanf("%d", &b);
printf("Please select value for c");
scanf("%d", &c);
printf("Please select value for the upper limit");
scanf("%d", &d);
printf("Please select value for the lower limit");
scanf("%d", &e);
printf("Please select the number of rectangles for the Simpson's Rule (Input 100)");
scanf("%n", &n);
int i;
double sum=0,length=(double)(d-e)/(n),ad,bd,cd,dd;
ad=(double)a;
bd=(double)b;
cd=(double)c;
dd=(double)d;
for (i=0;i<n;i++)
{
sum+=(ad*(dd*dd+2*dd*length*i+length*length*i*i)+bd*(dd+length*i)+cd)*length;
printf("the value is = %d",sum);
}
return sum;
}
Why do you think this
scanf("%e", &e);
should be that way?
The scanf() function takes a format specifier to match the scanned input with, in your case you want to store the values in a double variable, for which you need the "%lf" specifier, so all your scanf()'s should change to
scanf("%lf", &whateverDoubleVariableYouWantToStoreTheResultIn);
You don't need to cast from a variable of a given type to the same type, like here
dd=(double)d;
And also, you must know, that scanf() returns a value, you should not ignore it because your program will misbehave in case of bad input, you should check scanf() in a library manual or the C standard to understand better how to use it.
In addition to #iharob fine advice:
Change n type
// double a, b, c, d, e, n;
double a, b, c, d, e;
int n;
Adjust input code
// and previous lines
if (1 != scanf("%lf", &e)) // %d --> %lf
Handle_InputError();
printf("Please select the number of rectangles for the Simpson's ...
if (1 != scanf("%d", &n) // %n --> %d
Handle_InputError();
Adjust output
// printf("the value is = %d",sum);
printf("the value is = %e",sum); // or %f
Minor bits
// int main()
int main(void) // or int main(int argc, char *argv[])
// return sum; returning a double here is odd
return 0;

Resources