Archived

This topic is now archived and is closed to further replies.

mink_trapper

Whats wrong with my code?

Recommended Posts

Hello GDNet members, I am sorta new to C++ and for my first program i tried to make a program that will solve the quadratic equation for me. After finishing my program and compiling and running, I tested it out. It didnt work... Here it is for those of you that can help.
#include <iostream>
#include <stdlib.h>
#include <math.h>

int Newfunc(int d, int b, int a, int g);

int main(int argc, char *argv[])
{
  int a, b, c, d, e, f, g, h;
  
  
  cout << "                       Welcome to Insertname" << endl;
  cout << "                    Brought to you by Nickware" << endl;
  cout << " " << endl;
  cout << "The quadratic formla is ax^2+bx+c" << endl;
  cout << " " << endl;
  cout << " What is A?" << endl;
  cin >> a;
  cout << "What is B?" << endl;
  cin >> b;
  cout << "What is C?" << endl;
  cin >> c;
  cout << "your equation is " << a << "x^2+" << b << "x+" << c << endl;
  system ("PAUSE");
  d = (b * b) - (4 * a * c); 
    if (d < 0)
    {
    Newfunc(d,b,a,g);
    return 0;
    } 
  e = sqrt (d);
  f = (-b + e) / (2 * a);
  g = (-b - e) / (2 * a);
  cout << "The solutions are " << f << " and " << g <<endl;
  system ("PAUSE");
}

int Newfunc(int d, int b, int a, int g)
{
    int h;
    h = sqrt (-d);
    cout << "                   Complex Solutions:" << endl;
    cout << "The real part is" << - b / (2 * a) << endl;
    cout << "The imaginary part is " << g / (2 * a); 
    system ("PAUSE");
}
______________________________________________________________ "It takes all kinds." -Adam Carolla

Share this post


Link to post
Share on other sites
Looks right (I didn''t read the whole thing), but your problem is probably as a result of the fact that all of your variables are ints. Ints only stores integers (meaning whole numbers), while most of the numbers involved in the quadratic equation are decimal numbers. Use double instead.

The official zorx website

Share this post


Link to post
Share on other sites
mink, your problem is here:


int Newfunc(int d, int b, int a, int g)
{
int h;
h = sqrt (-d);
cout << " Complex Solutions:" << endl;
cout << "The real part is" << - b / (2 * a) << endl;
cout << "The imaginary part is " << g / (2 * a);
system ("PAUSE");
}


your function is supposed to return an int...it returns nothing....the rest of the program should work without a hitch, but man, you've got variables galore! Regardless of efficiency, the proggy should work once you work on your function.

[edited by - nervo on January 24, 2004 10:30:14 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by PlayGGY
Yeah, clum is right.

But mink_trapper, aren't you the guy you illegally downloaded Visual Studio...?


Yea i am, and im sorry fo r it (I dont have it anymore, i deleted it)

EDIT: Whats a double? I dont think im ther in my book yet.
______________________________________________________________

"It takes all kinds." -Adam Carolla


[edited by - mink_trapper on January 25, 2004 3:28:34 AM]

Share this post


Link to post
Share on other sites
A double is a 64 bit floating point datatype

It can hold numbers in the range approximately +/- 10^308, with a precision of about 16 digits

Notice however, that precision will be lost when performing arithmetic operations on the numbers.

You could also use extended doubles (which are the native format of the fpu in most modern computers). They have a 64bit mantissa, and a 16bit exponent, making for a higher range and precision. (and they support none normalized values)

Share this post


Link to post
Share on other sites