473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

I need help: Time is limited. Due tomorrow!

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.
Nov 4 '08 #1
5 1530
gpraghuram
1,275 Expert 1GB
We cant write the code for you.
Tell us what problem u are facing with this.

Raghu
Nov 4 '08 #2
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;
}
Nov 4 '08 #3
r035198x
13,262 8TB
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.
Nov 4 '08 #4
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.


Expand|Select|Wrap|Line Numbers
  1. #include "head.h"
  2. int main()
  3. {//start main
  4.     double a = 0, b = 0, c = 0, z = 0, stepsize = 0, leftbound = 0, rightbound = 0, secondroot = 0, root = 0;
  5.     int degree = -1;
  6.     cout << "Enter the desired degree(2 or 3) or type (-1) to quit: ";
  7.     cin >> degree;
  8.  
  9.     while(degree != -1)
  10.     {
  11.         while((degree != 2) && (degree != 3))
  12.         {
  13.             cout << "\nThis is not an option." << endl;
  14.             cout << "Enter the desired degree (2 or 3): ";
  15.             cin >> degree;
  16.         }
  17.             cout << "\nEnter a, b, c, and z: ";
  18.             cin >> a;
  19.             cin >> b;
  20.             cin >> c;
  21.             cin >> z;
  22.  
  23.             if(degree == 3)
  24.             {
  25.  
  26.                 cout << "\nPlease enter the step size, right bound, and left bound: ";
  27.                 cin >> stepsize;
  28.                 cin >> rightbound;
  29.                 cin >> leftbound;
  30.             }
  31.  
  32.             root = roots(degree, leftbound, rightbound, stepsize, z, a, b, c, secondroot);
  33.  
  34.             if (degree == 2)
  35.             {//start if
  36.                 if (root != -9999)
  37.                 {//start inner if
  38.                     cout << "Root 1 is: " << root << endl;
  39.                     cout << "Root 2 is: " << secondroot << endl;
  40.                 }//end inner if
  41.                 else
  42.                 {//start else
  43.                     cout << "Root 1 is an imaginary and can not be calculated" << endl;
  44.                     cout << "Root 2 is an imaginary and can not be calculated" << endl;
  45.                 }//end else
  46.             }// end if
  47.  
  48.             cout << "\nEnter the desired degree (2 or 3) or type (-1) to quit: ";
  49.             cin >> degree;
  50.  
  51.     }//end while
  52. }// end main
  53.  
  54.  
  55. Poly.cpp FUNCTION
  56. double poly(double x, double z, double a, double b, double c) 
  57.  
  58. {//start poly 
  59.  
  60. double y; 
  61.  
  62. y=z*pow(x,3)+a*pow(x,2)+b*x+c; 
  63.  
  64. return y; 
  65.  
  66. }//end poly
  67.  
  68.  
  69.  
  70.  
  71. Roots.cpp FUNCTION
  72. double roots(int degree, double leftbound, double rightbound, double stepsize, double z, double a, double b, double c, double &secondRoot)
  73. {//start root
  74.     double i = leftbound, test1 = 0, test2 = 0, root = 0, descrim = 0, estimateOfRoot = 0, rootnum = 0;
  75.  
  76.     switch(degree) 
  77.     {//start switch
  78.         case 2:
  79.             // aX^2 + bX + c
  80.             descrim = pow(-b,2) - 4 * a* c;
  81.             cout << "The Descriminant is: " << descrim << endl;
  82.             if(descrim < 0)
  83.             {//start if
  84.  
  85.                 estimateOfRoot = -9999;
  86.                 secondRoot = -9999;
  87.             }// end if
  88.             else
  89.             {//start else
  90.  
  91.                 estimateOfRoot = ((-b + sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
  92.                 secondRoot = ((-b - sqrt(pow(-b,2) - 4 * a* c))/ (2*a));
  93.             }//end else
  94.  
  95.             return estimateOfRoot;    
  96.  
  97.             break;
  98.  
  99.         case 3:
  100.  
  101.             descrim = pow(-b,2) - 4 * a* c;
  102.             cout << "\nThe Descriminant is: " << descrim << endl;
  103.             while(i < rightbound)
  104.             {// start while
  105.                 test1 = poly(i, z, a, b, c);
  106.                 test2 = poly(i + stepsize, z, a, b, c);
  107.  
  108.                 if(abs(test1) < 0.0001)
  109.                 {//start if
  110.  
  111.                     root = i;
  112.                     cout << "There is a root at: " << root << endl;
  113.                     rootnum++;
  114.                 }// end if
  115.                 else if((test1 * test2) < 0)
  116.                 {// start else
  117.  
  118.                     root = ((i + stepsize) + i) / 2;
  119.                     cout << "There is a root at: " << root << endl;
  120.                     rootnum++;
  121.                 }//end else
  122.  
  123.                 i += stepsize;
  124.             }// end while
  125.     }
  126. }
Nov 4 '08 #5
arnaudk
424 256MB
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.
Nov 4 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kieran Dutfield | last post by:
Code: ALTER PROCEDURE GetBookedResource ( @StartDate datetime, @EndDate datetime, @Resource char(30) ) AS SELECT *
19
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...
8
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 ......
3
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"...
4
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...
15
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...
12
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...
5
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...
10
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.