C++ :: Determine Roots Of Quadratic Equations?
May 23, 2013
I have given following exercise in my cpp book: Determine the roots of quadratic equations
ax^2 + bx + c = 0
using formula
x = -b +(plus-minus) (root)b^2 - 4ac/2a
i don't now how to write such code...
View 6 Replies
ADVERTISEMENT
Oct 22, 2014
I have a task to find errors in this long line of code in order to correctly calculate the solutions of quadratic equations.
Code:
int main(int argc, char *argv[]) {
int validInput, solution_type;
double a, b, c, root_1, root_2, q;
/***********************
* Input / Validation
***********************/
/* Check numbers of arguments, and read input */
validInput = (argc = 4);
[Code] ....
Is a section of the code (the first section). And as you can probably guess, it goes on to calculate for a > 0 etc...
I dont really understand what the validinput section is saying? And a, b and c are never defined so Xcode is just saying a,b,c,root1,root2 are uninitialized and I also dont know what means. Do I need to define these values?
Code:
int main(int argc, char *argv[]) {
int validInput, solution_type;
double a, b, c, root_1, root_2, q;
[Code] ......
View 9 Replies
View Related
Sep 10, 2013
I am having trouble understanding how this loops would work. Give the function (x*x*x*x) - (10*x*x*x) + (35*x*x) - (50*x) + 24 Write a program that will use bisection method to find the roots of this function. Define lower limit and upper limit (e.g. -1.05 and 6.05) Starting at the lower limit step along the X axis at intervals of 0.1 for H calculating the function values f(x) and f(x +h), then f(x+h) and f(x+2h) until the upper limit is exceeded. If sign of the function value changes this indicates a root between ranges. Apply bi sectional method to this range until root has been found with an epsilon of 0.000001, then continue on until upper limit has been exceeded and all 4 roots found. If the function is within 0.000001 of zero then root has been found therefore no need for bi sectional method.
Code:
#include "stdafx.h"
#include "math.h"
#define H 0.1
#define epsilon 0.000001
double F(double x);
int main(void)
[code]....
So we have a function and a range. Program requires to work along the X axis at 0.1 increments until it reaches a point where the value changes from positive to negative or negative to positive. Then apply bisection method within that range to a given accuracy then print that root. Then continue on X axis until the next change of sign is found.
View 8 Replies
View Related
Feb 7, 2015
I have been writing code to find the roots of a quartic (with coefficients input by the user) by the Newton Rapson method. The method fails if no real roots exist so I have been trying to implement an initial check to see whether the equation has real roots. I've been trying to use the intermediate value theorem but it only works for functions with a positive and negative tail. extend the bounds (fa and fb) to check over many ranges or any other mathematical method of better effect?
Atm I define fa<0<fb where fa=F(-10) and fb=F(10).
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
int a0, a1, a2, a3, a4;
double F(double x) //Sets main function {
return (a0+a1*x+a2*pow(x, 2)+a3*pow(x, 3)+a4*pow(x, 4));
[code].....
View 1 Replies
View Related
Jan 21, 2013
I am looking to create a table somthing like this output below but I am very new to this and my code is abit everywhere.
x sqrt(x) x ^ 2 x ^ 3
========================================
1 1 1 1
2 1.414 4 8
3 1.732 9 27
4 2 16 64
5 2.236 25 125
6 2.449 36 216
7 2.646 49 343
8 2.828 64 512
9 3 81 729
10 3.162 100 1000
========================================
Totals: 55 22.47 385 3025
========================================
This is the code I have I have got it to cal some of the values but numbers our everywhere. I am doing more Trial and error than anything.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
[Code].....
View 8 Replies
View Related
Mar 12, 2013
this is what i have so far
#include <iostream>
#include <cmath>
using namespace std;
int main () {
int v;
[Code] ....
im trying to get the program to print this as an example if the user enters 5 (the rounding of the decimal is optional).
1 1.41 1.73 2 2.24
1 1.41 1.73 2
1 1.41 1.73
1 1.41
1
why its not reading my for loop for rows its only doing columns ...
View 4 Replies
View Related
Oct 18, 2013
This is my code:
Code:
#include <math.h>
#include <stdio.h>
int main(void) {
float a,b,c,root_1,root_2;
printf("Please enter value a from the quadratic equation
[Code] ......
And I keep getting this error:
Code:
/tmp/ccgtUIun.o: In function `main':
assign345.c:(.text+0xc7): undefined reference to `sqrt'
assign345.c:(.text+0xef): undefined reference to `sqrt'
collect2: ld returned 1 exit status
View 5 Replies
View Related
Jul 4, 2014
soo quadratic equation solver. output is "x=nan x=nan".
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//prototyping
double ans_1(double,double,double)
[code].....
View 2 Replies
View Related
Jan 28, 2014
We are suppose to build a program to do the quadratic formula, which isnt really a issue. my issue is i have a if else loop and my if is being ignored.
what i want it to do is is state that if the (b * /> - 4(a)© is a negative, then it prints out stating it cant do this.
note: i know that i can use the i number system but we have been asked to not too and to do this instead.
here is my code
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, d, e, f, g, h, i; /* i would love to figure out a way to do this without so many variables*/
[code]....
View 3 Replies
View Related
Nov 19, 2013
I need to find out all the possible equations which are same of a given equation. For eg. a+b+c-d and b+c+a-d are same. So if the user inputs a+b+c-d then the output will be all the possible equations that can be formed from the given equation, viz:
a+c+b-d
c+a+b-d
-d+a+c+b
etc
i.e the program should rearrange the operands and operators in such a way that it should give the same result.
View 5 Replies
View Related
Feb 3, 2013
I am creating a program that solves the quadratic equation ax^2 + bx +c.
I have this program almost complete except the output of the equation in the function called display_quadratic. I need the program to display the variables a,b,c in the equation ax^2 + bx + c but I am having 2 problems. My first problem is that I cannot get the right addition and subtraction signs for the equation.
For instance, if the program had the values for a,b,c as 2,2,3
it will display 2x^2 2x 3
How can I get it to display 2x^2 + 2x + 3? or if it was negative like 2x^2 - 2x - 3?
My next question is how do I get to not display the coefficients that are 1?
I had an if-else statement but no matter what I created it would overlap with another statement and print out twice. Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void display_quadratic (float a, float b, float c);
float root1(float a, float b, float c);
[Code] ......
View 2 Replies
View Related
Oct 7, 2013
This is my code for the quadratic equation. It keeps telling me that my else is illegal since no matching if statement and my else statement is missing a statement
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;
int main() {
string Name;
[Code] .....
View 3 Replies
View Related
Aug 23, 2014
I have tried writing a code which takes two numbers from the user and calculates their square root then the roots are added up to return the sum. The program is coming out with loads of errors.
#include<iostream>
#include<cmath>
float main(){
using namespace std;
float m1,m2,m3,m4,m5;
[Code] ....
View 4 Replies
View Related
Aug 17, 2013
Program which accepts two lines and and determines their intersection point and whether they lie within a circle, also given interactively. I'm racing against time and I've racked my skull to no avail
View 2 Replies
View Related
Feb 16, 2015
write codes that could solve a quadratic formula, and my codes are like this:
#include <bjarne/std_lib_facilities.h>
int main()
{
cout << "Enter the coefficients of a quadratic polynomial a*x**2 + b*x +c:
";
cout << " a? ";
double a;
cin >> a;
cout << " b? ";
[code]....
Which runs perfectly, but I have 2 questions:
1. How to simplify these code? On the assignment sheet the professor wrote about using void solve_linear(double b, double c); and void solve_ quadratic (double a, double b, double c);which I currently dont understand how these works. He asked us to write a well-encapsulated (as short as possible) program.
2. These are for extra points: the precision problem of floating numbers: professor asked us to find a way to get the precise answer of it, like this: Enter the coefficients of a quadratic polynomial a*x**2 + b*x +c:
a? 1
b? -20000
c? 1.5e-9
Trying to solve the quadratic equation 1*x*x + -20000*x + 1.5e-09 == 0
Using classical formula: Two roots, x = 20000 and x = 0
Using stable formula: Two roots, x = 20000 and x = 7.5e-14
My guess is that
A. Using code like if(x1*x2=a/c) to check if numbers were approximated.
B. Somehow determine the larger one in x1 and x2.
C. Somehow use that larger one to do something
View 2 Replies
View Related
Jul 6, 2014
So, I successfully made a program that will perform the quadratic equation on three numbers, imaginary or real. however, i am now trying to simplify the result, as to get rid of the "/2a" on the bottom. Hence the simplify() function. I just started to create the simplification function, and am attempting to divide the imaginary part of the solution as well as the real part of the solution by 2a. Somehow, it gives the error, "error:invalid operands of types 'int' and 'double *' to binary 'operator*'" on lines 105 and 106. I suspect it has to do with the pointers and references that i am passing as parameters. Also, just an aside, I have never actually seen "/=" be used. It can be, right? I know "+=" can be.
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>//simplify the answer
using namespace std;
int count=0;
//prototyping
double ans_1(double,double,double);
[Code] ....
View 5 Replies
View Related
Oct 23, 2013
The main point of the program is to calculate equations just like a standard calculator but I wanted to do it myself. I don't understand what the problem is right now but I've managed to create a program that asks for both values but somehow it doesn't want to ask for an operator (*, /, + etc). What's wrong with my code that the terminal skips the scanning part for the operator?
Code:
#include <stdio.h>
int main() {
int value1, value2, answer;
char operator;
[code]....
View 12 Replies
View Related
Apr 30, 2013
I wrote a program that solves an equation of two numbers, but in addition to that, I want it to be able to continue to solve longer equations. Ex: ( solves 2 * 4, or 2 * 4 - 5).I want to put this part of the program's result into a variable and go from there. How do I place the result of the calculation into a variable, and where would it go?
Code:
#include <stdio.h>
#include <string.h>
int main(void) {
int a;
scanf("%d", &a);
char s[2];
scanf("%s", s);
int b;
scanf("%d", &b);
}
[code]....
View 4 Replies
View Related
Nov 15, 2014
I have to make a numerical integration program, how I can write my code so that the user is able to write their own function that they want to integrate?
E.g. they would see the message: 'please enter your function' and would be able to write whatever they wanted e.g. 'x +5' then this would then be integrated by the program.
I have already written a program that can integrate a known function but would prefer that the user could choose their own.
View 2 Replies
View Related
Dec 14, 2014
I am trying to write a code that solves a system of linear equations such as A*B=C. My system has a dimension equal to 1600. The matrix A cab be separated into 4 sub matrices and each can be handled by a different thread. I tried to solve this using the following code:
int main() {
int count = 0;
//Inputing matrix A
ifstream matrix;
matrix.open("example.txt");
[Code] ....
Although the above code gives the correct answer, the time needs to find the solution is bigger than that needed without using threads.
View 1 Replies
View Related
Nov 10, 2013
Calculated by the explicit scheme. Produces some very large numbers.
task:
[math] U_t = 3 (1,1-0,5 x) U_ {xx} + e ^ t-1 [/ math]
[math] U (0, t) = 0 [/ math]
[math] U (1, t) = 0 [/ math]
[math] U (x, 0) = 0.01 (1-x) x [/ math]
Need to find a solution with accuracy [math] 0.0001 [/ math] on the interval [math] T = 1 / a ^ *, where a ^ * = max a (x, t) [/ math] Plot graphs of functions [math] u (x ^ *, t), u (x, jt ^ *) [/ math] where [math] x ^ * = 0.6, t ^ * = T/10, j = 1,2,4 [/ math]
explicit difference scheme is as follows:
([math] $ u_t ^ {j +1}-u_i ^ j) / tau = 3 (1,1-0,5 x_i) (u_ {i +1} ^ {j}-2u_i ^ j + u_ {i -1} ^ j) / h ^ 2 + e ^ {t_j} +1 $ $ [/ math]
code of the program:
int main ( void ) {
setlocale(LC_ALL, "rus");
int I = 10, J = 30, i, j;
double T = 1.0/ pow(3.3, 0.5), h_x = 1.0/ I, h_t = T/ J, epsilon = h_t + pow(h_x, 2), c;
double **u = new double *[I + 1];
for (i = 0; i <= I; i++) u[i] = new double [J + 1];
[code]....
displays the following:
[URL]
View 2 Replies
View Related
Dec 15, 2012
I have a problem: solve the system of equations by the Gauss-Jordan methods with pthreads. I have a big matrix A (for example 2000x2000), so i must solve Ax = b. Before I devide matrix to number of threads and each thread must work only with his peace of matrix.
Code:
#include <iostream>
#include "synchronize.h"
#include <pthread.h>
using namespace std;
typedef struct _ARGS {
int thread_count;
int thread_number;
[Code] .....
I write it on Ubuntu, and when I compile [g++ main.cpp -o main -lpthread -lm] the it works good(for example, if in 1 thread I get time_of_working = 10 sec, on 2 threads time_of_working = 5.4, i.e. about 2 times faster ), if I compile like this [g++ main.cpp -o main -lpthread -lm -O3] it is only 1.2-1.3 times faster.
View 1 Replies
View Related
Nov 12, 2014
I've made an effort for three days to write this code. But, my brain has stopped now. I wrote code to find the status of the game (win, loss or tie). However, I can't determine the tie status of the game. Tie status is the problem
View 5 Replies
View Related
Apr 16, 2013
I'm working with arrays that might have NULL bytes in them and I'm wondering how to determine the length of the array or store it somewhere with the array (strlen() won't work because of the NULL, right?).
I've found advice like store the length of the array in the first byte of the array, but since sizeof(size_t) is 8 should I leave the first 8 bytes for the length?
Would it be better do define my own structure which would store the array and its length? What's the usual way these things are handled in practice?
View 7 Replies
View Related
Apr 8, 2013
I would like to make a handler for input events, but not be required to define all the functions.For example, if I had
void mouseDown(int button,vec2 pos);
void mouseUp(int button,vec2 pos);
static void mouseStateHandle(int button,int state,int x,int y) {
switch(state){
case DOWN:
[code]....
in the 'mouseStateHandle' function? I don't know enough about C++ to be able to come up with a solution.
View 19 Replies
View Related
Nov 9, 2014
I am making an eVoting program which takes input from .txt file and outputs in the same .txt file. I need to ask the user to enter the candidate they wish to vote and then read the previous tally from the .txt file and add one to it. The problem is determining the numbers in a .txt file and adding one to it.
For example voting_Tally.txt contains:
Bloomberg 1234
Bill De Blasio 6789
How would it be possible to first determine the name and then add one to their tally. For Example:
Bloomberg 1235
Bill De Blasio 6790
View 2 Replies
View Related