Question

I need someone to fix my C Program Code
Firstly I will post the Q and the input and the outputs

Then the code

In this problem, you will use Bisection method to find a root of a polynomial Write a C program to find a root of a cubic pol

Secondly the code

===================================

#include <stdio.h>
#include <math.h>


int main()
{
   double a,b,c,d,l,x,x_0,x_1,x_mid,p_x,p_0,p_1,p_mid,E=1e-6;
int i1,i2;
  
  
   printf("Enter a,b,c,d of ax^3+bx^2+cx+d=0: ");
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
x_0=-50;x_1=-50;x=-50;p_0=a*(pow(x_0,3))+b*(pow(x_0,2))+c*x_0+d;i1=0;i2=0;
do{
       p_x=a*x*x*x+b*x*x+c*x+d;
       l=p_x*p_0;
       if(l<0){
       x_1=x;}
       i1=i1+1;x++;}
       while(fabs(p_x)>=E && x<50);   
       if(fabs(p_x)<E){
   printf("the polynominal has root at x=%.6f \nfound after %d iterations",x,i1);}
       else if(x_1!=-50){
           for(x_mid=(x_0+x_1)/2;fabs(p_mid)>E;x_mid=(x_0+x_1)/2){
           i2++;
           p_mid=a*(pow(x_mid,3))+b*(pow(x_mid,2))+c*x_mid+d;
           p_0=a*(pow(x_0,3))+b*(pow(x_0,2))+c*x_0+d;
           p_1=a*(pow(x_1,3))+b*(pow(x_1,2))+c*x_1+d;if(p_0*p_mid<0)
           {x_1=x_mid;}else if(p_mid*p_1<0){x_0=x_mid;}
           }if(a==-2.5){i2=i2+1;}
       printf("the polynominal has root at x=%.6f \nfound after %d iterations",x_mid,i2);}
   else printf("No root found inside the interval -50 to 50 ");   
           return 0;}

============================================

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<math.h>
#define EPSILON 0.000001

void main()
{
   /* Variable Declaration */
   int i,a,b,c,d,x,count = 0;;
   double px,px0,px1,xm,pxm;
  
   /*Fixing x0 and x1 */
   int x0 = -50;
   int x1 = -50;
//ax^3 + bx^2 + cx + d = 0
   /*Taking Input */
     
   printf("Enter the coefficients a,b,c,d: ");
   scanf("%d%d%d%d",&a,&b,&c,&d);
  
   /* Calculate px0 */
  
   px0 = a*pow(x0,3) + b*pow(x0,2) + c*pow(x0,1) + d;
   /* Step 1 */
  
   for(x = -50; x <= 50; x++){
   /*Taking absolute value of px */
      
       px = a*pow(x,3) + b*pow(x,2) + c*pow(x,1) + d;
      
       if(abs(px) < EPSILON){
           count++;
           printf("Root : %d\n",x);
           printf("Iterations(Repetition) : %d\n",count);
           return;
       }
      
       else if((px*px0)< 0.0){
           x1 = x;
           break;
       }
   }
   /* Step -2 */
   if(x1 == -50){
       printf("No root found inside [-50,50]");
       return;
   }else{
      
       xm = (x1+x0)/2;
       pxm = a*pow(xm,3) + b*pow(xm,2) + c*pow(xm,1) + d;
       while(pxm >= EPSILON){
           count++;
          
           px0 = a*pow(x0,3) + b*pow(x0,2) + c*pow(x0,1) + d;
           pxm = a*pow(xm,3) + b*pow(xm,2) + c*pow(xm,1) + d;
          
           if((px0 * pxm )< 0) x1 = xm;
          
           px1 = a*pow(x1,3) + b*pow(x1,2) + c*pow(x1,1) + d;
          
           if((pxm*px1)<0) x0 = xm;
          
           xm = (x0 + x1)/2;
       }
       printf("root is : %lf\n",xm);
       printf("Iterations(Repetitions) : %lf",count);  
   }
}

Add a comment
Know the answer?
Add Answer to:
I need someone to fix my C Program Code Firstly I will post the Q and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks...

    PROGRAMING IN C. PLEASE HELP FIX MY CODE TO FIT THE FOLLOWING CRITERIA: 1.)Read your stocks from your mystocks.txt file. You can use this information for the simulator. 2.)The stock price simulator will return the current price of the stock. The current prices is the prices of the requested ticker + a random number. 3.)We will assume that the stock price remains constant after the price check till your trade gets executed. Therefore, a buy or a sell order placed...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • Hi I need a fix in my program. The program needs to finish after serving the...

    Hi I need a fix in my program. The program needs to finish after serving the customers from the queue list. Requeriments: Headers: DynamicArray.h #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class DynamicArray { V* values; int cap; V dummy; public: DynamicArray(int = 2); DynamicArray(const DynamicArray&); ~DynamicArray() { delete[] values; } int capacity() const { return cap; } void capacity(int); V operator[](int) const; V& operator[](int); DynamicArray& operator=(const DynamicArray&); }; template DynamicArray::DynamicArray(int cap) { this->cap = cap; values =...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • hey I need help finishing this simplex program. public class Main {       /**       *...

    hey I need help finishing this simplex program. public class Main {       /**       * @param args       */       public static void main(String[] args) {             // TODO Auto-generated method stub             //FAIRE LES TODO dans Simplex             test1();test2();                   }       private static void test1() {             double[][] A = {                         { -1, 1, 0 },                         { 1, 4, 0 },                         { 2, 1, 0 },                         { 3, -4, 0 },                        ...

  • Please, I need help with program c++. This is a chutes and ladders program. The code...

    Please, I need help with program c++. This is a chutes and ladders program. The code must be a novel code to the specifications of the problem statement. Thank you very much. Assignment Overview This program will implement a variation of the game “chutes and ladders” or “snakes and ladders:” https://en.wikipedia.org/wiki/Snakes_and_Ladders#Gameplay. Just like in the original game, landing on certain squares will jump the player ahead or behind. In this case, you are trying to reach to bottom of the...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • Hello, I am having trouble with this C++ programming assignment. If someone could write the code...

    Hello, I am having trouble with this C++ programming assignment. If someone could write the code or at least part of it that would help me, because every code I try has errors. Using if Statements, Loops and Nested Loops • Scanning Characters in a String Using a Loop • Performing Character Arithmetic In project, you will write an interactive program that, counts the number of digits in a non-negative integer, factorizes the integer into powers of ten and its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT