New to CS; Need some help with HW

Hi guys.
I'm taking my first CS course this fall.
Pretty neat stuff.
So we have this assignment due in a couple days.
I have it working and everything; I'm just not getting the right values.

So this is the prompt:
"Design a C++ program that will:

Use Cramer's Rule to solve for x and y. (Remember that x and y may not turn out to be whole numbers, so declare them of an appropriate data type.)
Display the original equations (see sample output below).
Display the computed values of x and y with labels. Do NOT set precision for these values."

Here is my source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

int main ()
{
  int a; // Declaring the value of a as an integer.                                                                                                                                            
  int b;
  int c;
  int d;
  int e;
  int f;
  float x; // Declaing the value of x as more than just an integer.                                                                                                                            
  float y;

  cout << " " <<  endl;
  cout << "My Name" <<  endl;
  cout << "CS 135 Section 1003" <<  endl;
  cout << "Assignment 03" <<  endl;
  cout <<" " <<  endl;

  cout <<"This program will solve for x and y using Cramer's Rule." << endl;
  cout << "The following two equations shall be used: \n ax + by = c \n dx +ey = f \n \n";

  cout << "Enter the integer value for a" << endl;
  cin >> a;                                                        
  cout << "Enter the integer value for b" << endl;
  cin >> b;
  cout << "Enter the integer value for c" << endl;
  cin >> c;
  cout << "Enter the integer value for d" << endl;
  cin >> d;
  cout << "Enter the integer value for e" << endl;
  cin >> e;
  cout << "Enter the integer value for f" << endl;
  cin >> f;

  cout << "" << endl;
  cout << "Your first equation is: " << a << "x +" << b << "y = " << c <<   endl;
  cout << "Your second equation is: " << d << "x +" << e << "y = " << e <<  endl;
  cout << "" << endl;

  x= ((c * e) - (b * f))/((a * e) - (b * d));
  y= ((f * a) - (c * d))/((a * e) - (b * d));

  cout << "x=" << x << endl;
  cout << "y=" << y << endl;
  cout << "" << endl;


  return 0;
}


The values that I get for x and y are incorrect.
I know that there's a problem with int or float or something akin to that.
Any help would be greatly appreciated.
Last edited on
what the value of "x" and "y" supposed to be? a double data type or something? i don't really get it...
try declaring all the int as double or float because if I remember correctly an operation between integers will return an integer
Lines 42 and 43 should be:

1
2
  x= static_cast<float>((c * e) - (b * f))/((a * e) - (b * d));
  y= static_cast<float>((f * a) - (c * d))/((a * e) - (b * d));


otherwise you'll get integer division.

Also, you need to replace that last e on line 39 with f.
@chipp The values for x and y need to be more then just integers, I was getting the wrong values because it was doing integer division and not displaying values that had more then 1 decimal place.

@ccsdude The assignment asked to define all variables as integers expect for x and y.

@cire Thank you! :)
Topic archived. No new replies allowed.