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 :)
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
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.
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.
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).
#include<iostream>
usingnamespace 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.
constdouble 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 usingnamespace std; Google why this is so.