I need to make a program in C that takes 3 coefficients, a, b, c, then solves for Delta. It then takes Delta and decides what Function to send it to determine it's output.
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: Find Delta, solve for roots.
*
* Input: Coefficients a, b, c.
*
* Output: Roots
*/
#include <stdio.h>
#include <math.h>
int main (void)
{
//Local Declarations
float a;
float b;
float c;
float delta;
//Statements
printf("Input coefficient a.\n");
scanf("%.2f", &a);
printf("Input coefficient b.\n");
scanf("%.2f", &b);
printf("Input coefficient c.\n");
scanf("%.2f", &c);
printf("%fx^2 + %fx + %f\n", &a, &b, &c);
//Process
delta = (b * b) - (4 * a * c);
if (delta > 0) twoRoots(a, b, c, delta);
else if (delta = 0) oneRoot(a, b, c, delta);
else if (delta < 0) noRoots();
return;
} // End main
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: To solve for the two roots.
*
* Input: None
*
* Output: Root one, Root two.
*/
#include <stdio.h>
#include <math.h>
int twoRoots ()
{
//Local Declarations
float xOne;
float xTwo;
float delta;
float deltaRoot;
float a;
float b;
printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", &xOne);
printf("%.2f", &xTwo);
return;
} // End twoRoots
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: To solve for the one root.
*
* Input: None
*
* Output: Root one.
*/
#include <stdio.h>
#include <math.h>
int oneRoot ()
{
//Local Declarations
float xOne;
float xTwo;
float deltaRoot;
float a;
float b;
printf("There is exactly one distinct root./n");
xOne = -b / (2*a);
printf("%.2f", &xOne);
return;
} // End oneRoot
/*
*Program Name: COP 2220-10018 Project 4
*
* Author: Nathan Gamble
*
* Description: To inform the roots are complex.
*
* Input: None
*
* Output: Statement.
*/
#include <stdio.h>
#include <math.h>
int noRoots ()
{
//Local Declarations
printf("There are two distinct complex roots./n");
return;
} // End noRoots
When I run it, I get the following Output:
Input coefficient a.
1
Input coefficient b.
Input coefficient c.
0.000000x^2 + 882156984598706310000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000.000000x + 0.000000
Process returned 16384 (0x4000) execution time : 10.641 s
Press any key to continue.
I only input 1, for a, and then it spits outt he remainder of the main method.
Few things that jump out first:
printf("Input coefficient a.\n");
scanf("%f", &a); // you were scanning for 0.2f .. any reason why?
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);
your printf is also wrong .. change it to this:
printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c); // you were printing the addresses of a,b,c .. printf just needs the name of variables not their addresses
Output after doing above changes:
$ ./test
Input coefficient a.
1.5
Input coefficient b.
2.5
Input coefficient c.
3.5
1.50x^2 + 2.50x + 3.50
Fixed code: ( ask me if you have questions about any part )
#include <stdio.h>
#include <math.h>
// function declarations
void twoRoots (float a,float b,float delta);
void oneRoot (float a,float b,float delta);
int main (void)
{
//Local Declarations
float a;
float b;
float c;
float delta;
float solution;
printf("Input coefficient a.\n");
scanf("%f", &a);
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);
printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c);
delta = (float)(b*b) - (float)(4.0 * a * c);
printf("delta = %0.2f\n",delta);
if (delta > 0){
twoRoots(a,b,delta);
}else if (delta == 0) {
oneRoot(a,b,delta);
}else if (delta < 0.0){
printf("There are no real roots\n");
}
return 0;
}
void twoRoots (float a,float b,float delta)
{
float xOne;
float xTwo;
float deltaRoot;
printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", xOne);
printf("%.2f", xTwo);
}
void oneRoot(float a,float b,float delta)
{
float xOne;
float xTwo;
float deltaRoot;
printf("There is exactly one distinct root\n");
xOne = -b / (2*a);
printf("%.2f", xOne);
}
Output1:
$ ./test
Input coefficient a.
1.1
Input coefficient b.
5.5
Input coefficient c.
2.2
1.10x^2 + 5.50x + 2.20
delta = 20.57
There are two distinct roots.
-0.44-4.56
Output2:
$ ./test
Input coefficient a.
1
Input coefficient b.
4
Input coefficient c.
4
1.00x^2 + 4.00x + 4.00
delta = 0.00
There is exactly one distinct root
-2.00
Output3:
$ ./test
Input coefficient a.
1
Input coefficient b.
3
Input coefficient c.
9
1.00x^2 + 3.00x + 9.00
delta = -27.00
There are no real roots
I optimised the code and made it a whole lot more efficient here :
http://pastebin.com/GS65PvH6
I think that the problem comes from the fact that 1 is recognized as an int and not a float.
When you write %.2f scanf expects you to input a float. If it detects something else it fails and does not read any other scanf requests as specified in the man page.
Your immediate problem lies here:
scanf ("%.2f", &a);
You can put a length limiter on the value to be scanned but you probably shouldn't be trying to limit what's input anyway.
In any case, the .2 option you're using is not valid for scanf, it's a printf thing controlling the precision of the output.
The ISO standard states that scanf needs "an optional decimal integer greater than zero that specifies the maximum field width (in characters)". So there's no way using just scanf to limit how many digits are allowed after the decimal point.
Use this instead:
scanf ("%f", &a);
including for the other scanf calls.
As to further problems, there are a few, some of which are below. I haven't provided an exhaustive list as the problem specific to your question is the scanf format string.
First, you want to print the values of those variables rather than their addresses:
printf ("%fx^2 + %fx + %f\n", a, b, c);
Second, you pass the variables a/b/c/delta to your functions but you do not receive them. You need to declare them something like:
int twoRoots (float a, float b, float c, float delta)
and ensure you remove any local variable declarations for those names so that they don't hide the passed-in ones (or cause compilation errors).
Related
Reference Image
The output always comes: 6.35 (I think it's a garbage value)
the code is
#include<stdio.h>
#include<math.h>
float formula(float,float);
int main()
{
float l=0,a=0;
printf("enter the length of the pendulum(l)\n");
scanf("%f",&l);
printf("Enter the angle of displacemnt(a)\n");
scanf("%f",&a);
printf("the length is %0.2f\n",l);
printf("the angle of displacemnt is %0.2f\n",a);
printf("the period of pendulum is %0.2f",formula(l,a));
return 0;
}
float formula(float l, float a)
{
float P=0,ran=0,g = 9.8;
ran = (l/g) * (1 + ((1/4)*(pow((float)(sin((double)a/2)),2))));
P = 2 * M_PI * ((float)sqrt((double)ran));
return P;
}
I don't know what is happening 😐
You can simplify this implementation by using doubles or floats exclusively. The 1/4 integer division yields 0. Here's an example:
#include <stdio.h>
#include <math.h>
float formula(float l, float a)
{
float ran = l / 9.8f * (1.0f + powf(sinf(a/2.0f), 2.0f) / 4.0f);
return 2.0f * (float) M_PI * sqrtf(ran);
}
int main(void)
{
float l;
float a;
printf("Enter the length of the pendulum(l): ");
scanf("%f", &l);
printf("Enter the angle of displacemnt(a): ");
scanf("%f", &a);
printf("The length is %0.2f\n", l);
printf("The angle of displacemnt is %0.2f\n", a);
printf("The period of pendulum is %0.2f\n", formula(l,a));
return 0;
}
I have written the following code to compute the roots of a quadratic equation:
int quadroots(int *ap, int *bp, int *cp,int *root1p, int *root2p, int *dp);
int main (void) {
int ap,bp,cp;
int dp, root1p, root2p;
quadroots(&ap, &bp, &cp, &root1p, &root2p, &dp);
printf("The solutions are: %f, %f", root1p, root2p);
}
int quadroots(int *ap, int *bp, int *cp,int *root1p, int *root2p, int *dp){
int a, b, c, d, root1, root2;
printf("Enter a, b, c \n");
scanf("%d, %d, %d", &a, &b, &c);
if (a==0) {
printf ("The system is linear. The roots cannot be computed using this program: a cannot be 0. Please recompile");
return 0;
}
int b_sqared = b*b;
d = b_sqared - (4 * a * c);
if (d<0) {
d=-d;
printf("The roots of this equation are the complex numbers: \n");
printf("%.3f+%.3fi", ((-b) / (2*a)), (sqrt(d) / (2 * a)));
printf(", %.3f%.3fi", (-b / (2*a)), (-sqrt(d) / (2*a)));
}
else if (d==0) {
printf("The root of this equation are real and equal. \n");
root1= (-d / (2*a));
printf("The roots of this equation are: %.3f, %.3f", root1, root1);
}
else {
printf ("The roots of the quadratic equation are real numbers. \n");
root1 = (-b + sqrt(d)) / (2*a);
root2 = (-b - sqrt(d)) / (2*a);
printf("Roots of the quadratic equation are the real numbers %.3f, %.3f", root1,root2);
}
return 0;
*root1p=root1;
*root2p=root2;
}
This is based off a code I had previously written which worked, but back then, I wasn't using functions.
As it is now, it compiles and runs fine (ie. it takes in the numbers and performs a calculation), but the answer it prints out is totally incorrect.
Eg. for the input "1 5 6" (corresponding to the equation x^2 +5x + 6, it should print out " The roots are real numbers.
The roots are the real numbers 6 and 1"
since those are the roots of the equation. However, it doesn't. What is printed are some absurdly huge numbers (Enter a, b, c
1 5 6
The roots of this equation are the complex numbers:
-2719010580126301300000000000.000+0.000i, -2719010580126301300000000000.0000.000iThe solutions are: 0.000000, 0.000000)
Any help would be much appreciated.
Thank you very much! Best.
printf("%.3f+%.3fi", ((-b) / (2*a)), (sqrt(d) / (2 * a)));
You are using integer division in ((-b) / (2*a)) So you will get incorrect values for some numbers.
You can use.
printf("%.3f+%.3fi", ((-b) / (2.0*a)), (sqrt(d) / (2 * a)));
to force a conversion to a double before the division. You need to do this for all division between two integers in the code.
The design of this programm is horrific, but if you
add #include <math.h> (so sqrt is known)
replace all ints by floats (you don't want integer maths here)
replace scanf("%f, %f, %f", &a, &b, &c); by scanf("%f %f %f", &a, &b, &c); (correct format string for scanf).
it should work more or less.
I didn't dig further, so there may be other problems though.
I'm new to C language and coding and I encountered a question asking me to change the function header of:
float RealRoot_1(float a, float b, float c);
float RealRoot_2(float a,float b,float c);
to become:
void RealRoot_1(void);
void RealRoot_2(void);
I was told that it has something to do with Global Variables but I still couldn't figure it out after trying quite some time. Can anyone please explain on how to do it? Thanks a lot.
The source file is as below:
#include<stdio.h>
#include<math.h>
int main()
{
float RealRoot_1(float a, float b, float c); // Prototype declaration
float RealRoot_2(float a, float b, float c);
// Defining Input Variables
float x, y, z;
// Defining Output Variables
float Root_1, Root_2;
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
Root_1 = RealRoot_1(x,y,z);
Root_2 = RealRoot_2(x,y,z);
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
float RealRoot_1(float a, float b, float c)
{
float x;
x = (-1*b + sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
float RealRoot_2(float a, float b, float c)
{
float x;
x = (-1*b - sqrt(pow(b,2) - 4 * a * c)) / (2 * a);
return x;
}
This can be done by using global variables. You need to ensure that the variable names used in the function are the same as the ones used in the main code.
#include<stdio.h>
#include<math.h>
void RealRoot_1(void); // Prototype declaration
void RealRoot_2(void);
float x, y, z;
float Root_1, Root_2;
int main()
{
// Defining Output Variables
printf("Please enter the factor of X^2: ");
scanf("%f",&x);
printf("Please enter the factor of X: ");
scanf("%f",&y);
printf("Please enter the free factor: ");
scanf("%f",&z);
RealRoot_1();
RealRoot_2();
printf("the First Root is: %f \n", Root_1);
printf("the Second Root is: %f \n", Root_2);
system("pause");
}
void RealRoot_1(void)
{
Root_1 = (-1*y + sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
void RealRoot_2(void)
{
Root_2 = (-1*y - sqrt(pow(y,2) - 4 * x * z)) / (2 * x);
}
Please note that this is a worse way of doing things than was given in the initial problem. In the initial exercise. You are loosing modularity and using too many globals is in general a bad idea.
You can also see Are global variables bad?
This should be self explanatory:
float RR_a, RR_b, RR_c;
float RR_d; // store result here(like a return value)
void RealRoot_1(void); // prototypes
void RealRoot_2(void);
void main(void)
{
printf("Please enter the factor of X^2: ");
scanf("%f",&RR_a);
printf("Please enter the factor of X: ");
scanf("%f",&RR_b);
printf("Please enter the free factor: ");
scanf("%f",&RR_c);
RealRoot_1();
printf("the First Root is: %f \n", RR_d);
RealRoot_2();
printf("the Second Root is: %f \n", RR_d);
system("pause");
}
void RealRoot_1(void)
{
float x;
x = (-1*RR_b + sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
void RealRoot_2(void)
{
float x;
x = (-1*RR_b - sqrt(pow(RR_b,2) - 4 * RR_a * RR_c)) / (2 * RR_a);
RR_d = x;
}
Notice that after calling RealRoot_1 we now print the result before calling RealRoot_2. That's because the result of RealRoot_1 which is stored in RR_d is overwritten by RealRoot_2, thus it is lost.
You can circumvent this by declaring a second return variable, RR_d_2 and storing the result of RealRoot_2 in it.
We do not need duplicates for RR_a, RR_b or RR_c because their values are not modified within the functions.
This way of writing functions has limitations, which will be obvious when faced with recursion or multi-threading.
I am working in c after a long time.Here i have to achieve three functionality which includes
get a number and show half
2.Get the square of the number
3.Get two number and show their summation and sabtraction.
I am using devC++ and when i compile the code i get the error i mentioned in the title which conflict type if squareInput.What is wrong here:
#include<stdio.h>
#include<conio.h>
int main(){
float x;
printf("enter a number\n");
scanf("%f",&x);
//TASK 1 : display half of the number
pirntf("half of x is = %.3f",x);
//TASK 2 : square of number
squareInput(x); //call square function from here
// TASK 3 : get two numbers and display both summation and sabtraction
float num1,num2; // declare two floating number( floating numbers can hold decimal point numbers
printf("enter num1 \n");
scanf("num1 is =%f",&num1);
printf("enter num2 \n");
scanf("num2 is =%f",num2);
calculate(num1,num2);// call calculate function
getch();
}
float squareInput(float input){
float square=input*input;
printf("\n square of the number is %.3f \n",square);
return 0;
}
float calculate(float num1,float num2){
//summation
float summation= num1+num2; // declare antoher variable called summation to hold the sum
//sabtraction
float sabtraction=num1-num2;
printf("summation is %.2f \n",summation);
printf("sabtraction is %.2f \n",sabtraction);
return 0;
}
Things will go wrong without prototypes. Add
float squareInput(float input);
float calculate(float num1,float num2);
in front of int main().
If you don't declare a function before it's called, the compiler assumes it as a int-returning function. However, squareInput() return float, so the compiler(or linker, maybe) complains to you.
Also note that definitions are declarations(but not vice versa, obviously), so moving the definitions of squareInput() and calculate() in front of where they are called works too.
At the time you call squareInput and calculate, they haven't been defined yet. So C assumes an implicit declaration of int squareInput() and int calculate(). These implicit declarations conflict with the definitions of these functions.
You can fix this by either adding declarations for each of these functions before main:
float squareInput(float input);
float calculate(float num1,float num2);
Or by simply moving the functions in their entirety before main.
Be sure to add prototypes when you use a function. That way you do not need to worry too much about the order in which you call them.
Also try to separate your problems into smaller bits if you can. A comment like TAKS1 shows you that you actually want a function with that name.
#include <stdio.h>
//prototypes
void AskUserForOneNumer(float * number, const char * question );
void TASK_1(float x);
void TASK_2(float x);
void TASK_3(float a, float b);
int main()
{
float x, a, b;
AskUserForOneNumer(&x, "enter x");
AskUserForOneNumer(&a, "enter a");
AskUserForOneNumer(&b, "enter b");
TASK_1(x);
TASK_2(x);
TASK_3(a, b);
}
void TASK_1(float x)
{
printf("x = %g\n", x);
printf("0.5 * x = %g\n", 0.5 * x);
}
void TASK_2(float x)
{
printf("x = %g\n", x);
printf("x * x = %g\n", x * x);
}
void TASK_3(float a, float b)
{
printf("a = %g\n", a);
printf("b = %g\n", b);
printf("a + b = %g\n", a + b);
printf("a - b = %g\n", a - b);
}
void AskUserForOneNumer(float * number, const char * question)
{
float x;
printf("%s\n", question);
scanf("%f", &x);
printf("your input was %g\n", x);
*number = x;
}
Is this the correct way to write the normal distribution function http://en.wikipedia.org/wiki/Normal_distribution or should I be using the pow function? I am really confused so help would be greatly appreciated :)
#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 Inputs: ");
scanf("%lf", &N);
for (v=1; v<=N; v++)
{
printf("Enter Value: ");
scanf("%lf", &x);
n=(-1/2);
printf("f(x)= ");
math1 =1/(s*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");
}
It's too hard to read your code, but I can tell it's wrong. Here is a short version:
double twopi = 8.0 * atan(1.0); // preferable to using M_PI
double x = ..., sigma = ..., mu = ...;
double y = (1.0 / (sigma * sqrt(twopi))) *
exp(-(x - mu)*(x - mu) / (2.0 * sigma * sigma));
Notice how I translate the mathematical formula directly to an expression in C... this makes it easy to verify that the code is correct. It's a bit harder when you use a bunch of temporary variables math1, math2, math3...
Remember: exp() is the same thing as its counterpart in mathematics, exp. So exp(x) is ex. Once you realize this, you will see the errors in your code.