Notices
Results 1 to 1 of 1

Thread: computer science program help?

  1. #1 computer science program help? 
    New Member
    Join Date
    Mar 2009
    Posts
    1
    i am horrible in comp sci. and this is wha ti have. it may be ALL wrong but PLEASE help.

    #include <iostream>
    #include <cmath>
    #include <complex>
    using namespace std;
    int main ()
    {
    double d, d1, d2, d3, d4, a, b, c, i;

    cout << "Enter a, b, and c for the quadradic equation: " << endl;
    cin >> a >> b >> c;

    d = b * b- 4.0 * a * c;
    d1 = -1 * b + sqrt(-d * i)/2.0 * a;
    d2 = -1 * b - sqrt(-d * i)/2.0 * a;
    d3 = -1 * b + sqrt(d)/2.0 * a;
    d4 = -1 * b - sqrt(d)/2.0 * a;

    {
    if (d > 0)
    cout << "Since the discriminannt = " << d << endl;
    cout << "The two roots are real: " << endl;
    cout << "Root 1= " << d3 << endl;
    cout << "Root 2= " << d4 << endl;}
    {
    else (d<0)
    cout << "Since the discriminat = " << d << endl;
    cout << "The two roots are complex, i.e has a real and an imaginary part:" << endl;
    cout << "Root 1= " << d1 << endl;
    cout << "Root 2= " << d2 << endl;
    }


    return 0;
    }



    now what the problem is that i am having is that i can't get the i for imaginary numbers to show.

    professors instructions : We pointed out then that if the discriminant d = b2 – 4ac is negative, the library function sqrt(d) will not work because the precondition for it work properly is that the argument d must be positive. In theory, however, one is allowed to take the square root of a negative number. Let’s assume that d is negative. Knowing the fact that sqrt(-1) = i, where i is known as an imaginary number, one can rewrite sqrt(d) as sqrt(-d * -1) = sqrt(-d) * sqrt(-1) = sqrt(-d) * i. Since d is negative, -d must be positive, so the sqrt(-d) will compute correctly. With the understanding of the above discussion, you are asked to rewrite the quadratic equation program such that the solution will be correct regardless of the value of the discriminant, negative or positive. Notice that each root may in general has two parts: the real and imaginary parts. For example, a root may have look like 3.25 – 7.2i, where the real part of the root is 3.25 and the imaginary part is -7.2. So you need to declare two variables for each root, one to store the real part; the other stores the imaginary part. (Note: complex numbers are a powerful tool for solving numerous engineering and science problems. Therefore, complex data type has been preprogrammed and is part of C++ built-in class).

    Your program outputs should be similar to that of two the test runs shown below:

    First run:
    Enter a, b, and c for the quadratic equation: 2 5 1
    Since discriminant = 17.00 >= 0
    The two roots are real:
    Root 1 = -0.22 or (-0.22, 0.00)
    Root 2 = -2.28 or (-2.28, 0.00)
    Second run:
    Enter a, b, and c for the quadratic equation: 5 2 4
    Since discriminant = -76.00 < 0
    The two roots are complex, i.e., it has a real and an imaginary parts:
    Root 1 = -0.20 +0.87i or (-0.20, +0.87)
    Root 2 = -0.20 -0.87i or (-0.20, -0.87)


    Reply With Quote  
     

  2.  
     

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •