This is probably the worst prof I have ever had so I have no idea what to do to make this program. Help would be greatly appreciated.
Specifications:
Function poly(given x, z, a, b, c; returns y)
Function roots(given degree, left, right, z, a, b, c, &secondRoot; returns estimateOfRoot)
Note: The roots function has a switch structure to process either a polynomial of degree 2 or 3 with case 2: to handle quadratic formula that returns two roots, one through the normal return statement via estimateOfRoot and a second root through the reference parameter &secondRoot; and with case 3: to handle the cubic polynomial with the incremental search technique.
Your main module should drive the roots function with PIE from the user for degree, coefficients, interval limits, and step size until the user decides to quit the program with a value of -1 for degree, that is, an input sentinel of -1 to terminate a loop in the main module that drives your Polynomial Roots Problem.
5 1530
We cant write the code for you.
Tell us what problem u are facing with this.
Raghu
Here, I wrote psuedocode for it. Does this seem correct?
Main: read coefficients, interval endpoints a and b,
and step size
compute the number of subintervals, n
set k to 0
while k<= n-1
compute left subinterval endpoint
compute right subinterval endpoint
check_roots (left, right, coefficients)
increment k by 1
check_roots (b, b, coefficients)
check_roots (left, right, coefficients);
set f_left to poly(left, coefficients)
set f_right to poly(right, coefficients)
if f_left is near zero
print root at left endpoint
else
if f_left * f_right < 0
print root at midpoint of subinterval
return
poly(x,a0,a1,a2,a3);
return a0x^3 + a1x^2 + a2x + a3
New Function
#include <iostream>
#include <cmath>
Using namespace std;
int main()
{
int n;
double a0, a1, a2, a3, a, b, step, left, right;
cout<<”Enter coefficients a0, a1, a2, a3: \n”;
cin>>a0>>a1>>a2>>a3;
cout<<”Enter interval limits a, b (a<b): \n”;
cin>>a >>b;
cout<<”Enter step size: \n”;
cin>>step;
n= ceil((b-a)/step);
for (int k=0; k<=n-1; k++)
{
Left = a + k*step;
If (k == n-1)
{
Right=b;
}
}
Else
{
right=left+step;
}
Check_roots(left,right,a0,a1,a2,a3);
}
Check_roots(b,b,a0,a1,a2,a3);
Return 0;
}
New Function
Void check_roots(double left, double right, doubl a0, double a1, double a2, double a3)
{
Double f_left, f_right;
F_left = poly(left,a0,a1,a2,a3);
F_right=poly(right,a0,a1,a2,a3);
If (fabs(f_left) < 0.1e-04)
{
Cout<< “root detected at “ << left << endl;
}
If (fabs(f_right)<0.1e-04)
;
Else
{
If (f_left*f_right <0)
{
Cout<<”root detected at “<< (left+right)/2)<<endl;
}
}
}
Return;
}
New Function
Double poly(double x, double a0, double a1, double a2, double a3)
{
Return a0*x*x*x+a1*x*x+a2*x+a3;
}
Where is the switch structure that was mentioned in your specs?
Better write the code (according to the specs) and post only if you get specific problems with it. Just dumping some pseudo code on us and asking us if it's correct is a no no.
The descriminant value shows up but the roots do not output. Any thoughts?
Heres the problem specifications.
Function poly(given x, z, a, b, c; returns y)
Function roots(given degree, left, right, z, a, b, c, &secondRoot; returns estimateOfRoot)
Note: The roots function has a switch structure to process either a polynomial of degree 2 or 3 with case 2: to handle quadratic formula that returns two roots, one through the normal return statement via estimateOfRoot and a second root through the reference parameter &secondRoot; and with case 3: to handle the cubic polynomial with the incremental search technique.
Your main module should drive the roots function with PIE from the user for degree, coefficients, interval limits, and step size until the user decides to quit the program with a value of -1 for degree, that is, an input sentinel of -1 to terminate a loop in the main module that drives your Polynomial Roots Problem. -
#include "head.h"
-
int main()
-
{//start main
-
double a = 0, b = 0, c = 0, z = 0, stepsize = 0, leftbound = 0, rightbound = 0, secondroot = 0, root = 0;
-
int degree = -1;
-
cout << "Enter the desired degree(2 or 3) or type (-1) to quit: ";
-
cin >> degree;
-
-
while(degree != -1)
-
{
-
while((degree != 2) && (degree != 3))
-
{
-
cout << "\nThis is not an option." << endl;
-
cout << "Enter the desired degree (2 or 3): ";
-
cin >> degree;
-
}
-
cout << "\nEnter a, b, c, and z: ";
-
cin >> a;
-
cin >> b;
-
cin >> c;
-
cin >> z;
-
-
if(degree == 3)
-
{
-
-
cout << "\nPlease enter the step size, right bound, and left bound: ";
-
cin >> stepsize;
-
cin >> rightbound;
-
cin >> leftbound;
-
}
-
-
root = roots(degree, leftbound, rightbound, stepsize, z, a, b, c, secondroot);
-
-
if (degree == 2)
-
{//start if
-
if (root != -9999)
-
{//start inner if
-
cout << "Root 1 is: " << root << endl;
-
cout << "Root 2 is: " << secondroot << endl;
-
}//end inner if
-
else
-
{//start else
-
cout << "Root 1 is an imaginary and can not be calculated" << endl;
-
cout << "Root 2 is an imaginary and can not be calculated" << endl;
-
}//end else
-
}// end if
-
-
cout << "\nEnter the desired degree (2 or 3) or type (-1) to quit: ";
-
cin >> degree;
-
-
}//end while
-
}// end main
-
-
-
Poly.cpp FUNCTION
-
double poly(double x, double z, double a, double b, double c)
-
-
{//start poly
-
-
double y;
-
-
y=z*pow(x,3)+a*pow(x,2)+b*x+c;
-
-
return y;
-
-
}//end poly
-
-
-
-
-
Roots.cpp FUNCTION
-
double roots(int degree, double leftbound, double rightbound, double stepsize, double z, double a, double b, double c, double &secondRoot)
-
{//start root
-
double i = leftbound, test1 = 0, test2 = 0, root = 0, descrim = 0, estimateOfRoot = 0, rootnum = 0;
-
-
switch(degree)
-
{//start switch
-
case 2:
-
// aX^2 + bX + c
-
descrim = pow(-b,2) - 4 * a* c;
-
cout << "The Descriminant is: " << descrim << endl;
-
if(descrim < 0)
-
{//start if
-
-
estimateOfRoot = -9999;
-
secondRoot = -9999;
-
}// end if
-
else
-
{//start else
-
-
estimateOfRoot = ((-b + sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
-
secondRoot = ((-b - sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
-
}//end else
-
-
return estimateOfRoot;
-
-
break;
-
-
case 3:
-
-
descrim = pow(-b,2) - 4 * a* c;
-
cout << "\nThe Descriminant is: " << descrim << endl;
-
while(i < rightbound)
-
{// start while
-
test1 = poly(i, z, a, b, c);
-
test2 = poly(i + stepsize, z, a, b, c);
-
-
if(abs(test1) < 0.0001)
-
{//start if
-
-
root = i;
-
cout << "There is a root at: " << root << endl;
-
rootnum++;
-
}// end if
-
else if((test1 * test2) < 0)
-
{// start else
-
-
root = ((i + stepsize) + i) / 2;
-
cout << "There is a root at: " << root << endl;
-
rootnum++;
-
}//end else
-
-
i += stepsize;
-
}// end while
-
}
-
}
The descriminant value shows up but the roots do not output. Any thoughts?
Yes. Debug your code. Why don't you cout << root straight after
root = roots(degree, leftbound, rightbound, stepsize, z, a, b, c, secondroot);
to see what comes out? If the roots dont show then what happens instead? Does your program crash? Did you print out intermediate variables in your function like a,b,c to see they are what you expected? By the way, if degree==3 then the way you've written it you will never see your roots.
Please Use CODE tags around your code.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kieran Dutfield |
last post by:
Code:
ALTER PROCEDURE GetBookedResource
(
@StartDate datetime,
@EndDate datetime,
@Resource char(30)
)
AS
SELECT *
|
by: Fran?ois Laroche |
last post by:
hey guys, I know that you will tell me that's easier to do in php or
even in asp, but I just dont have the time to learn it
1- I need a javascript that will show the date of tomorrow
2- I need a...
|
by: Bill |
last post by:
Hello out there;
This may be a challenge but I'm certain it's possible but I can't seem
to figure out how.
I have a table that has several date fields, e.g., Date1, Date2,
Date3, Date4 ......
|
by: Froggy / Froggy Corp. |
last post by:
Thx for your quick answer too :)
Richard Huxton wrote:
>
> On Wednesday 18 February 2004 20:18, Froggy / Froggy Corp. wrote:
> > Hello,
> >
> > I asked one time for more "benchmark"...
|
by: Chris |
last post by:
Hello,
I am attempting to build a MS SQL query that will return data from
"today"; today being current day 8:00AM-10:00PM today. My goal is to
return the data from a table that is written to...
|
by: Cheryl Langdon |
last post by:
Hello everyone,
This is my first attempt at getting help in this manner. Please
forgive me if this is an inappropriate request.
I suddenly find myself in urgent need of instruction on how to...
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that...
|
by: bean330 |
last post by:
Hey, I'm somewhat new to C# and I need a little help, please!
I'm selecting a bunch of records, setting properties on a COM
executable and then calling a method on that executable to run. I...
|
by: WebCM |
last post by:
There is a function: http://paste.ubuntu.com/21865
It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as today, the function returns "Today". There
is one...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |