Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Sep 7th, 2005, 3:26 PM   #1
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC