kirchoff law in c++ program

28
FINAL PROJECT BEC10102 2012/201 3 1.0 INTRODUCTION Nowadays C++ is useful and C++ is one of the most popular programming languages and is implemented on a wide variety of hardware and operating system platforms. C++ is an excellent language to start programming in a lot of applications that you use are probably written in c++ and once you learn some basic concepts, learning other languages, like java for example, will be much easier. A computer program is a set of instructions that a programmer writes to tell a computer how to carry out a certain task. The instructions, however, must be in a language that the computer understands. Computers only understand binary language wish that composed of 1 s and 0 s. This is a low level language and very hard to program in. So humans invented higher level languages such as C++ or Pascal to make the job easier. As you will see, these languages are nearly like English but you don t have the freedom to write what you like there are still rules you have to follow. To convert a program in C++ to a binary or executable file that the computer can understand we use a compiler. In this Project of BEC10102, all student electrical engineering should be involved themselves to design and developed using C++ program to completed the task or project are given by lecturer. My groups members able to solve the problem crating with the all knowledge what we learn, with used selection, if selection, if-else selection, and for looping in repetition structure, also function prototype, and function call. 2.0 REQUIREMENT SPECIFICATION Based on the given taks Problem Crafting in Section A, the developed a C++ program able to solve problem of electric circuits by applying Ohm s Law and other related theorems and formula : i. To analyze the resistive circuits that are connected in series and parallel. ii. To determine : Current in a series circuit. Voltage across each parallel branch. Total effect of voltage sources in series. Total effect of current sources in parallel. Total resistance. Power in a circuit. 1 | Page

Upload: ismail-ibrahim

Post on 02-Nov-2014

985 views

Category:

Documents


139 download

DESCRIPTION

programming

TRANSCRIPT

Page 1: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

1.0 INTRODUCTION

Nowadays C++ is useful and C++ is one of the most popular programming languages and is implemented on a wide variety of hardware and operating system platforms. C++ is an excellent language to start programming in a lot of applications that you use are probably written in c++ and once you learn some basic concepts, learning other languages, like java for example, will be much easier.

A computer program is a set of instructions that a programmer writes to tell a computer how to carry out a certain task. The instructions, however, must be in a language that the computer understands. Computers only understand binary language wish that composed of 1’s and 0’s. This is a low level language and very hard to program in. So humans invented higher level languages such as C++ or Pascal to make the job easier. As you will see, these languages are nearly like English but you don’t have the freedom to write what you like there are still rules you have to follow. To convert a program in C++ to a binary or executable file that the computer can understand we use a compiler.

In this Project of BEC10102, all student electrical engineering should be involved themselves to design and developed using C++ program to completed the task or project are given by lecturer. My groups members able to solve the problem crating with the all knowledge what we learn, with used selection, if selection, if-else selection, and for looping in repetition structure, also function prototype, and function call.

2.0 REQUIREMENT SPECIFICATION

Based on the given taks Problem Crafting in Section A, the developed a C++ program able to solve problem of electric circuits by applying Ohm’s Law and other related theorems and formula :

i. To analyze the resistive circuits that are connected in series and parallel.ii. To determine :

Current in a series circuit. Voltage across each parallel branch. Total effect of voltage sources in series. Total effect of current sources in parallel. Total resistance. Power in a circuit. Voltage divider. Current divider.

1 | P a g e

Page 2: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

3.0 ANALYSIS

Table 1 : Data Analysis

ITEM DESCRIPTION

INPUT DATA

a = Option for the circuit type to calculate the total of resistance. The option as below: 1. Series 2. Parallel 3. Enter value of current & voltage.

n = The number of the resistance that been entered.

array[i] = Declaration for every value of resistance been entered.

x = Option for user to choose 1=yes or 0=no to continue with current divider or voltage divider.

V = The value of voltage that been entered.

I = The value of current that been entered.

b = Option for the circuit type to calculate voltage divider or current divider. The option as below: 1. Series 2. Parallel.

R = The total of resistance that been entered (RT).

Ri = The value of resistance that been entered (Rn).

Option = Option for the calculation type. The option as below:1. Voltage2. Current3. Resistance4. Power5. Current Divider and Voltage Divider

PROCESS

Calculation for the Voltage (option=1) V = I * R

Calculation for the Current (option=2) I = V / R

Calculation for the Total of Resistance (option=3) When a = 1 (series), the process is as below : resistance = resistance + array[i]

When a = 2 (parallel), the process is as below : denominator = denominator + 1/ array[i], resistance = 1 / denominator

When a = 2 (parallel), the process is as below :

2 | P a g e

Page 3: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

R = V / I

*x is for the user to continue with current divider (parallel) or voltage divider (series) referring the circuit type option (a), when x = 1 (yes), the process is as below : Vi = (array[i] / resistance) * V

or Ii = (resistance / array[i]) * I

when x = 0 (no), the program will return to main menu.

Calculation for the Power (option=4) P = I * V

Calculation for the Current Divider and Voltage Divider (option=4) When b = 1 (series), the process is as below : Vi = (Ri / R) * V

When b = 2 (parallel), the process is as below : Ii = (R / Ri) * I

Return To Main Menu y = 1

PROGRAM OUTPUT

resistance = The total of resistance.Vi = The voltage drop (voltage divider) through resistance[i]. Ii = The current drop (voltage divider) through resistance[i].R = The value of resistance.V = The value of voltage.I = The value of current.P = The value of power.

3 | P a g e

Page 4: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

4.0 DESIGN

4.1 Main Function

4 | P a g e

Y

Option = 2

Option = 1

Option = 3

Option = 4

Option = 1

Input Option

EnterI and R

Print P

EnterV and R

Enter I and V

Print i

PrintVV = I*R

I = V/R

Rtotal

P = I*V

Y = 1

V – I divider

BEGIN

END

Y

Y

Y

Y

N

N

N

N

N

Page 5: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

Figure 1 : The flowchart of Main Function

4.2 Total Of Resistance Function

aa

bb

5 | P a g e

Rtotal

Return

Enterarray

[i]

Enter v

Print Resistance

Array [i]

Enter x

Entern

Print R

Enter I and V

Enter a

i < = n

x = 1

i < = n

a = 2

a = 1 Resistance = 0.00

i ++ i = 1

i ++Resistance = resistance + array[i]

i = 1

R = V / I

Y

N

N

Y

Y

N

Y

N

Y

N

Page 6: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

Figure 2 : The flowchart of Total of Resistance Function (1)

From aa

From bb

6 | P a g e

Vi = (array[i]/resistance)V

i =1

Denominator=denominator+ (1/array[i])

i = 1

i ++

i ++

Ii = (resistance/array[i])*I

i < = n

x = 1

i < = n

Print Ii

Array[i]

Array [i]

Enter I

Enter x

Print resistance

Resistance = 0.00 Denominator= 0.00

Enter n

Print Vi

Page 7: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

Figure 3 : The flowchart of Total of Resistance Function (2)

4.3 Voltage Divider And Current Divider

Figure 4 : The flowchart of Voltage Divider and Current Divider Function

7 | P a g e

V –Idivider

Return

Print Vi

EnterV, R and Ri Print Ii

EnterI, R and Ri

ENTER b

b = 1

Vi = (Ri/R) * V

Ii = (R/Ri) * I

N

Y

Page 8: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

5.0 THE SOURCE CODE USING C++

8 | P a g e

Page 9: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

9 | P a g e

Page 10: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

10 | P a g e

Page 11: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

11 | P a g e

Page 12: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

12 | P a g e

Page 13: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

13 | P a g e

Page 14: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

6.0 TESTING AND VERIFICATION

1. The final result will come out as below when the program is executed.

Figure 5 : Main Menu

2. When user key in 1 is to calculate the voltage of resistor

Current =40A

Resistor = 8 ohm

Voltage =40x8

=320V

Figure 6 : Voltage Calculation

14 | P a g e

Page 15: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

3. When user key in 2 is calculate the current of resistor

Voltage = 68v

Resistance =4 ohm

Current = 68/4

= 17A

Figure 7 : Current Calculation

4. Next press 3, to calculate the resistance, having two choice which it circuit are connect in parallel or series.

Figure 8 : Resistance Calculation

15 | P a g e

Page 16: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

5. User key in 1 that mean resistor is series connection, shown as below.

R1 = 10 ohmR2 = 10 ohmTotal of resistors = 20 ohms

Whether user want to continue calculate the current divider or not, the question still appear on the screen when resistor finish calculate

Not continue calculate the current divider press 2 will return back the initially page.

16 | P a g e

Page 17: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

Figure 9 : End of Resistance Calculation (series)

.Press 1 is continue voltage divider, shown as belowR1=(10*20v)/20 = 10vR2=(10*20v)/20 = 10v

Figure

10 : Continue to Voltage Divider

6. User key in 2 resistors is connected with parallel shown as below.

R1 = 15 ohmR2 = 15 ohmTotal of resistors = 1/ (1/15 +1/15) = 7.5 ohms

17 | P a g e

Page 18: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

Not continue with current divider.

Figure 11 : End of Resistance Calculation (Parallel)

7. Next press 4 in the program able to calculate the power on the circuit.

Power = cunrrent x voltage

18 | P a g e

Page 19: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

= 20x5

= 100w

Figure 12 : Power Calculation

8. Key in 5 in the program is calculate voltage divider and current divider as below.

19 | P a g e

Page 20: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

Figure 13 : Voltage Divider And Current Divider

Select 1 in series condition on the circuit .

Voltage divider = 50x33 /66 = 25v

Figure 14 : Voltage Divider Calculation

Select 2 in parallel condition on the circuit.

Current divider = (25/4 )x 68 = 212.5A

Figure 15 : Current Divider Calculation

9. If the users do not want continue just key in 0. Then, the program will exit.

20 | P a g e

Page 21: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

Figure 16 : Exit

21 | P a g e

Page 22: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

7.0 DISCUSSION

Every starting the program have define header file before the program written in these program for example using ( iostream) to insert input and output program, and then we will declare what we need to use for the input for example “a” to declare for the input in integer .

After user will select what user want to calculate either circuit in the series or parallel, if the user select “2” this program will execute for parallel circuit calculation.

Then program will go to selection function that been made by using else, else if and if function for series circuit calculation, selection need to use in this program to select the process in the program build in. Selection function help the program to detect what the process will be execute for these case the user choose parallel circuit calculation in the one parallel circuit. The program will show the user to “Enter number of resistors in the circuit:” then the user decide the how many resistor that user need to calculate in these parallel circuit. If the user insert 2 resistor need to be calculate the process execute to calculate the resistor, here the function “for” is use for repetition in the calculation show that if input inserted is 2 the function will repeat until it fulfill requirement, after the requirement have been fulfill it will stop inserting the resistor value.

22 | P a g e

Void declare Rtotal for voiding return

Declare each of the integer and double variables use in this program

If users select 2 the program will calculate for parallel

If users select 1 the program will calculate for series

Header need to define what needed in the program

Page 23: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

When the user select for no 1 it calculate the series resistance in the circuit, the program was shown as below:

If the user want to continue to calculate current divider user need to select 1 to continue to the next process. In the process user will show “Please enter the value of current:” and decide value of current in the circuit.

When the user select for no 3 it calculate the resistance (R) in circuit when the user insert

the value for input V and I.

23 | P a g e

Selection function for parallel cicuit

Process of calculation or formula for parallel circuit

Repetition Function for the resistor value

Formula to calculate the series resistance

The user select no 1 for calculate series

resistance

Input for resistance

Repetition for resistance value

Input V and I

V= voltage I=current

Page 24: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

When the user select no 1 it calculate the voltage divider for series circuit.

24 | P a g e

Repetition Function

These will show the result of current drop through R

Formula to calculate the resistance in

circuit

The selection for user choose

Formula to calculate voltage divider in

circuit

User select 1 to calculate series voltage divider

The program run first

Page 25: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

To exit the program the user will need to insert 0 as the input to exit the program that have been executed, these process using switch and break function. If the function either other input or 0 were inserted It will end the program.

25 | P a g e

Enter 0 to exit the program or to end the program

default: other than input inserted the program will return to zero or program will ended

To terminate the program and create new program to run

The selection to choose whether to calculate selection

The present for calculationTo terminate the

statement

Page 26: kirchoff law in c++ program

FINAL PROJECT BEC10102 2012/2013

These is formula to calculate the resistance in the parallel circuit n=number of the resistor inside of the circuit and 1 is over 1/R1+1/R2…1/Rn, so these program use array to keep or hold the value of resistor.

8.0 CONCLUSION

After all the steps taken, we have managed to create a program which solved our problems mentioned earlier. Thus this program conclusively is able to calculate the total resistance in series, parallel and parallel-series circuit. Plus, including the calculation of power, voltage divider and current divider in the circuit able functional in ours program.

Since there is a rigorous and complicated method in calculating the total resistance in the circuit manually (by hand), thus we have successfully created a program which is able to assist the user to solve the problem more easily.

This program allows the user to manipulate the type of circuit and the input needed in order to attain the correct solution in solving the total resistance in the circuit. In the request of the user, the program would also able to attain the voltage or current drop of each resistance in the circuit.

26 | P a g e