Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:35:09+00:00 2026-06-05T14:35:09+00:00

I have to create a bisection program for quadratic equations. Here are the instructions

  • 0

I have to create a bisection program for quadratic equations. Here are the instructions for two individual steps:

The program has to meet the following requirements:

  1. Coefficients a,b, and c of the quadratic equation shall be read from the keyboard by a separate function named readCoeffs(). The function shall return to the caller a structure with all 3 coefficients read. t can be 3 fields of a structure or a single field with a 3-element array.

  2. Function, which calculates the roots, shall return the results to the caller in a form of a structure with 3 fields: root1, root2 and exists (a Boolean variable to determine if the roots exist).

I can’t write the function prototypes and function headings correctly. When I write them like this:

struct calcRoots(double, double, double, double quadfunc(double), bool&);

struct readCoeffs();

int main(){

I get errors. I think Microsoft Visual Studio expects me I to create structures with curly brackets and a semicolon following. I am very lost. Any feedback is greatly appreciated.

Here is the entire code I am working with. I am continually modifying it. This is my first course in c++. I am struggling.

/****************************************************************************
//File: bisect.cpp
//Finds the root of a function using the bisection method
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

struct coeffStru{
        double a;
        double b;
        double c;};
struct rootStru{
    double root1;
    double root2;
    bool exists;
               };

//function prototypes
struct calcRoots(double, double, double, double f(double),bool&);
double quadfunc(struct, double);
struct readcoeffs(coeffStru);

int main() {
    double xLeft=-10.0;
    double xRight=10.0; //end points of interval
    double epsilon;       //exists tolerance
    double root;          //root found by bisect
    bool exists;              //exists flag

//Get tolerance
cout<< "Enter tolerance: ";
cin>>epsilon;

struct coeffStru = readcoeffs();

//use bisect calcRoots function to look for root of function f
struct rootStru = calcRoots(xLeft, xRight, epsilon, quadfunc, exists);




//display result
if (!exists){
    cout<< "Root found at "<<root
        <<" \nValue of f(x) at root is: "<<f(root);
        system("PAUSE");
        return 0;
           }
else{
    cout<<"There may be no root in ["<<xLeft<<", "<<xRight<<"]"<<endl;
    system("PAUSE");
    return 0;
    }
}




//Struct return type readcoeffs function
struct readcoeffs(coeffStru){

    cout<<"Enter values for a,b,and c of a quadratic equation."<<endl;
    cin>>a>>b>>c;
    return struct coeffStru;
                            }








//Implements bisection method to find a root of function f
//in interval [xLeft, xRight].
//PRE: xLeft, xRight, and epsilon are defined.
//POST: Returns midpoint of xLeft and xRight if the difference between thses values is <= epsilon. Returns true to exists if there is no root.
struct calcRoots (double xLeft, double xRight, double epsilon, struct coeffStru, double quadfunc(double), bool& exists) //IN: endpoints of interval for possible root, IN: exists tolerance, IN: the function, OUT: exists flag
{
    double xMid;                        //midpoint of interval
    double fLeft;
    double fRight;              //function values at xLeft, xRight,
    double fMid;                        //and xMid


//Compute function values at initial endpoints of interval
    fLeft = quadfunc(xLeft,coeffStru);
    fRight = quadfunc(coeffStru, xRight);


    //Repeat while interval > exists tolerance
    while (fabs ( xLeft - xRight) > epsilon)
    {
        //Compute function value at midpoint
        xMid = (xLeft + xRight)/ 2.0;
        fMid = quadfunc(coeffStru, xMid);

        //Test function value and reset interval if root not found
        if(fMid ==0.0)      //if xMid is the root
            root1 = xMid;    //set root1 to xMid
        else{ 
            xRight = xMid;
            xMid = (xLeft + xRight)/ 2.0;
            fMid = f(coeffStru, xMid);
            }
            if(fLeft * fMid < 0.0)  //root in [xLeft, xMid]





        //Display next interval
        cout<< "New interval is [" <<xLeft
            << ", " <<xRight << "]"<<endl;
    }    //ends 1st while loop


    //If no change of sign in the interval, there is no unique root
    exists = (fLeft * fRight) >0;  //test for same sign - set exists
    if(exists){ 
        return -999.0;      //no 1st root - return to caller
              }


    //Return midpoint of last interval
    root1 = (xLeft + xRight) / 2.0;
//Compute function values at initial endpoints of interval
    fLeft = f(coeffStru, xLeft);
    fRight = f(coeffStru, xRight);



    //Repeat while interval > exists tolerance
    while (fabs ( xLeft - xRight) > epsilon)
    {
        //Compute function value at midpoint
        xMid = (xLeft + xRight)/ 2.0;
        fMid = f(coeffStru, xMid);

        //Test function value and reset interval i root not found
        if(fMid ==0.0)      //xMid is the root
            return xMid;    //return the root
        else if (fLeft * fMid < 0.0)  //root in [xLeft, xMid]
            xRight = xMid;
        else                            //root in [xMid, xRight]
            xLeft = xMid;

        //Display next interval
        cout<< "New interval is [" <<xLeft
            << ", " <<xRight << "]"<<endl;
     }    //ends 2nd while loop


    //If no change of sign in the interval, there is no unique root
    exists = (fLeft * fRight) > 0;  //test for same sign - set exists
    if(exists){ 
        return -999.0;      //no 2nd root - return to caller
              }


    //Return midpoint of last interval
    root2 = (xLeft + xRight) / 2.0;

return struct rootStru;
}   //ends calcRoots method




//Function whose root is being found.
//PRE: x is defined.
//POST: Returns f (x)
double quadfunc(struct coeffStru, double x){
    return a * pow(x, 2.0) - b* pow(x, 1.0) + c;}


************************************************************/

I am getting many errors as you can imagine. My major issue now seems to be passing variables and structs by reference and values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:35:10+00:00Added an answer on June 5, 2026 at 2:35 pm
    struct calcRoots(double, double, double, double quadfunc(double), bool&);
    

    struct by itself is not a type. You should say the name of struct being returned. So, what kind of struct is calcRoots expected to return ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have create following structure. Here levels are up to 4 and groups are
I have create following string multidimensional array in java. which has top outer array
I have create a socket with CFSocket. My program is correct but now i
I have create the class HoverButton which derives from Form.Button. Here I override the
I am trying to create a program in C++ that will use the bisection
i have create a table in sqlite that giving me java.lang.ArrayIndexOutOfBoundsException here is code
I have create testing application that has 3 classes Car Radio SportCar : Car
I have create to fields in my form and I have place in, two
I have create a HTML page with some JavaScript code. Here is the code
I have create two tables with reference with another table: I like this: Table1:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.