![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Ohm's Law
In electronics you can use Ohm's Law to find the Voltage, Resistance and Current of an electrical circuit.
Here is a small simple C++ program that uses Ohm's Law, it could prove useful to someone, probably not but i decided to post it here, because i never really post any of my code ![]() /*
File: ohm.cpp
Title: Ohm's law
Author: ColdDeath
Date: 07/09/05 19:47
Description: This program demonstrates Ohm's law. (I=V/R)
V = voltage in Volts, R = resistance in Ohms, I = current in ampheres
*/
#include <iostream> //needed for user input and output
using namespace std;
int VoltFind(float current, float resistance)
{
//V=I*R
float voltage = current * resistance;
cout<<"\nVoltage: "<<voltage<<endl;
return 0;
}
int ResistFind(float voltage, float current)
{
//R=V/I
float resistance = voltage / current;
cout<<"\nResistance: "<<resistance<<endl;
return 0;
}
int CurrentFind(float voltage, float resistance)
{
//I=V/R
float current = voltage / resistance;
cout<<"\nCurrent: "<<current<<endl;
return 0;
}
int main(int argc, char *argv[])
{
float I;
float R;
float V;
int menuOption;
cout<<"Main Menu\n1. Find Voltage\n2. Find Resistance\n3. Find Current\n4. Exit"<<endl;
cin>>menuOption;
switch (menuOption)
{
case 1:
cout<<"Current: ";
cin>>I;
cout<<"\nResistance: ";
cin>>R;
VoltFind(I, R);
break;
case 2:
cout<<"\nVoltage: ";
cin>>V;
cout<<"Current: ";
cin>>I;
ResistFind(V, I);
break;
case 3:
cout<<"Voltage: ";
cin>>V;
cout<<"\nResistance: ";
cin>>R;
CurrentFind(V, R);
break;
case 4:
return 0;
default:
cout<<"Please use a correct menu option.";
return 0;
}
return 0; //Exit the main function
}If anyone has any suggestions or comments about this program, feel free to speak. -CD EDIT: This code is straightforward and simple so i decided not to comment it too much
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0
![]() |
It would be nice if the code dealt with circuits. I mean, can you make it so that it can find currents and voltage across circuits?
|
|
|
|
|
|
#3 |
|
Programmer
Join Date: May 2005
Posts: 41
Rep Power: 0
![]() |
Hey, if you can write a programme to figure out Integrals (multi variable) and Derivitives that would make calc 3 a lot easier (oh yes, vectors too)
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
like maple or mathcad or whatever?
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
or mathlab and mathematica
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Thevenin's equivalent, perhaps, or Norton's? I already have a 50-cent calculator.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|