Top Banner
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
82

Computer Programming EEU 101 Total Resistance Calculations Project

Mar 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Programming EEU 101 Total Resistance Calculations Project

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

Page 2: Computer Programming EEU 101 Total Resistance Calculations Project

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

Page 3: Computer Programming EEU 101 Total Resistance Calculations Project

3

Contents Page

Page 4: Computer Programming EEU 101 Total Resistance Calculations Project

4

1 Acknowledgement 2

2 Table of Contents 3

3 Abstract 4

4 Introduction 5

5 Minutes of Meetings 6-9

6 Problem Analysis 105.15.2

Problem StatementInput and Output Analysis

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

Page 5: Computer Programming EEU 101 Total Resistance Calculations Project

5

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 0Brown 1Red 2Orange 3Yellow 4Green 5Blue 6Violet 7Gray 8White 9

Introduction

The integer codes of the bands are in the order of colour1, colour2 and colour3. The resistance in ohms is calculated using this formula:

Resistance = (10 x colour1 + colour2) x 10colour 3

Page 6: Computer Programming EEU 101 Total Resistance Calculations Project

6

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

Page 7: Computer Programming EEU 101 Total Resistance Calculations Project

7

Meeting Topic

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

Date 26th January 2011 From 08:30 PM To 10:00 PM

Location Café Lembaran Note Taker

Nurul Wahieda Muhamad Bustaman 108488

Duration 1.5 hr

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

12

The problem statement, input and output analysis are done.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

Page 8: Computer Programming EEU 101 Total Resistance Calculations Project

8

Meeting Topic Coding and debugging.

Date28th February 2011

From 08:30 PM To 10:00 PM

Location Café Lembaran Note Taker

Nurul Wahieda Muhamad Bustaman 108488

Duration 1.5 hr

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 next meeting.

All

Minutes of 3rd Meeting

Summary

Page 9: Computer Programming EEU 101 Total Resistance Calculations Project

9

Meeting Topic

Testing, validation and integration

Date 16th March 2011 From 08:30 PM To 10:00 PM

Location Café Lembaran Note Taker

Nurul Wahieda Muhamad Bustaman 108488

Duration 1.5 hr

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

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 each team members.

All

Minutes of 4th Meeting

Summary

Page 10: Computer Programming EEU 101 Total Resistance Calculations Project

10

Meeting Topic

Presentation preparation

Date 28th March 2011 From 08:30 PM To 10:00 PM

Location Café Lembaran Note Taker

Nurul Wahieda Muhamad Bustaman 108488

Duration 1.5 hr

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

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

Page 11: Computer Programming EEU 101 Total Resistance Calculations Project

11

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:

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 DecompositionTotal Resistance

Calculation

Page 12: Computer Programming EEU 101 Total Resistance Calculations Project

12

Parallel circuit

The Programmers

Series circuit

DisclaimerParallel-Series circuit

Coloured Bands of Resistor

Total resistance (ohm)

Value of each resistor (ohm)

Number of resistor

Value of resistors (ohm) of each line

Number of lines having more than one resistor

Number of lines between node x and node y

Number of resistor in each line

First colour’s

code

Codes of each colour

Second colour’s

code

Third colour’s

code

Page 13: Computer Programming EEU 101 Total Resistance Calculations Project

13

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

circuit == 1and circuit ==2

Input: number

Start for loop for main display menu

(a) (d) (c)

circuit==3

Display "Enter Total Number of Parallel Resistor:"

Display "Enter Value of Parallel Resistor #"

input=R

Display colour-code table and “Enter three resistor code”.

input get a, b, c

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

Display ‘d’

(b)

Page 14: Computer Programming EEU 101 Total Resistance Calculations Project

14

input =’a’

Display " finalsum2”

Display names of programmers

Display ‘R_equiv’

R_equiv = (R_equiv * R ) / ( R_equiv + R )

Display disclamer quote

Get no_of_line, no_of_line_morethan1r,

“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”

Page 15: Computer Programming EEU 101 Total Resistance Calculations Project

15

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.

Page 16: Computer Programming EEU 101 Total Resistance Calculations Project

16

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

language which includes functions involving memory allocation, process

control, conversions and others.

<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.

Page 17: Computer Programming EEU 101 Total Resistance Calculations Project

17

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.

Page 18: Computer Programming EEU 101 Total Resistance Calculations Project

18

Page 19: Computer Programming EEU 101 Total Resistance Calculations Project

19

Page 20: Computer Programming EEU 101 Total Resistance Calculations Project

20

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.

Page 21: Computer Programming EEU 101 Total Resistance Calculations Project

21

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.

Page 22: Computer Programming EEU 101 Total Resistance Calculations Project

22

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.

Page 23: Computer Programming EEU 101 Total Resistance Calculations Project

23

Page 24: Computer Programming EEU 101 Total Resistance Calculations Project

24

Page 25: Computer Programming EEU 101 Total Resistance Calculations Project

25

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

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

Page 26: Computer Programming EEU 101 Total Resistance Calculations Project

26

Page 27: Computer Programming EEU 101 Total Resistance Calculations Project

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.

Page 28: Computer Programming EEU 101 Total Resistance Calculations Project

28

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();

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;

Page 29: Computer Programming EEU 101 Total Resistance Calculations Project

29

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;

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

Page 30: Computer Programming EEU 101 Total Resistance Calculations Project

30

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) << "-->";

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";

Page 31: Computer Programming EEU 101 Total Resistance Calculations Project

31

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";

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";

Page 32: Computer Programming EEU 101 Total Resistance Calculations Project

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;

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";

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);

Page 33: Computer Programming EEU 101 Total Resistance Calculations Project

33

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

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

Page 34: Computer Programming EEU 101 Total Resistance Calculations Project

34

//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,

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;

Page 35: Computer Programming EEU 101 Total Resistance Calculations Project

35

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)){

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}

Page 36: Computer Programming EEU 101 Total Resistance Calculations Project

36

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 () ;

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}

Page 37: Computer Programming EEU 101 Total Resistance Calculations Project

37

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";

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";

Page 38: Computer Programming EEU 101 Total Resistance Calculations Project

38

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)){

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";

Page 39: Computer Programming EEU 101 Total Resistance Calculations Project

39

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)

{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";

Page 40: Computer Programming EEU 101 Total Resistance Calculations Project

40

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

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

Page 41: Computer Programming EEU 101 Total Resistance Calculations Project

41

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;

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;

Page 42: Computer Programming EEU 101 Total Resistance Calculations Project

42

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;

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;

Page 43: Computer Programming EEU 101 Total Resistance Calculations Project

43

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;

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;

Page 44: Computer Programming EEU 101 Total Resistance Calculations Project

44

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;

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; }

Page 45: Computer Programming EEU 101 Total Resistance Calculations Project

45

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;

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;

Page 46: Computer Programming EEU 101 Total Resistance Calculations Project

46

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; }

Page 47: Computer Programming EEU 101 Total Resistance Calculations Project

47

Testing and Validation

The Parallel Circuit

When the user keys in 1

Page 48: Computer Programming EEU 101 Total Resistance Calculations Project

48

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,

Page 49: Computer Programming EEU 101 Total Resistance Calculations Project

49

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

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

Page 50: Computer Programming EEU 101 Total Resistance Calculations Project

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.

Page 51: Computer Programming EEU 101 Total Resistance Calculations Project

51

50

R3

105

R1

R2

R4

75

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.

Circuit 1

68

79

R5

Page 52: Computer Programming EEU 101 Total Resistance Calculations Project

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.

Page 53: Computer Programming EEU 101 Total Resistance Calculations Project

53

The Parallel-Series Circuit

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

2R3R5

100R110

R630

R42

R25

Page 54: Computer Programming EEU 101 Total Resistance Calculations Project

54

Page 55: Computer Programming EEU 101 Total Resistance Calculations Project

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)

Page 56: Computer Programming EEU 101 Total Resistance Calculations Project

56

Explanation

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 equationSeries: 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.

2R3R5

100 R110 R6

30

R42

R25

2R3R5

100R110

R630

R42

R25

5 lines containing resistor

Page 57: Computer Programming EEU 101 Total Resistance Calculations Project

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.

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

2R3R5

100 R110 R6

30

R42

R25

R3 & R4 connected in

series

Page 58: Computer Programming EEU 101 Total Resistance Calculations Project

58

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

2R3R5

100 R110 R6

30

R42

R25

Only 1 line which contain more

than 1 resistor

Page 59: Computer Programming EEU 101 Total Resistance Calculations Project

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

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

2R3R5

100 R110 R6

30

R42

R25

R5100

R110

R630

Rs

R25

Combining R3 & R4 to obtain a

sum of resistance in the line

Rs = R3 + R4

Page 60: Computer Programming EEU 101 Total Resistance Calculations Project

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.

Colour Number Code

BlackBrownRedOrange Yellow

01234

BNROY

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

Colour Number Code

GreenBlue VioletGrayWhite

56789

GEVAW

Page 61: Computer Programming EEU 101 Total Resistance Calculations Project

61

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.

Gantt chart

No. ActivityWeek

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 Analysis of the program

2 Decomposition of the poblem

3 Coding And Debugging

4 Integration

5 Testing And Validation

6 Program Completed

7 Presentation

Page 62: Computer Programming EEU 101 Total Resistance Calculations Project

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.

Page 63: Computer Programming EEU 101 Total Resistance Calculations Project

63

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.

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/>

Page 64: Computer Programming EEU 101 Total Resistance Calculations Project

64