Just started coding and it seems like I must have typed the formula wrongly or something because the code keeps giving me answers that are very random numbers (eg. 2.30126e-06)
#include <iostream>
using namespace std;
int main()
{
double V, I, R;
I=R/V;
V=12;
R=6;
cout << "The current is equals to " << I << "\n";
The point to understand is that variables in programming are not like symbolic variables in mathematics. You can't "set up" the equation and then plug in; the computer reads in the instructions one at a time and performs the calculations on the spot.
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
double V, I, R;
I = R / V;
V = 12;
R = 6;
cout << "The current is equals to " << I << "\n";
return 0;
}
Line 7: please give your variables a better name. I may know what "V", "I" and "R" is ,but not everyone does. Also someone else may know these letters as standing for something different "V" for velocity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
doublevolts{}, current{}, resistance{}; // <--- ALWAYS initialize all your variables.
I = R / V;
V = 12;
R = 6;
cout << "The current is equals to " << current << "\n";
return 0;
}
Line 7: please give your variables better names. I may know what "V", "I" and "R" stand for, but someone else may know them by different names. Call them "volts", current" and "resistance". It makes a lot mere sense to those who do not know. Second as a capital letter I think it is defined as a constant. Lower case letters tend to work better for regular variables.
From C++11 on an empty set of {}s and the compiler will initialize your variables to the proper type of (0)zero needed. Or you can put a value between the {}s. There is no (=) needed just the {}s.
Lines 9 and 11 and 12 are reversed. You are calculating garbage values then giving them a value. Put line 9 after line 12 and see what happens.
What you are doing here works, but in the future you will want to make sure that you are not dividing by (0)zero.
When transcribing mathematical formula into code, there actually is an argument to be made to keep the symbols close to what their representations are in literature. If the person reading the code knows this is some sort of circuit system, they're going to know what VIR is. Just my two cents. (Perhaps this example is too simplistic to make a significant difference one way or the other. Maybe a function about transistor voltages would show this better.) It's a weak argument, to be sure -- both extremes (extremely verbose names, extremely terse name) are probably bad.