i need to gain a better understanding of function definition, declarations and proper calls using this program. I really need the understanding of how to use them. Could you show me the proper way to write this program with all three correct and explained?
#include <stdio.h>
#include <math.h>
quad_equation(float a, float b, float c);
int main()
{
float a, b, c, determinant, r1,r2;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0) { r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else (determinant<0);
{
printf("Both roots are complex");
}
return 0;
I just solved this exact question here : (I guess this is a part of an assignment )
https://stackoverflow.com/a/19826495/1253932
Also looking at your code .. you never use the function quad equation .. also you haven't defined the type of the function ( int/void/float/char) etc.
For ease: ( here is the entire code ) -- ask me if you don't understand anything
#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);
}
EDIT:
A slightly more optimized and better functioning code that I made from the above mentioned code:
http://pastebin.com/GS65PvH6
Edit2:
From your comments you try to do this:
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
This will fail if you input something like this: 121
Beacuse scanf will read the whole 121 into a and it will have nothing for b,c ( rather it will put \n(enter) into b and undefined into c )
So use the scanf the way I have used it in my code
OK - this is full of problems! I attempt to point them out, and show what "better" looks like. I hope this helps.
quad_equation(float a, float b, float c);
This is probably intended to be a "function prototype". A prototype tells the compiler "I am going to use this function later, and this is how it needs to be called, and the type it returns". You did not specify a return type; probably you want to use int to say whether you found roots or not, and print out the result in the function. Better would be to pass space for two return values as a parameter:
int quad_equation(float a, float b, float c, float* x1, float* x2);
Now we can use the main program to get input/output, and let the function solve the problem:
int main(void) {
{
float a, b, c, r1, r2;
int n;
// here you get the inputs; that seems OK
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a,&b,&c);
// now you have to "call your function"
// note that I make sure to follow the prototype: I assign the return value to an int
// and I pass five parameters: the coefficients a, b, c and the address of two variables
// x1 and x2. These addresses will be where the function puts the roots
n = quad_equation(a, b, c, &r1, &r2);
// when the function returns, I can print the results:
printf("There are %d roots:\n", n);
// based on the value of n, I change what I want to print out:
if (n == 2) printf(" %f and ", r1); // when there are two roots I print "root 1 and"
if (n > 0) printf("%f\n", r2); // when there is at least one root, I print it
// note that if n == 0, I would skip both print statements
// and all you would have gotten was "There are 0 roots" in the output
}
int quad_equation(float a, float b, float c, float* x1, float* x2) {
// function that computes roots of quadratic equation
// and returns result in x1 and x2
// it returns the number of roots as the return value of the function
float determinant;
determinant=b*b-4*a*c;
if (determinant>0)
{
*x1 = (-b+sqrt(determinant))/(2*a);
*x2= (-b-sqrt(determinant))/(2*a);
return 2;
}
if (determinant==0) {
*x1 = *x2 = -b/(2*a);
return 1;
}
return 0;
}
Related
Really new programmer with a really bad professor here.
I have this code that is supposed to get inputs from a text document (inputsFile) using a function (get_coefficients) and do stuff with it. Currently, everything works perfectly except it reads the same line from the file every time it's executed in the while loop in main. I've google around but I can't find anything that is implementable in my case. I've tried implementing while loops and trying to pass some sort of counting variable but nothing seems to work.
Since this is an assignment for school, I can't do anything fancy. So the more elementary the explanation, the better please, haha. Thank you in advance for any help I get.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//=======================================FUNCTIONS======================================
//a function that displays instructions
void display_instructions() {
cout << "Enter a, b, c: ";
}
//a function that gathers inputs
void get_coefficients(double& a, double& b, double& c) {
ifstream inputsFile;
string inputs;
string inputString;
inputsFile.open("textinputs.txt");
inputsFile >> a >> b >> c;
cout << a << b << c;
inputsFile.close();
}
//a function that calculates a discriminant
double calc_discriminant(double a, double b, double c) {
double discriminant = (b * b) - (4 * a * c);
return discriminant;
}
//a function that calculates root 1
double calc_root_1(double a, double b, double c, double disc) {
double r1 = ((-b) + (sqrt(disc))) / (2 * a);
return r1;
}
//a function that calculates root 2
double calc_root_2(double a, double b, double c, double disc) {
double r2 = ((-b) - (sqrt(disc))) / (2.0 * a);
return r2;
}
void display(double r1, double r2) {
cout << "the roots are " << r1 << " and " << r2 << "." << endl;
}
//=======================================EXCECUTE=======================================
int main()
{
//while again is true
for (int i = 0; i < 3; i++) {
double a, b, c;
display_instructions();
//run get numbers
get_coefficients(a, b, c);
//if discrimant less than 0 stop the program
if (calc_discriminant(a, b, c) < 0) {
cout << "No real solution" << endl;
}
else { //else get the roots
double disc = calc_discriminant(a, b, c);
double r1 = calc_root_1(a, b, c, disc);
double r2 = calc_root_2(a, b, c, disc);
//print the roots
display(r1, r2);
}
}
}
The problem here is that when you do inputsFile.open() you're opening the file from the start. You should only open the file once so each time you read from it you'll read the next line from where you were the last time. However, the ifstream will be deleted at the end of the scope if you don't save it anywhere. What you can do is initialize the ifstream in main() and then pass it to the function by reference so the ifstream will still be there until the program reaches the end of main.
I think this should work for what you want:
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
//=======================================FUNCTIONS======================================
//a function that displays instructions
void display_instructions() {
cout << "Enter a, b, c: ";
}
//a function that gathers inputs
void get_coefficients(double& a, double& b, double& c, ifstream& inputsFile) {
string inputs;
string inputString;
inputsFile >> a >> b >> c;
cout << a << b << c;
}
//a function that calculates a discriminant
double calc_discriminant(double a, double b, double c) {
double discriminant = (b * b) - (4 * a * c);
return discriminant;
}
//a function that calculates root 1
double calc_root_1(double a, double b, double c, double disc) {
double r1 = ((-b) + (sqrt(disc))) / (2 * a);
return r1;
}
//a function that calculates root 2
double calc_root_2(double a, double b, double c, double disc) {
double r2 = ((-b) - (sqrt(disc))) / (2.0 * a);
return r2;
}
void display(double r1, double r2) {
cout << "the roots are " << r1 << " and " << r2 << "." << endl;
}
//=======================================EXCECUTE=======================================
int main()
{
ifstream inputsFile;
inputsFile.open("textinputs.txt");
//while again is true
for (int i = 0; i < 3; i++) {
double a, b, c;
display_instructions();
//run get numbers
get_coefficients(a, b, c, inputsFile);
//if discrimant less than 0 stop the program
if (calc_discriminant(a, b, c) < 0) {
cout << "No real solution" << endl;
}
else { //else get the roots
double disc = calc_discriminant(a, b, c);
double r1 = calc_root_1(a, b, c, disc);
double r2 = calc_root_2(a, b, c, disc);
//print the roots
display(r1, r2);
}
}
inputsFile.close();
}
I've written a code to find the reciprocal of a number without using division but rather approximating it using bisection method. Got two doubts. One, what should I keep the lower limit and the upper limit for x? Also is there a faster method to converge from the limits to the required value(reciprocal of the input) rather than just averaging it? And the main doubt, when I try to run it, It just stops after receiving the input number from the user. Any hints so solve that?
Here is the code:
#include<stdio.h>
#include<cstdlib>
#define epsilon 0.0001
float f(float x, float &m)
{
if(m==0)
{
printf("Reciprocal not defined");
return 0.0;
}
else
{
return x+1/m;
}
}
int main(void)
{
float m,g1,x,g2,c;
printf("Enter a number:\n");
scanf("%f",f(x,m));
g1=epsilon;
g2=m;
while(abs(f(g1,m)-f(g2,m))>epsilon)
{
c=(g1+g2)/2;
if(f(g1,m)*f(c,m)<0)
{
g2=c;
}
else if(f(c,m)*f(g2,m)<0)
{
g1=c;
}
}
printf("The reciprocal is approximately %f",c);
}
The code is expected to work as follows:
Enter a number: 5
The reciprocal is approximately 0.2
Enter a number: 0
Reciprocal is not defined
Instead of this, it shows:
Enter a number:
Reciprocal is not defined
(without accepting any input)
Your overall code is far too convoluted and your usage of scanf doesn't make sens.
You probbaly want something like this:
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#define epsilon 0.0001f
int main(void)
{
float m, g1, g2, c, diff;
printf("Enter a number:\n");
scanf("%f", &m);
g1 = epsilon;
g2 = m;
do
{
c = (g1 + g2) / 2;
diff = c * m - 1.0f;
if (diff > 0)
g2 = c;
else
g1 = c;
} while (fabs(diff) > epsilon);
printf("The reciprocal is approximately %f", c);
}
For your question "Also is there a faster method to converge from the limits to the required value (reciprocal of the input) rather than just averaging it?", I have no idea, but searching by bisection is generally rather fast.
Test program that tests for a range of values:
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#define epsilon 0.0001f
int main(void)
{
float g1, g2, c, diff;
for (float m = 1; m < 20; m += 1)
{
g1 = epsilon;
g2 = m;
do
{
c = (g1 + g2) / 2;
diff = c * m - 1.0f;
if (diff > 0)
g2 = c;
else
g1 = c;
} while (fabs(diff) > epsilon);
printf("The reciprocal of %f is approximately %f vs. %f, diff %f\n", m, c, 1/m, c-1/m);
}
}
Live demonstration
So I'm writing code to semi-automatically solve (in)direct proportional questions using two values that are always given at the start. It's returning a -nan(ind) error so I'm hopefully seeking for someone to help. Thank you in advance, your help is always appreciated no matter how small it is.
I understand nan is not a number but it's just being irritating to fix this, not asking for someone to feed me the fix but if you'd like to you may - I'm looking to find a fix so that in the future I wouldn't be as clueless when it comes to an issue like this.
#include "Prop.h"
float c, d, k;
std::string prop::getinput(std::string obj) {
std::getline(std::cin, obj);
}
float prop::storefloat(float inp) {
std::cin >> inp;
return 1;
}
int prop::printarr(float arr[]) {
std::copy(arr, arr + sizeof(arr) / sizeof(arr[0]), std::ostream_iterator<float>(std::cout, "\n"));
}
int prop::compare(int com, int pare) {
if (com && pare <= 0) {
return 0;
}
}
void prop::direct(float a, float b, float constant) {
constant = DIRECTfindconstant(a, b);
printf("%d\n", constant);
c = ((constant) * (b));
printf("%d\n", c);
d = ((b) / (constant));
printf("%d\n", d);
std::cout << "Constant : " << constant << "\nDominant algebraic letter : " << c << "\nSecond Letter : " << d << "\n";
}
float prop::completedirect(float a, float b, float c) {
compare(a, b);
direct(a, b, c);
return 0;
}
float prop::DIRECTfindconstant(float a, float b) {
//k on bottom right, a ontop, b on bottom right
float k = ((a) / (b));
return k;
}
float prop::INDIRECTfindconstant(float a, float b) {
//k ontop, a on bottom left, b on bottom right
float k = a * b;
return k;
}
void prop::caseinput(int inp, float val, float val2) {
switch (inp) {
case Prop::proportionality::direct: {
float constant = DIRECTfindconstant(val, val2);
printf("%d\n", constant);
printf("%d", val);
printf("%d", val2);
completedirect(val, val2, constant);
break;
};
default:
break;
}
}
//prop.cpp
int main()
{
std::cin >> test;
pro->storefloat(a);
pro->storefloat(b);
pro->caseinput(test, a, b);
system("pause");
return 0;
}
Quoting the relevant part:
float prop::storefloat(float inp) {
std::cin >> inp;
return 1;
}
This is pass-by-value, as your book should tell you. You can change the local inp, but it does not affect the caller. The return value 1 is pointless here, while return values are the logical way to return a value to the caller.
I have the following code which says that 'distance' is used uninitialized in this function.
This is a code that accepts two coordinates from a cartesian plane and uses the distance between them as radius to find the area of a circle. This is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point {
int x, y;
};
double getDistance(struct Point a, struct Point b)
{
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distance;
}
int main()
{
float Area;
double distance;
struct Point a, b;
printf("Enter coordinate of point a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of point b: ");
scanf("%d %d", &b.x, &b.y);
printf("Distance between a and b: %lf\n", getDistance(a, b));
Area= 3.14 * distance * distance;
printf("\nArea of Circle : %f", Area);
return 0;
}
This is correct: variable distance inside getDistance and variable distance inside main are two different variables.
When you write this
printf("Distance between a and b: %lf\n", getDistance(a, b));
distance inside main does not get set.
You can fix it by adding an assignment
distance = getDistance(a, b);
printf("Distance between a and b: %lf\n", distance);
Implementation note: Since you need distance squared, you can avoid taking square root by defining a function getDistanceSquared, and using it instead.
You should read more carefully the compiler warning, because it is refering to the variable distance in your main function not the one in getDistance.
I think, what you actually wanted to do was this:
distance = getDistance(a, b);
printf("Distance between a and b: %lf\n", distance);
Then, you can use the result of getDistance anywhere in your main function. ;)
You forgot to assign distance variable, try something like:
int main()
{
float Area;
double distance;
struct Point a, b;
printf("Enter coordinate of point a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of point b: ");
scanf("%d %d", &b.x, &b.y);
distance = getDistance(a, b);
printf("Distance between a and b: %lf\n", distance);
Area= 3.14 * distance * distance;
printf("\nArea of Circle : %f", Area);
return 0;
}
So I have a program that implements an adaptive 2D trapezoidal rule on the function x^2 + y^2 < 1, but it seems that the recursion isn't working -- the program here is a modified form of a (working) 1D trapezoidal method so I'm not sure where the code breaks down, it should return PI:
double trapezoidal(d_fp_d f,
double a, double b,
double c, double d) { //helper function
return 0.25*(b-a)*(d-c)*
(f(a, c)+f(a, d) +
f(b, c)+f(b, d));
}
double atrap( double a, double b, double c, double d, d_fp_d f, double tol )
{// helper function
return atrap1(a, b, c, d, f, tol );
}
double atrap1( double a, double b, double c, double d, d_fp_d f, double tol)
{
//implements 2D trap rule
static int level = 0;
const static int minLevel = 4;
const static int maxLevel = 30;
++level;
double m1 = (a + b)/2.0;
double m2 = (c + d)/2.0;
double coarse = trapezoidal(f,a,b,c,d);
double fine =
trapezoidal(f, a, m1, c, m2)
+ trapezoidal(f, a, m1, m2, d)
+ trapezoidal(f, m1, b, c, m2)
+ trapezoidal(f, m1, b, m2, d);
++fnEvals;
if( level< minLevel
|| ( abs( fine - coarse ) > 3.0*tol && level < maxLevel ) ){
fine = atrap1( a, m1, c, m2, f,tol/4.0)
+ atrap1( a, m1, m2, d, f, tol/4.0)
+ atrap1(m1, b, c, m2, f, tol/4.0)
+ atrap1(m1, b, m2, d, f,tol/4.0);
}
--level;
return fine;
}
where the function is given by
double ucircle( double x, double y)
{
return x*x + y*y < 1 ? 1.0 : 0.0;
}
and my main function is
int main()
{
double a, b, c, d;
cout << "Enter a: " <<endl;
cin >> a;
cout << "Enter b: " <<endl;
cin >> b;
cout << "Enter c: " <<endl;
cin >> c;
cout << "Enter d: " <<endl;
cin >> d;
cout << "The approximate integral is: " << atrap( a, b, c, d, ucircle, 1.0e-5) << endl;
return 0;
}
It will not actually run forever, but it actually run for a very long time that you think it is running for ever and that is the reason: in first run level is one and function enter your if and it call itself 4 times, now consider first time: it is also enter the if and call itself 4 more times and it continue ... for correctly chosen input like one specified by you, condition abs(fine - coarse) is always true so only thing that can stop the flow from entering the if is level that will be increased and then decreased so your function will be called almost 4^30 and that's really a big number that you can't see its end in an hour or 2!
Like BigBoss already wrote, your program should finish, it would just take a long time since 30 recursions mean 4^30 function calls for atrap1, which is 1152921504606846976. Just let that number sink in.
Here are some more things to consider:
You probably wanted to use fabs instead of abs in the "break condition". (I think you should get a warning for integer conversion - or something similar - for this) abs may return unpredictable values for float or double parameters. Very high values.
tol seems to be a variable that represents a target precision value. However, with each recursion you further increase this target precision. At the 10th recursion it's already about 1E-11. Not sure this is intended. Whatever tol means.
You probably don't want the /4.0 (the .0 is redundant by the way) in your recursive calls.
You do compile this with optimization, right?
trapezoidal, minLevel, maxLevel could be macros.
Your function does not like threaded execution due to level being static. You should make it a parameter for atrap1.