You are on page 1of 64

EEU 101/2 Computer Programming

Group Project
Title: Total Resistance Calculation
Group 4
Group members:
ANG KOK WEI 108467
GOH CHIA YEE 108474
KOAY BOON AIK 108146
MOHAMAD SHAZELEE BIN SIMON 108153
NURUL WAHIEDA BINTI MUHAMAD BUSTAMAN 108488
TIONG KWONG YII 108170

School of Mechanical Engineering


Date of submission: 31 March 2011

Lecturer: Associate Prof. Dr. Umi Kalthum Ngah

Acknowledgement
2

Here, we would like to acknowledge a few groups of supportive people, who became the

foundation for our achievement in concluding this Group Project for the course EEU 101/2

Computer Programming.

At the outset, there would be no greater accomplishment without the guide and precious

knowledge from our valued lecturer. Consequently, no one else is meant for this part of our

acknowledgement but the person in mind, Associate Prof. Dr. Umi Kalthum Ngah.

Furthermore, we would like to announce our gratitude towards our mentor in the course

EEU 101/2 for all the guidance given in learning the C++ programming language.

To finish with, we would like to express our truthful appreciation towards those around us,

especially those from the other groups, who had been there to assist us with regard to their

priceless time and effort.

Table of Contents
Contents Page
1 Acknowledgement 2
3

2 Table of Contents 3

3 Abstract 4

4 Introduction 5

5 Minutes of Meetings 6-9

6 Problem Analysis 10
5. Problem Statement
1 Input and Output Analysis
5.
2
7 Design Decomposition & Methodology 11-13

8 Coding and Debugging 14-26

9 Integration 27-45

10 Testing and Validation 46-59

11 Gantt Chart 60

12 Conclusion 61

13 Bibliography 62

Abstract
4

Our program is to enable the user to easily calculate the resistance of a resistor using a colour
coded band and the total resistance in a circuit of different type. In a series circuit, the current
through each of the components is the same. To find the total resistance, add the resistances Ri
of each component and take the the sum.

In a parallel circuit, the voltage across each of the components is the same. To find the total
resistance, add the reciprocals of the resistances Ri of each component and take the reciprocal
of the sum.

For series-parallel circuits, the total resistance is computed using series and parallel formula,
where most circuit can be broken down to a simple parallel or series circuit.

Most resistors have three coloured bands close together at one end. The colour codes are as
follows:

Black 0
Brown 1 The integer codes of the bands are in the order of colour1,
Red 2 colour2 and colour3. The resistance in ohms is calculated
Orange 3 using this formula:
Yellow 4
Green 5 Resistance = (10 x colour1 + colour2) x 10colour 3
Blue 6
Violet 7
Gray 8
White 9

Introduction
5

Project Title: Total Resistance Calculation.

Project Objectives:

1) Calculate the resistance of resistor in a series, parallel or parallel-series circuit.

2) Calculate the resistance of a resistor based on the colour coded band on a resistor.

Project Scope:

1) This program can calculate resistances of resistors.

2) The circuit taken into consideration include series, parallel and parallel-series circuit.

3) This programme can calculate resistances of resistors based on the colour bands of the
resistors.

4) The circuit taken into consideration must only consist of a power source, and resistors
either in the connection of series or parallel or the combination of both.

5) The equivalent resistance is calculated without considering the effective range of


resistance but only the average resistance value of the resistors in the circuit.

In this project, we tried to utilize as many commands as we are capable of to demonstrate our
familiarity with these commands besides showing variation. Among the commands used are:
for, if, if-else, switch, do-while, looping, function, pointer and arrays.

Minutes of 1st Meeting


Summary
6

Meeting Analysis of the program and design decomposition of the Program’s


Topic problem
Date 26th January 2011 From 08:30 PM To 10:0
0 PM
Location Café Lembaran Note Nurul Wahieda Duration 1.5
Taker Muhamad hr
Bustaman 108488

Topic
1. Discussion on problem statement, input and output analysis.
2. Discussion on the flow chart for the decomposition of the problem

Attendee List
# Name Present Absence
Reason
1 NURUL WAHIEDA BT MUHAMAD BUSTAMAN 108488 Y -
2 ANG KOK WEI 108467 Y -
3 TIONG KWONG YII 108170 Y -
4 MOHAMAD SHAZELEE BIN SIMON 108153 Y -
5 KOAY BOON AIK 108146 Y -
6 GOH CHIA YEE 108474 Y -

Meeting Notes
1 The problem statement, input and output analysis are done.
2 The design composition of the whole program is done.

Action Items For Next Meeting


# Task Person
Responsible
1 Come up with ideas for the C++ codes of each part of the program. All

Minutes of 2nd Meeting


Summary
Meeting
Topic
Coding and debugging.
Date From 08:30 PM To 10:0
28th February 2011
0 PM
Location Café Lembaran Note Nurul Wahieda Duration 1.5
Taker Muhamad hr
Bustaman 108488
7

Topic
1. Writing codes for each part of the program.

Attendee List
# Name Present Absence
Reason
1 NURUL WAHIEDA BT MUHAMAD BUSTAMAN 108488 Y -
2 ANG KOK WEI 108467 Y -
3 TIONG KWONG YII 108170 Y -
4 MOHAMAD SHAZELEE BIN SIMON 108153 Y -
5 KOAY BOON AIK 108146 Y -
6 GOH CHIA YEE 108474 Y -

Meeting Notes
1 Each member are assigned to write the parts of the program.

Action Items For Next Meeting


# Task Person
Responsible
1 Write code for each part of the program which is to be integrated in the All
next meeting.

Minutes of 3rd Meeting


Summary
Meeting Testing, validation and integration
Topic
Date 16th March 2011 From 08:30 PM To 10:0
0 PM
Location Café Lembaran Note Nurul Wahieda Duration 1.5
Taker Muhamad hr
Bustaman 108488

Topic
1. Each code for every part of the program is debugged to check error. Each one is then
compiled and run.
2. The integrated code is debugged, compiled and run.

Attendee List
# Name Present Absence
Reason
8

1 NURUL WAHIEDA BT MUHAMAD BUSTAMAN 108488 Y -


2 ANG KOK WEI 108467 Y -
3 TIONG KWONG YII 108170 Y -
4 MOHAMAD SHAZELEE BIN SIMON 108153 Y -
5 KOAY BOON AIK 108146 Y -
6 GOH CHIA YEE 108474 Y -

Meeting Notes
1 The errors which occurred in compiling the integrated codes while debugging have
been solved.

Action Items For Next Meeting


# Task Person
Responsible
1 Complete parts of the project’s paperwork which has been assigned to All
each team members.

Minutes of 4th Meeting


Summary
Meeting Presentation preparation
Topic
Date 28th March 2011 From 08:30 PM To 10:0
0 PM
Location Café Lembaran Note Nurul Wahieda Duration 1.5
Taker Muhamad hr
Bustaman 108488

Topic
1. Rechecking program’s code.
2. Preparing for the presentation of the program.

Attendee List
# Name Present Absence
Reason
1 NURUL WAHIEDA BT MUHAMAD BUSTAMAN 108488 Y -
2 ANG KOK WEI 108467 Y -
3 TIONG KWONG YII 108170 Y -
4 MOHAMAD SHAZELEE BIN SIMON 108153 Y -
5 KOAY BOON AIK 108146 Y -
6 GOH CHIA YEE 108474 Y -

Meeting Notes
9

1 The codes, paperwork and the forecasted flow of our presentation are done.

Action Items
# Task Person
Responsible
1 Prepare for the presentation of the program. All

Problem Analysis

Problem Statement

Amongst the common type of combination of the electric circuit encompasses

parallel, series, and a combination of both. Due to the rigorous and complicated

method in calculating the total resistance in the circuit manually (by hand), thus we

intend to create a program which assist the user to solve the problem more easily.

This program will then allow 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 be able to attain the

resistance of a resistor using the colour coded band calculation.

Input and Output Analysis

Input data:
10

 Type of resistance calculation (parallel, series, series-parallel and

colour coded band), number of resistors in the circuit, the value (in ohm

unit) of each of the resistors, the code for the colour coded band.

Output data:

 Total resistance in the circuit or of the resistor.

Design Decomposition
Total Resistance
Calculation

Parallel Parallel- The


circuit Series circuit Disclaimer Programmers

Series Coloured Bands


circuit of Resistor

Number of Number of lines Codes of


resistor between node x each
and node y colour

Number of lines First


having more than colour’s
one resistor code
Value of each
resistor (ohm)
11

Second
Number of colour’s
resistor in each code
line

Total
Third
resistanc Value of resistors
colour’s
e (ohm) (ohm) of each code
line
12

Start

Display the main display menu.

Output six choices for the user to choose from. Ask the user for what type of calculation required.

Input: circuit

Start for loop for main display menu

circuit == 1
and circuit ==2 circuit==3

Display "Enter Total Number Display colour-code table and


of Parallel Resistor:" “Enter three resistor code”.

Input: number input


get a, b,
c
Display "Enter Value of
Parallel Resistor #" d=((10*a + b))*(pow(10.0,c))

inp Displ
ut= ay
R ‘d’

(a) (b) (c) (d)


13

R_equiv = (R_equiv * R ) Get no_of_line, no_of_line_morethan1r,


/ ( R_equiv + R )

Display ‘R_equiv’

input =’a’

“sum[a] = sum[a] +
series_line_resistance”
“sum_1r = sum_1r +
1/parallel_line_resistance”
“sumslfinal=1/sumsl”
“finalsum1=1/sumslfinal+1/
totalsum”
“finalsum2=1/finalsum1”

Display " finalsum2”

Display disclamer quote


Display names of programmers
14

Coding and Debugging

This is known as a comment. The comment is placed first as to specify what the code is for. In

this case, the comment shown refers to the codes of our total resistance calculation program.

The list above is for the headers used in our program. The following table will elaborate on its

term of usage.

Header Definition
<iostream> Declares objects that control reading from and writing to the standard

streams. This is needed to perform input and output in our program.

<math.h> A header for basic mathematical operations since our program requires

mathematical calculations to be done.

<iomanip> Provides facilities to manipulate output formatting, such as the base used

when formatting integers and the precision of floating point values. An

example of its usage is in the calculation of the resistance for the colour

coded band resistor in our program.

<cstdlib> The header of the general purpose standard library of C programming

language which includes functions involving memory allocation, process

control, conversions and others.


15

<windows.h> This header defines a very large number of Windows specific functions that

can be used in C ++ programming language. Specifically, in our program, it

is intended for the colour output of texts shown on the command prompt.

Basically, this tells the compiler to use the std namespace.


16

Then we declare all the functions which are used in our program, including the coloured text

output.

Next is the code for the int main( ), where this is the vital and central part of our program. This

main user interface function, will allow the user to navigate through to the other parts of the

program. Which is why we have chosen to use the switch operator in the int main( ) function

as to make an easy-to-use main display menu for the user.


17
18
19

Further elaboration on each part of the int main( ) code has been embedded in the code itself,

where they are stated after each ‘\\’ symbol. This is done to make it easily understandable for

programmers other than our group members.

After the definition for the int main( ) function, we then define the other function starting off

with the centerwelcometext( ) function. This function uses pointer operator for the text output

on the main display menu, where they are aligned to the center of the screen.
20

Next is the definition for the parallel circuit function. This parallel( ) function will be called

when the user keys in the number ‘1’ from the main display menu. Since this will initiate the

case ‘1’ for the switch operator in the int main( ) function earlier. The elaboration are in the

code, stated after the ‘//’ symbol.

Afterward is the definition for the series circuit function. This series( ) function will be called

when the user key in the number ‘2’ from the main display menu. Since this will initiate the

case ‘2’ for the switch operator in the int main( ) function earlier. The elaboration are in the

code, stated after the ‘//’ symbol.


21

Further on, the ps( ) function will be called when the user type in the number ‘3’ in the main

display menu. This will then initiate the case ‘3’ for the switch operator in the int main( )

function earlier. The elaboration are in the code, stated after the ‘//’ symbol.

The ps( ) function is to calculate the total resistance in the parallel-series circuit.

Though in ps() function itself includes two more function. The first one, DisplayMenu (), is to

ask the user either to continue or abort the program before going on further. The second one ,

Functionc(), is then the part asking the user for inputs regarding the information on the

parallel-series circuit. The gathered input is then calculated and presented as the final output,

which is the total resistance.


22
23
24

Then, for case ‘4’ in the int main( ) function switch operator, the coded( )function is called.
25

This is to inform the user of the code for each colour band on a resistor.
26
27

The program will then ask the user for inputs. There is then another two switch operators in

order to find b and c, which have the same format as the switch stated before. Then when the

program have attained the value for ‘a’, ‘b’ and ‘c’, it will then compute the answer for‘d’,

which is the resistance of the resistor.

Furthermore, the next function, group ( ) will be called through case ‘5’ from the switch

operator in the int main ( ) function. This function is to include each of our team member

name and matrics number.

Lastly, the dis ( ) function simply contains a disclaimer’s quote if there happens to be a misuse

of or a contradiction with the answer given by the program.

Integration
// GROUP 4 C++ PROJECT Total Resistance Calculation.cpp : main project file.

#include <iostream>

#include <math.h>

#include <iomanip>

#include <cstdlib>

#include <windows.h>//for colouring background and text

using namespace std;

void centerwelcomeText(char* s); //declare functions

void parallel();

void series();

void coded();
28

void DisplayMenu();

float FunctionC();

float ps ();

void group();

void dis();

//Colouring the text and background later

enum colour { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK,

DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };

struct setcolour

{ colour _c;

HANDLE _console_handle;

setcolour(colour c, HANDLE console_handle)

: _c(c), _console_handle(0)

{ _console_handle = console_handle; }};

basic_ostream<char> &operator<<(basic_ostream<char> &s, const setcolour &ref)

{SetConsoleTextAttribute(ref._console_handle, ref._c);

return s; }

//function's definition for the main output on the screen: stores all other function to be called when prompt by the

user

int main()

{//defining variables needed in this function

char circuit;

int number = 0;

int i = 0;

double R_equiv =0.0;


29

double R = 0.0;

//defining to colour the text

HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

//the operator to let the user repeat placing in inputs as required

for(;;) {//do-while loop for the main output to project on the screen

do {cout << "\n";

centerwelcomeText("WELCOME USER!!");//calls the funtion centerwelcomeText(char* s)

centerwelcomeText("GROUP 1 C++ PROJECT 2011!!");

cout << "\n";

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);//simple banner : coloured

cout <<setcolour(YELLOW, chandle) <<

setw(25)<<"**"<<"******************************"<< setw(2)<< "**"<<endl;

cout <<setcolour(YELLOW, chandle) << setw(25)<<"**"<<setcolour(RED, chandle) <<

" LET'S CALCULATE RESISTANCE "<< setw(2)<<setcolour(YELLOW, chandle) << "**"<<endl;

cout <<setcolour(YELLOW, chandle) << setw(25)<<"**"<<"******************************"<<

setw(2)<< "**""\n\n\n\n";

//Switch operator : enables a the user to select among several alternatives

cout << setcolour(RED, chandle) << " Choose an option" << setcolour(WHITE, chandle) << " (or q to

exit!!)\n\n";

cout << setcolour(TEAL, chandle) << "1. Parallel Circuit\n\n";

cout << setcolour(GREEN, chandle) << "2. Series Circuit\n\n";

cout << setcolour(PINK, chandle) << "3. Parallel-Series Circuit\n\n";

cout << setcolour(BLUE, chandle) << "4. Coloured Bands Of Resistor\n\n";

cout << setcolour(YELLOW, chandle) << "5. The Programmers\n\n";

cout << setcolour(DARKRED, chandle) << "6. Disclaimer\n\n";

cout << setcolour(RED, chandle) << "-->";


30

cin >> circuit; // the value keyed in will determine the case sequence executed in the switch operator below

} while(circuit < '1' || circuit > '6' && circuit != 'q') ;// the do-while loop will repeat its execution as long as

the user key in numbers between 1 till 6

//the user is given the choice to exit the program by keying in 'q'

if(circuit == 'q') break;

cout << "\n\n";

switch (circuit) {// the value of 'circuit' by user determines the case sequence executed

case '1':

parallel();//calls the parallel() funtion for execution

cout << "\n";

cout << "\n";

system("pause");//enable the user to ponder on the answer given by the program before the screen is cleared of

previous output

system("CLS");//clear the previous data on the screen

system( "color 0c" );//return the normal background colour

break;

case '2':

series() ;//calls the parallel() funtion for execution

cout << "\n";

cout << "\n";

system("pause");//enable the user to ponder on the answer given by the program before the screen is cleared of

previous output

system("CLS");//clear the previous data on the screen

system( "color 0c" );//return the normal background colour

break;

case '3':

ps ();//calls the parallel-series funtion, ps() for execution

cout << "\n";


31

cout << "\n";

getchar();//enable the user to ponder on the answer given by the program before the screen is cleared of previous

output

system("CLS");//clear the previous data on the screen

system( "color 0c" );//return the normal background colour

break;

case '4':

coded();//calls the parallel() funtion for execution

cout << "\n";

cout << "\n";

system("pause");//enable the user to ponder on the answer given by the program before the screen is cleared of

previous output

system("CLS");//clear the previous data on the screen

system( "color 0c" );//return the normal background colour

break;

case '5':

group();//calls the parallel() funtion for execution

cout << "\n";

cout << "\n";

system("pause");//enable the user to ponder on the answer given by the program before the screen is cleared of

previous output

system("CLS");//clear the previous data on the screen

system( "color 0c" );//return the normal background colour

break;

case '6':

dis();//calls the parallel() funtion for execution

cout << "\n";

cout << "\n";


32

system("pause");//enable the user to ponder on the answer given by the program before the screen is cleared of

previous output

system("CLS");//clear the previous data on the screen

system( "color 0c" );//return the normal background colour

break; }}

return 0; }

//Function's definition to align center screen of welcoming text

void centerwelcomeText(char* s) {

//defining variables needed in this function

int l=strlen(s);

int pos=(int)((80-l)/2);

for(int i=0;i<pos;i++)

cout<<" ";

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE); // colouring the welcome text blue

cout<< setcolour(BLUE, chandle) << s <<endl; }

//The function's definition to calculate the total parallel resistance in the circuit

void parallel(){//defining variables needed in this function

int number = 0;

double R_equiv =0.0;

double R = 0.0;

//defining to colour the text

HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);

cout <<setcolour(YELLOW, chandle) << setfill('=') <<setw(80)<<"="<<endl;//lines for border and

simple decoration
33

cout << setfill(' ') <<setw(50)<<" "<<endl;

cout << setcolour(TEAL, chandle) << "Enter Total Number of Parallel Resistor: ";//Tell the user to put

in total number of resistor in circuit

while (!(cin >> number)){

cin.clear();

cin.ignore(1000,'\n');

cout<<"\nPlease enter the correct value: ";

} //the input by user is used to initialise the 'for' operator afterward

//Using 'for' operator to calculate as many input according to the user's wish without any restriction or

hassle

for (int count =1; count <=number; ++count) {

cout << "Enter Value of Parallel Resistor #" << count << ":";

while (!(cin >> R)){

cin.clear();

cin.ignore(1000,'\n');

cout<<"\nPlease enter the correct value of resistance: ";}

if (count ==1)

R_equiv =R;

else

R_equiv = (R_equiv * R ) / ( R_equiv + R ) ; //formula to calculate total resistance in

parallel circuit}

cout.setf(ios::fixed);//a format option that displays floating point numbers in normal notation - no

trailing zeroes and no scientific notation.

cout.precision(4);//a manipulator that sets the total number of digits to be displayed when floating point

numbers are output.

cout << "\n";

SetConsoleTextAttribute(hOut,
34

BACKGROUND_BLUE|

BACKGROUND_INTENSITY);//highlighting the background of the answer to blue with

intensity

cout << " The Equivalent Resistance is : " << R_equiv << " Ohms\n\n\n"<< flush;//generates the

equivalent resistance on the screen}

//The function's definition to calculate the total series resistance in the circuit

void series(){//defining variables needed in this function

int number = 0;

double R_equiv=0.0;

double R = 0.0;

//defining to colour the text

HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);

cout <<setcolour(YELLOW, chandle) << setfill('=') <<setw(80)<<"="<<endl;//lines for border and

simple decoration

cout << setfill(' ') <<setw(50)<<" "<<endl;

cout << setcolour(GREEN, chandle) << "Enter Total Number of Series: ";//Tell the user to put in total

number of resistor in circuit

while (!(cin >> number)){

cin.clear();

cin.ignore(1000,'\n');

cout<<"\nPlease enter the correct value: ";

}//the input by user is used to initialise the 'for' operator afterward

//Using 'for' operator to calculate as many input according to the user's wish without any restriction or hassle

for (int count =1; count <=number; ++count) {

cout << "Enter value of series resistor #" << count << ":";

while (!(cin >> R)){


35

cin.clear();

cin.ignore(1000,'\n');

cout<<"Please enter the correct value of resistance: ";}

if (count ==1)

R_equiv =R;

else

R_equiv = R_equiv + R;//formula to calculate total resistance in series circuit}

cout.setf(ios::fixed);//a format option that displays floating point numbers in normal notation - no

trailing zeroes and no scientific notation.

cout.precision(4);//a manipulator that sets the total number of digits to be displayed when floating point

numbers are output.

cout << "\n";

SetConsoleTextAttribute(hOut,

BACKGROUND_GREEN|

BACKGROUND_INTENSITY);//highlighting the background of the answer to green with

intensity

cout << " The Equivalent Resistance is : " << R_equiv << " Ohms\n\n\n" << endl;//generates the

equivalent resistance on the screen}

//The function's definition to calculate the resistance of the parallel-series circuit

float ps (){

char choice;

char command;

HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);

cout <<setcolour(PINK, chandle) <<" "<<endl;

DisplayMenu () ;
36

while (command != 'c')

{ cin>>choice;//function call

if ((choice=='C')||(choice=='c'))

FunctionC(); //function call

else

return 0; }}

void DisplayMenu () //function of DisplayMenu

{cout<<" Enter c to continue or any key to quit"<<endl;//ask the user if want to continue}

float FunctionC () //function of FunctionC

{int a,b,c;

int no_of_line;

int no_of_line_morethan1r;

int no_of_resistor_insl;

float series_line_resistance;

int no_of_line_contain1r;

float parallel_line_resistance;

float sum[no_of_line_morethan1r] , sumsl , totalsum , sum_1r ,sumslfinal ,finalsum1 , finalsum2;

cout<<"\n";

cout<<"3. Circuit C : Combination of Series and Parallel:\n";

cout<<" Node X \n";

cout<<"

.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@****************

@@@@@@@@. \n";

cout<<" | | | | | \n";

cout<<" | --- --- --- --- \n";

cout<<" | |R| |R| |R| | R |\n";

cout<<" | | 11| | 21| | 31| | N1|\n";

cout<<" | --- --- --- --- \n";


37

cout<<" | | | | | \n";

cout<<" | --- --- --- --- \n";

cout<<" | |R| |R| |R| | R |\n";

cout<<" V | 12| | 22| | 32| | N2|\n";

cout<<" | --- --- --- --- \n";

cout<<" | | | | | \n";

cout<<" | * * * | \n";

cout<<" | * * * | \n";

cout<<" | --- --- --- --- \n";

cout<<" | |R| |R| |R| | R |\n";

cout<<" | | 1n| | 2n| | 3n| | Nn|\n";

cout<<" | --- --- --- --- \n";

cout<<" | | | | | \n";

cout<<"

.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@****************

@@@@@@@@. \n";

cout<<" Node Y \n";

cout<<"===========================================================================

====\n";

cout<<"Explaination:\n";

cout<<"\n";

cout<<"Rab: a=number of line .\n";

cout<<" b=number of resistor.\n";

cout<<"Example: R12 means 2nd resistor in line 1.\n";

cout<<"-------------------------------------------------------------------------------\n";

cout<<"\n";

cout<<"How many lines are connected between Node X and Node Y ?\n";

while (!(cin>>no_of_line)){
38

cin.clear();

cin.ignore(1000,'\n');

cout<<"Please enter the correct value for the number of lines : "; }

cout<<"How many lines which has more than 1 resistor are connected to it ?\n";

while(!(cin>>no_of_line_morethan1r)){

cin.clear();

cin.ignore(1000,'\n');

cout<<"Please enter the correct value for the number of lines : "; }

if (no_of_line_morethan1r!=0)

{cout<<"\n";

cout<<"Lets call the line which contain more than 1 resistor as 'series_line'.\n";

for(a=0;a<no_of_line_morethan1r;a++)

{cout<<"\n";

cout<<"Please enter the number of resistor in series_line "<<(a+1)<<" :";

while(!(cin>>no_of_resistor_insl)){

cin.clear();

cin.ignore(1000,'\n');

cout<<"Please enter the correct value for the number of resistor : ";}

//get value from user

cout<<"Please enter the value of resistance of resistor in series_line "<<(a+1)<<": (in ohms)\n";

sum[a]=0;

for(b=0;b<no_of_resistor_insl;b++)

{cout<<"R "<<(a+1)<<(b+1)<<" :";

while(!(cin>>series_line_resistance)){

cin.clear();

cin.ignore(1000,'\n');

cout<<"Please enter the correct value of resistance : "; }

if(series_line_resistance<0)
39

{cout<<"Error. Negative resistance does not exist.\n";

cout<<"Please correct value of R "<<(a+1)<<(b+1)<<" :"; //ask user to reenter the value of resistance

cin>> series_line_resistance; }

sum[a] = sum[a] + series_line_resistance ; } //calculation

cout<<"The equivalent resistance of series_line "<<(a+1)<<" is "<<sum[a]<<" ohms."<<endl; } }

else {cout<<"Invalid data! Please press reenter the number of line which contain more than 1 resistor.\n";

system("CLS"); }

no_of_line_contain1r=no_of_line-no_of_line_morethan1r;

cout<<"\n";

cout<<"--------------------------------------------------------------------------------\n";

cout<<"Your circuit has "<<no_of_line_contain1r<<" line which contain only one resistor.\n";

if(no_of_line_contain1r!=0)

{cout<<"Please enter the value of each resistor. (in ohms)\n"; //get value from user

sum_1r=0;

for(c=0;c<no_of_line_contain1r;c++)

{cout<<"\n";

cout<<"Resistor "<<(c+1)<<" :";

while(!(cin>>parallel_line_resistance)){

cin.clear();

cin.ignore(1000,'\n');

cout<<"Please enter the correct value of resistance : "; }

if(parallel_line_resistance<0)

{cout<<"Error. Negative resistance does not exist.\n";

cout<<"Please correct the value of Resistor "<<(c+1)<<" :";//ask user to reenter the value of resistance

cin>>parallel_line_resistance; }

sum_1r = sum_1r + 1/parallel_line_resistance; }

totalsum = 1/sum_1r; }

sumsl=0; //calculation
40

for(a=0;a<no_of_line_morethan1r;a++)

{sumsl=sumsl+1/sum[a]; }

sumslfinal=1/sumsl;

finalsum1=1/sumslfinal+1/totalsum;

finalsum2=1/finalsum1;

cout<<"\n";

cout<<"--------------------------------------------------------------------------------\n";

cout<<"Thus, the equivalent resistance on the circuit is "<<finalsum2<<" ohms."<<endl;

cout<<"Press any key to quit"<<endl; }

//The function's definition to calculate the resistance according to a resistors' coloured band

void coded()

{char cd1,cd2,cd3;

double a,b,c,d;

HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);

cout <<endl;

cout <<setcolour(PINK, chandle)<< setw(30)<<"| COLOUR"<<setw(19)<<" "<<setcolour(TEAL,

chandle)<<setw(5)<<"CODE|"<<endl;

cout <<setcolour(DARKTEAL, chandle)<< setw(30)<<"| ------"<<"---------------------"<< setw(2)<<

"--|"<<endl;

cout <<setcolour(DARKGRAY, chandle)<<setw(30)<<"|

Black"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> B |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(DARKRED, chandle) << setw(30)<<"|

Brown"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> N |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(RED, chandle) << setw(30)<<"| Red"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<">

R |"<<setfill(' ')<<setw(20)<<" "<<endl;


41

cout <<setcolour(DARKYELLOW, chandle) << setw(30)<<"|

Orange"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> O |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(YELLOW, chandle) << setw(30)<<"|

Yellow"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> Y |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(GREEN, chandle) << setw(30)<<"|

Green"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> G |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(BLUE, chandle) << setw(30)<<"| Blue"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<">

E |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(DARKPINK, chandle) << setw(30)<<"|

Violet"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> V |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(WHITE, chandle) << setw(30)<<"|

White"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> W |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(GRAY, chandle) << setw(30)<<"|

Gray"<<setfill('-')<<setw(14)<<"-"<<setw(10)<<"> A |"<<setfill(' ')<<setw(20)<<" "<<endl;

cout <<setcolour(DARKTEAL, chandle)<< setw(30)<<"| ------"<<"---------------------"<<

setw(2)<< "--|"<<endl;

cout <<setcolour(TEAL, chandle) <<setfill(' ')<<setw(20)<<" "<<endl;

cout << "\nEnter the"<<setcolour(DARKGREEN, chandle)<< " three "<<setcolour(TEAL, chandle)<<

"resistor code>>";

cout <<endl;

cout <<setfill(' ')<<setw(20)<<" "<<endl;

cout << " #1 >> ";

cout << "\n";

//look for cd1

switch(cd1){

case'B':case'b': a=0;

break;

case'N': case'n': a=1;


42

break;

case'R': case'r': a=2;

break;

case'O': case'o':a=3;

break;

case'Y': case'y':a=4;

break;

case'G': case'g':a=5;

break;

case'E': case'e': a=6;

break;

case'V': case'v':a=7;

break;

case'A': case'a':a=8;

break;

case'W':case'w': a=9;

break; }

cout << " #2 >> ";

cin>>cd2;

cout << "\n";

//look for cd2

switch(cd2){ case'B':case'b': b=0;

break;

case'N': case'n':b=1;

break;

case'R':case'r': b=2;

break;

case'O': case'o':b=3;
43

break;

case'Y':case'y': b=4;

break;

case'G':case'g': b=5;

break;

case'E':case'e': b=6;

break;

case'V': case'v':b=7;

break;

case'A': case'a': b=8;

break;

case'W': case'w':b=9;

break; }

cout << " #3 >> ";

cin>>cd3;

cout << "\n";

//look for cd3

switch(cd3){

case'B':case'b': c=0;

break;

case'N': case'n':c=1;

break;

case'R':case'r': c=2;

break;

case'O': case'o':c=3;

break;

case'Y':case'y': c=4;

break;
44

case'G':case'g': c=5;

break;

case'E': case'e':c=6;

break;

case'V': case'v':c=7;

break;

case'A': case'a':c=8;

break;

case'W': case'w':c=9;

break; }

d = ((10*a + b))*(pow(10.0,c));

cout << "The resistance is "<<d;

cout << "\n";}

void group()

{HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);

cout <<setcolour(GREEN, chandle) << setfill('=') <<setw(80)<<"="<<endl;//lines for border and simple

decoration

cout << setfill(' ') <<setw(50)<<" "<<endl;

cout <<setw(10)<<" "<<setcolour(YELLOW, chandle)<< setw(40) << "The Programmers\n";

cout <<setw(9)<<" "<<setcolour(YELLOW, chandle)<< setw(40) << "---------------";

cout << setfill(' ') <<setw(50)<<" "<<endl;

cout <<setw(10)<<" "<<setcolour(RED, chandle)<< setw(30)<<"ANG KOK WEI"<<setw(5)<<"

"<<setcolour(DARKTEAL, chandle)<<setw(6)<<"108467"<<endl;

cout <<setw(10)<<" "<<setcolour(RED, chandle)<< setw(30)<<"MOHAMAD SHAZELEE BIN

SIMON"<<setw(5)<<" "<<setcolour(DARKTEAL, chandle)<<setw(6)<<"108153"<<endl;


45

cout <<setw(10)<<" "<<setcolour(RED, chandle)<< setw(30)<<"KOAY BOON AIK"<<setw(5)<<"

"<<setcolour(DARKTEAL, chandle)<<setw(6)<<"108146"<<endl;

cout <<setw(10)<<" "<<setcolour(RED, chandle)<< setw(30)<<"GOH CHIA YEE"<<setw(5)<<"

"<<setcolour(DARKTEAL, chandle)<<setw(6)<<"108474"<<endl;

cout <<setw(10)<<" "<<setcolour(RED, chandle)<< setw(30)<<"NURUL WAHIEDA MUHAMAD

BUSTAMAN"<<setw(5)<<" "<<setcolour(DARKTEAL, chandle)<<setw(6)<<"108488"<<endl;

cout <<setw(10)<<" "<<setcolour(RED, chandle)<< setw(30)<<"TIONG KWONG YII"<<setw(5)<<"

"<<setcolour(DARKTEAL, chandle)<<setw(6)<<"108170"<<endl; }

void dis()

{HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);

cout <<setcolour(GREEN, chandle) << setfill('=') <<setw(80)<<"="<<endl;//lines for border and simple

decoration

cout << setfill(' ') <<setw(50)<<" "<<endl;

cout <<setcolour(DARKRED, chandle)<< setw(100)<<" Our group accepts no liability for the content

of this program,\n or for the consequences of any actions taken on the basis of the \n information provided, unless

that information is subsequently confirmed in \n writing. If you are not the intended recipient you are notified that

\n disclosing, copying, distributing or taking any action in reliance on the \n contents of this information is strictly

prohibited without \n the agreement from the programmers themself"<<endl; }


46

Testing and Validation

 The Parallel Circuit

When the user keys in 1


47

The user will then be asked to key in the number of parallel resistor

As an example, the total number of parallel resistor is 10.

Press enter,
48

The program will asked the user to key in all the value of parallel resistor

For example,

R1=2, R2=1.5,

R3=1, R4=3,

R5=1.4, R6=2,

R7=2.2, R8=3,

R9=1, R10=1.1
49

Hence, the effective resistance in 10 parallel resistors is 0.1721 ohms.


50

If the user want to continue just press any key and it will return to the original pages.

If the users do not want continue just key in q. then the program will exit.
51

 The Series Circuit

1. First, compile the program in order to convert the language into the machine code.
Click Program > Compile.
2. If there is no error, then proceed to running the program.
3. After compiling the program, the status is “SUCCESS” without any error. This,
however, does not mean that the program is correct because the equivalent resistance
calculated by this program might be wrong as the program might have been written
inappropriately.
4. Click Debug > Run to... to run the program.
5. To verify the correctness of the program, we will test the program with a simple series
circuits.

R5 R4

50 75

R3
105

R R2
1
79 6
8

Circuit 1
52

6. Next, we will calculate the equivalent resistance of the circuit. The equivalent resistance of
circuit 1 calculated (by calculator) using the formulas below is 377ohm.

Series: Req= R1+R2+R3+........+Rn

7. The equivalent resistance calculated by our program is 377ohm. Hence, the program is
correct. Figure below verifies that our program can calculate the equivalent resistance of
circuit 1 correctly.
53

 The Parallel-Series Circuit

R5 R3
100 2
R6 R2 R1
30 5 10

R4
2

For an example of the “Parallel-series circuit”, a circuit consisting of 6 resistor connected in


parallel-series circuit which are R1 (10 ohms) , R2 (5 ohms), R3 (2 ohms), R4 (2 ohms), R5
(100 ohms) and R6(30 ohms).

The resistors are arranged as in the circuit above.

Via calculator, the equivalent resistance is calculated using the formulas:

Series: Req= R1+R2+R3+........+Rn

Parallel: 1/Req=1/R1+1/R2+1/R3+..........1/Rn
54
55

Comparing the answer from the calculator which is 1.685393258 ohms and the answer
obtained from the program above, it is verified that they are similar to it. Hence, in can be
proven that the program is valid and is able to calculate the parallel-series circuit (Series circuit
in parallel)
56

Explanation

R5 R3
100 2 R2 R1
R6 5 10
30

R4
2

1. In order to determine the equivalent resistance of a circuit, the type of the circuit must
be identified. (Series, parallel or parallel-series circuit)
2. The calculation of resistance are done with a for loop and equation
Series: Req= R1+R2+R3+........+Rn
Parallel: 1/Req=1/R1+1/R2+1/R3+..........1/Rn
3. Figure below shows the explanation of the program to calculate the equivalent
resistance of the circuit above.
4. First, enter the number of line containing the resistors, as asked by the program. Hence,
there would be five lines in the circuit.

R5 R3 5 lines containing
100 2 resistor
R6 R2 R1
30 5 10

R4
2
57

5. Since all of these five lines each containing resistors, are considered parallel to each
other, the equivalent resistance of the whole circuit will definitely be the sum of
resistance using formula:

Parallel: 1/Req=1/R1+1/R2+1/R3+..........1/Rn

6. Yet, in each of the parallel-circuit lines, there would be other resistance connected to it
as shown in the diagram above which a resistor is connected in series to R4.

R5 R3
100 2 R2 R1 R3 & R4

R6 5 10 connected in
30 series

R4
2

7. Hence, we can determine the number of lines which contains more than one resistor in
each parallel line.
58

Only 1 line
which contain
R5 R3 more than 1
100 2 R2 R1 resistor
R6 5 10
30

R4
2

8. Hence, to get the accurate equivalent resistance, the sum of the resistor (R3 & R4)
should be obtained in order to calculate the total sum of resistance in the circuit using
the formulas:
Series: Req= R1+R2+R3+........+Rn

Combining R3 &
R4 to obtain a
R5 R3
100 2 R2 R1 sum of
R6 5 10 resistance in the
30 line

R4
2
59

9. Hence, the circuit would contain only of resistor connected in parallel. Therefore, the
equivalent resistance of the circuit can now be calaculated using the formula:

Parallel: 1/Req=1/R1+1/R2+1/R3+..........1/Rn

R5
100
R6 R2 R1
30 5 10 Rs = R3 + R4

Rs

Finally, the equivalent resistance calculated by our program is 1.68539 ohms as shown in the
console window below:
60

 The Coloured Bands of Resistor


1. First of all, we can give the single-character code for the coloured bands that marks a
resistor and compute it into resistance.

Colou Numb Code


r er
Black 0 B Colou Numb Code
Brow 1 N r er
n 2 R Green 5 G
Red 3 O Blue 6 E
Orang 4 Y Violet 7 V
e Gray 8 A
Yello White 9 W
w

2. Next, we can calculate the equivalent resistance by giving the codes. The equivalent
resistance of the codes (ROY) if calculated (by calculator) by using the formula below
is 230000ohm.
 R(Red) is the colour 1 with number of 2
 O(Orange) is the colour 2 with number of 3
 Y(Yellow) is the colour 3 with number of 4

Equivalent resistance= (10×colour 1+colour 2) ×10colour 3

3. The equivalent resistance which has been calculated by our program is 23000ohm.
Hence, our program is correct. Figure below verifies that our program can calculate
the equivalent resistance with this method correctly.
61

Gantt chart

Week
No. Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Analysis of the
1
program                            
Decomposition of
2
the poblem                            
Coding And
3
Debugging                            
4 Integration                            
Testing And
5
Validation                            
Program
6
Completed                            
7 Presentation                            
62

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 for the resistance of a

resistor according to the colour coded band on a unit of resistor.

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 be able to attain the resistance of a resistor using the colour

coded band calculation.


63

Bibliography

C++ Language Tutorial February 29 2000. <http://www.cplusplus.com/doc/tutorial/ >

C++ Reference April 2002. < http://www.cppreference.com/wiki/start>

C Programming and C++ Programming August 2005. < http://www.cprogramming.com/>

You might also like