How to Calculate Algebra problems

I'm taking a computer science class that deals mostly with coding. I'm terrible at stuff like this but it's a graduation requirement (even though my major has nothing to do with this) So I might as well start learning.

One of our assignments has two questions that I wouldn't even begin to know how to answer. I have to write a program that solves these two equations

1) F = K (M1M2)/d^2

K = 6.67 * (10^-11 * m^3 * kg^-1 * m^-2)

I need to write a program that prompts the user to input the masses of the bodies and the distance between the bodies, and then outputs the force between the bodies.


2) (ax + b)(cx + d)

this one seems simpler. I just need the program to solve these problems using the FOIL method, letting the user input the values for a, b, c, and d


If anyone could help me understand how to write these programs, I'd really appreciate it :)
Last edited on
These are actually very simply to write; you only need how to prompt the user for input and how to assign to a variable (the formulas you have will almost work just copied in to code - just remove the units and rewrite the squaring portions).

What kind of program can you write? I'd like to know what point I need to explain from.
The only programs I've been able to write are like the most basic of basic programs. Like that "Hello World" one. We haven't really gotten very far in the class. I use VisualStudio Express if that helps
Last edited on
Sorry. I don't mean to rush but this is for an assignment that's due tomorrow.
Well you have the equations already. To get input you use cin >> instead of cout << so simply get the input then assign the values to a variable and output the values. Though keep in mind that an integer/integer = integer but if you make one a floating point(float or double) it will result in a floating point (float or double). You can do this by casting with (type) where type is float or double or static_cast<type>(variable or value) or adding a '.', '.0' or '.f' to the end of the value. Though these shouldn't really be a problem when dealing with masses/distances.

I'm sure you already know what the multiply and divide operators are so you would do something like:
1
2
3
4
5
6
double mass1 = 0.0;
double mass2 = 0.0f;

//get the input using cin >>

double k = 6.67 * 0.00000000001 * mass1 * masss1 * mass1 * weight / 10.0 * mass1 * mass1 / 100.0; //who knows which mass m is :P 
and then you would do the same for the rest of it.

Good luck on the assignment :) I probably won't be able to help you more unless it's due in more than 20 hours.
Last edited on
Thanks. This definitely helps and I kind of have but most of this stuff feels like an alien language. Here's my attempt but Visual Studio is telling me I'm doing something wrong.

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
#include <iostream>

using namespace std;

int main()
{


	double m1;
	double m2;
	double d;
	double k = 6.67 * (10 ^ -11 * m^3 * kg^-1 * m^-2);


	cout << "Mass of the first person" << endl;
	cin >> m1;

	cout << "Mass of the second person" << endl;
	cin >> m2;

	cout << "Distance between bodies" << endl;
	cin >> d;

	double Force = k*((m1*m2 / d ^ 2));
	cout << Force << endl;

	system("pause");

	return 0;



}


That's about all I really know how to do
Last edited on
First and foremost, your formula for calculating the gravitational force between two objects is wrong :)

This program should work just fine. It also has the right formula.
The second program is way simpler using the same fashion. This one is in fact a piece of cake too.

Please try to learn by doing (that's the purpose of the assignment I guess).

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
#include<iostream>

using namespace std;
 
int main()
{
    double mass1, mass2, k, result; 
    k = 6.674 * 0.00000000001; 
    
    cout << "enter the mass of the first object" << endl;
    cin >> mass1;
    
    cout << "enter the mass of the first object" << endl;
    cin >> mass2;
    
    cout << "enter the distance between the objects" << endl;
    cin >> d;
    
// your formula for gravitational force calculation is wrong. This is the right one :)
    result = (k * mass1 * mass2) / (d * d); 
    
    cout << "The gravitational force is: " << result <<" N" << endl;
    
    system("pause");
    
	return 0;
}


Post a reply if you need help with the second one AFTER YOU TRY YOURSELF.
Last edited on
This is wrong:
K = 6.67 * (10^-11 * m^3 * kg^-1 * m^-2)

You should not have included unites in the K constant. Its G actually, not K.

http://www.school-for-champions.com/science/gravitation_force_objects.htm#.VCTiTfldW1c

C++ does not care nor understand unites in this case!
Last edited on
I used K because that's what my teacher had instead of G :/
('k' is often used as the constant of proportionality symbol when talking in a general context).
I was able to get some help from a friend last night. He was able to walk me through it and I have a much better understanding now :)

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
#include <iostream>
#include <math.h>
using namespace std;

int main()
{


	double m1;
	double m2;
	double d;
	double k;
	k = (6.67 * pow(10, -11));

	std::cout.precision(2);

	cout << "Mass of the first person" << endl;
	cin >> m1;

	cout << "Mass of the second person" << endl;
	cin >> m2;

	cout << "Distance between bodies" << endl;
	cin >> d;

	cout << "The Force is: " << endl;

	float Force = k*((m1*m2) / pow(d, 2));
	cout << std::scientific << Force << " * m^3 * kg^-1 * m^-2" << endl;

	return 0;



}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
	int A, B, C, D;

	cout << "(Ax*B)(Cx*D)" << endl;

	cout << "Input the value of A: ";
	cin >> A;
	cout << "Input the value of B: ";
	cin >> B;
	cout << "Input the value of C: ";
	cin >> C;
	cout << "Input the value of D: ";
	cin >> D;

	cout << (A*C) << "x^2 + " << (A*D + B*C) << "x + " << (B*D) << endl;

	return 0;
}

One lots of small things .....

const double k = 6.67e-11; // double value with exponent

Remember, one must always specify a type (double in this case)

Also, the pow function is rather expensive, because it uses a series expansion to do it, so only use it if the exponent is not an integer. So d squared is just d * d

Always initialise your variables with something when you declare them - this can be the biggest source of errors.

Don't have using namespace std; Google why this is so.

Hope all is well :+)
Topic archived. No new replies allowed.