Mandip Kumar Mandal

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Module Code & Module Title

CS4001NI Fundamental of Computing

Assessment Weightage & Type


100% Individual Coursework

Year and Semester


2019-20 Spring

Student Name: Mandip Kumar Mandal


London Met ID: 19033689
College ID: NP01CP4S200008
Assignment Due Date: 20Sep 2020
Assignment Submission Date: 20Sep 2020

I confirm that I understand my coursework needs to be submitted online via Google Classroom under the
relevant module page before the deadline in order for my assignment to be accepted and marked. I am
fully aware that late submissions will be treated as non-submission and a marks of zero will be awarded.
Table of Contents
1.INTRODUCTION .......................................................................................................... 4
1.1.Python ................................................................................................................... 4
1.2.Bit Adder ................................................................................................................ 4
1.3.Goal ....................................................................................................................... 4
1.4.Objectives .............................................................................................................. 4
2.PROGRAM................................................................................................................... 5
2.1.Model ..................................................................................................................... 5
2.2.Algorithm ............................................................................................................... 7
2.3.Flowchart ............................................................................................................... 8
2.4.Pseudocode ......................................................................................................... 12
2.4.1.Adder Module ................................................................................................ 12
2.4.2.Check Module ............................................................................................... 13
2.4.3.Converter Module .......................................................................................... 14
2.4.4.Gates Module ................................................................................................ 15
2.4.5.Main Module.................................................................................................. 16
3.Data Structures .......................................................................................................... 19
4.Testing ....................................................................................................................... 21
4.1.Normal data ......................................................................................................... 21
4.2Maximum values ................................................................................................... 21
4.3.Minimum value..................................................................................................... 22
4.4.Wrong data type .................................................................................................. 22
4.5.Wrong values ....................................................................................................... 22
5.Conclusion ................................................................................................................. 24
6.References ................................................................................................................. 25
7.Appendix .................................................................................................................... 26
List of Figures
Figure 1.8-bit adder model .............................................................................................. 5
Figure 2.1-bit adder model .............................................................................................. 6
Figure 3.Box model of 8-bit adder ................................................................................... 7
Figure 4.Screenshot of testing normal data ................................................................... 21
Figure 5.Screenshots of testing maximum value ........................................................... 21
Figure 6.Screenshots of testing minimum value ............................................................ 22
Figure 7.Screenshots of testing wrong data type .......................................................... 22
Figure 8.Screenshots of testing wrong value ................................................................ 23

List of Tables
Table 1.Truth table of 8-bit adder .................................................................................... 5
Table 2.Testing 1-Normal Data ..................................................................................... 21
Table 3.Testing 2-Maximum Value ................................................................................ 21
Table 4.Testing 3 of minimum value.............................................................................. 22
Table 5.Testing 4 of wrong data type ............................................................................ 22
Table 6.Testing 5 of wrong values ................................................................................ 22
CS4051NI Fundamental of Computing

1.INTRODUCTION
In this coursework going make a model of 1-byte adder with by combing the numbers of
a bit adder by taking the arithmetic circuits to make a single 1-byte adder. And also a the
program in python to deliver the idea of 1-byte adder through python programming.

1.1.Python
Python is an example of a high-level language. Python is available across many platforms
likes Windows, Linux and Mac OS. Python is considered an interpreted language
because Python programs are executed by an interpreter. There are two ways to use the
interpreter: commandline mode and script mode. In command-line mode, you type Python
programs and the interpreter prints the result. (Allen, Jeffrey, & Chris, April 2002)

1.2.Bit Adder
Bit adder is a arithmetic circuit which can constructed in half adder and full adder. Different
logical combines like and, or, nor, nand, xor, not to form a bit adder and combination of a
bit adders help us to make 2-bit adder,3-bit adder,4-bit adder and many more.

1.3.Goal
The main goal of this coursework is to make program that can run a byte adder model, a
logical gates and the program that can convert decimal into binary numbers.

1.4.Objectives
The objectives are as follows:
-A lot of research for topics as bit adder, flowchart, pseudocode, arithmetic circuits and
algorithms.
-A model for a byte adder.
-An algorithm of a byte adder.
-Flowchart of an algorithm.
-Pseudocode of a program.
-Testing of program.
CS4051NI Fundamental of Computing

2.PROGRAM
2.1.Model

Figure 1.8-bit adder model

A B Cin Sum Cout


0 0 0 0 0
0 1 0 1 0
1 0 0 1 0
0 0 0 0 0
0 1 0 1 0
1 1 0 1 1
1 0 1 1 0
Table 1.Truth table of 8-bit adder

The above table shows the addition of two decimal number 100 and 50. The two number
are converted into binary number. Additionally, 0 is there in last of B row because binary
of 50 is not 8-bit. Then it adds the binary numbers and gives the sum output which is
writing from downward to upward position.
CS4051NI Fundamental of Computing

Figure 2.1-bit adder model

Figure 3.Box model of 8-bit adder


CS4051NI Fundamental of Computing

2.2.Algorithm
An algorithm is a procedure or formula for solving a problem, based on conducting a
sequence of specified actions. A computer program can be viewed as an elaborate
sumlistscience,
algorithm. In mathematics and computer = an algorithm usually means a small
adder.adder(binarynu
procedure that solves a recurrent problem. (Rouse, 2016)
m1,binarynum2)
The algorithm are as follows:
Step 1: Start
stringlist =
Step 2: assign number 1 asconverter.string(sumli
input
st)
Step 3: assign number 2 as input
Step 4: checking if(number1= null).Else go to step 2
Step 5: checking if(number2= nu).decimalout
Else go to =
step 2
Step 6: numberlist=number1 converter.decimal(sum
list)
Step 7: numberlist2=number2
Step 8: checking if(numberlist = alphabets or characters). Else go to 6
print binary
Step 9: checking if(numberlist2= alphabets or characters). Else go to 7
sum and
Step 10: stringcheck = check.check(numberlist)
print decimal
sum
Step 11: stringcheck2 = check.check(numberlist2)
Step 12: if stringcheck == "ok". Else go to 10
Step 13: if stringcheck2 == "ok". Else go to 11
Step 14: numberint1= int (number1)
Step 15: numberint2 = int (number2)
Step 16: if numberint1>0 and number1<127. Else go to step 14
Step 17: if numberint2>0 and number2<127. Else go to step 15
Step 18: binarynum1 = converter.binary(numberint1)
Step 19: binarynum2 = converter.binary(numberint2)
Step 20: stringa = converter.string(binarynum1)
Step 21: stringb = converter.string(binarynum2)
Step 22: sumlist = adder.adder(binarynum1,binarynum2)
Step 23: stringlist = converter.string(sumlist)
CS4051NI Fundamental of Computing

Step 24: decimalout = converter.decimal(sumlist)


Step 25: print binary sum. Else print invalid number
sumlist
Step 26: print decimal sum. Else print = number
invalid
adder.adder(binarynu
Step 27: print error message m1,binarynum2)
Step 28: if asking user to=y go to step 2.
Step 29: end stringlist =
2.3.Flowchart converter.string(sumli
st) representation that illustrates the sequence of operations
A flowchart is a diagrammatic
to be performed to get the solution to a problem. It can be seen from the definition that
flow always accompanies with business or transaction. However, not all of the flows can
decimalout =
be expressed by flowcharts,converter.decimal(sum
unless these flows are based on some fixed routines and
stable links. (Freeman, 2020) list)

Start
print binary
sum and
print decimal
sum
Input
number1 & C
number 2

no

If number1
& number2

yes

numberlist=number1
numberlist2=number2 A
CS4051NI Fundamental of Computing

A
sumlist =
adder.adder(binarynu
m1,binarynum2)
if(numberlist=alphab
ets or characters)
stringlist =
if(numberlist2=alpha
converter.string(sumli
bets or characters)
st)

decimalout =
converter.decimal(sum
list)
stringcheck = check.check(numberlist)
stringcheck2 = check.check(numberlist2)
print binary
sum and
print decimal
sum

stringchec
k == "ok"
stringchec
k2 == "ok"
stringchec
k2 == "ok"

numberint1= int (number1)


numberint2 = int (number2) B
CS4051NI Fundamental of Computing

sumlist =
adder.adder(binarynu
m1,binarynum2)
if numberint1>0
and
number1<127
stringlist =
converter.string(sumli
if numberint2>0
st) and
number2<127

decimalout =
converter.decimal(sum
list)

binarynum1 =print binary


converter.binary(numberint1)
sum and
binarynum2 print
= decimal
sum
converter.binary(numberint2)

stringa =
converter.string(binarynum1)
Step 21: stringb =
converter.string(binarynum2)

sumlist =
adder.adder(binarynum1,binary
num2)
CS4051NI Fundamental of Computing

sumlist =
adder.adder(binarynu
m1,binarynum2)

stringlist =
converter.string(sumli
st)

decimalout =
converter.decimal(sum
list)

print binary
sum and
print decimal
sum

C if asking
user="y"

End
CS4051NI Fundamental of Computing

2.4.Pseudocode
Pseudocode is a detailed yet readable description of what a computer program or
algorithm must do, expressed in a formally-styled natural language rather than in a
programming language. Pseudocode is sometimes used as a detailed step in the process
of developing a program. It allows designers or lead programmers to express the design
in great detail and provides programmers a detailed template for the next step of writing
code in a specific programming language. (Rouse, defination pseudocode : whatis, 2020)

2.4.1.Adder Module
Import gates
Def adder(binarynum1,binarynum2)
Initialize sumval=0
Initialize cin=0
Initialize cout=0
Initialize counter=-1
Initialize sumlist to empty list
For I in range(length of binarynum1)
Set x= binarynum1[counter]
Set y= binarynum2[counter]
first = gates.XOR (x,y)
second = gates.NAND (cin,first)
third = gates.OR (cin , first)
fourth = gates.AND (second,third)
sumval = fourth
fifth = gates.AND (x,y)
sixth = gates.AND (first, cin)
seventh = gates.NOR (fifth, sixth)
eigth = gates.NOT (seventh)
cout = eigth
cin = cout
CS4051NI Fundamental of Computing

sumlist.append (sumval)
counter = counter -1
end for
initialize rightorder to empty list
initialize rightordercarry to empty list
initialize rev =-1
for j in range (length of sumlist)
append rightorder to sumlist(rev)
end for
rev-=1
return rightorder
2.4.2.Check Module
Def check(numberlist)
Initialize conformation to empty
Set num to list from 0 to 9
For I in range (length of numberlist)
Set singleval=numberlist
Set ans= singleval in num
If ans== False
Set conformation= “not”
Break
Else
Confoformation= “ok”
End if
End for
Return conformation
Def null(number)
Initialize conformation to empty
If number and not number.isspace()
CS4051NI Fundamental of Computing

Set conformation=”ok”
Else
Conformation=”not ok”
End if
Return conformation
2.4.3.Converter Module
Def binary(numberint)
Initialize binary into empty list
Initialize remainder to 0
For i in range(8)
Set remainder = number%2
Append binary to remainder
Set numberint=numberint//2
End for
Initialize counter=-1
Initialize binarylist to empty list
For j in range(length of binary)
Append binarylist to binary(counter)
Set counter-=1
End for
Return binarylist
Def string(binarylist)
Initialize stringlist to empty list
For k in range(length of binarylist)
Set stringval=str(binarylist[k])
Append stringlist to stringval
End for
Set number=’ ’.join(stringlist)
Return number
CS4051NI Fundamental of Computing

Def decimal(binarylist)
Initialize counter=-1
Initialize totao to 0
For l in range(length of binarylist)
Set bit=binarylist[counter]
Calculate total=total + bit*(2**1)
Set counter-=1
End for
Return total
2.4.4.Gates Module
def AND (initialize x and y):
if x==1 and y==1
return 1
else
return 0
end if

def XOR (initialize x and y):


if x==y
return 0
else
return 1
end if

def OR (initialize x and y):


if x==0 and y==0
return 0
else
return 1
CS4051NI Fundamental of Computing

end if

def NOT (initialize x):


if x==0
return 1
else
return 0
end if

def NOR (initialize x and y):


if x==0 and y==0
return 1
else
return 0
end if

def NAND (initialize x and y):


if x==1 and y==1
return 0
else
return 1
end if
2.4.5.Main Module
import converter
import adder
import check
initialize ans=”yes”
while ans==”yes”
CS4051NI Fundamental of Computing

input number1
input number2
check nullcheck1=check.null(number1)
check nullcheck2=check.null(number1)
if nullcheck1==”ok” and nullcheck2=”ok”
set numberlist=list(number1)
set numberlist2=list(number2)
check stringcheck = check.check(numberlist)
check stringcheck2 = check.check(numberlist2)
if stringcheck == "ok" and stringcheck2 == "ok"
set numberint1= int (number1)
set numberint2 = int (number2)
if numberint1>=0 and numberint1<=127 and numberint2>=0 and numberint2<=127
initialize sumlist to empty list
convert numberint1 into binary number
convert numberint2 into binary number
convert binarynum1 into string
convert binarynum2 into string
print numberint1=stringa
print numberint2=stringb
print +
calculate sumlist = adder.adder(binarynum1,binarynum2)
convert sumlist into string
convert sumlist into decimal
print sum(Binary)
print sum(Decimal)
else
CS4051NI Fundamental of Computing

print ("Invalid number.")


print ("Please enter a number in range (0 - 127)")
else
print ("Invalid number. Do not enter alphabets and other characters. ")
print ("Please enter a number in range (0 - 127)")
else
print ("Do not leave blank space")
print ("Please enter a number in range (0 - 127)")
input an=message
set ans=an.lower()
if ans is not equal to "y" or "Y"
print ("Program has ended. Thank you")
end if
End module
CS4051NI Fundamental of Computing

3.Data Structures
Data structures are fundamental concepts of computer science which helps is writing
efficient programs in any language. Python is a high-level, interpreted, interactive and
object-oriented scripting language using which we can study the fundamentals of data
structure in a simpler way as compared to other programming languages. (Python-DS
Introduction, 2020)

Python has great importance of data structures. In python there are two type of data
structures. They are primitive and non-primitive data structures.

In primitive there are four data structures. They are:

1.String

String collects the data like alphabets, special character or alphabets contains numbers.
In python you can create it with the pair of single or double quotes. Like “name” ,”
@gamil” or “address-12”.

2.Integers

Integers are used for collecting numbers data of any positive or negative numbers. Like
1,23 or-43.

3.Float

Float data structures are used to collect decimal numbers data. Like 1.23 or 33.445.

4.Boolean

Boolean data structures are used to collect data in true and false values only.

And in non-primitive are four data structures. They are as follows:

1.Lists

Lists are used to store data of different data types in a sequential manner. There are
addresses assigned to every element of the list, which is called as Index. The index value
starts from 0 and goes on until the last element called the positive index. There is
CS4051NI Fundamental of Computing

also negative indexing which starts from -1 enabling you to access elements from the last
to first. (Akash, 2020)

2.Dictionary

Dictionaries are used to store key-value pairs. To understand better, think of a phone
directory where hundreds and thousands of names and their corresponding numbers
have been added. Now the constant values here are Name and the Phone Numbers
which are called as the keys. And the various names and phone numbers are the values
that have been fed to the keys. If you access the values of the keys, you will obtain all the
names and phone numbers. So that is what a key-value pair is. And in Python, this
structure is stored using Dictionaries. (Akash, 2020)

3.Tuple

Tuples are the same as lists are with the exception that the data once entered into the
tuple cannot be changed no matter what. The only exception is when the data inside the
tuple is mutable, only then the tuple data can be changed. (Akash, 2020)

4.Sets

Sets are a collection of unordered elements that are unique. Meaning that even if the data
is repeated more than one time, it would be entered into the set only once. It resembles
the sets that you have learnt in arithmetic. The operations also are the same as is with
the arithmetic sets.

In the program, I have used string and list.


CS4051NI Fundamental of Computing

4.Testing
4.1.Normal data
Action: Enter 25 and 30
Expected output: Numbers should be converted binary and correct sum of
binary and decimal should be displayed.
Actual output: The numbers are converted into binary and the correct sum
of binary and decimal is displayed.
Conclusion: Test successful.
Table 2.Testing 1-Normal Data

Figure 4.Screenshot of testing normal data

4.2Maximum values
Action: Enter 127 and 127
Expected output: Numbers should be converted binary and correct sum of
binary and decimal should be displayed.
Actual output: The numbers are converted into binary and the correct sum
of binary and decimal is displayed.
Conclusion: Test successful.
Table 3.Testing 2-Maximum Value

Figure 5.Screenshots of testing maximum value


CS4051NI Fundamental of Computing

4.3.Minimum value
Action: Enter 1 and 1
Expected output: Numbers should be converted binary and correct sum of
binary and decimal should be displayed.
Actual output: The numbers are converted into binary and the correct sum
of binary and decimal is displayed.
Conclusion: Test successful.
Table 4.Testing 3 of minimum value

Figure 6.Screenshots of testing minimum value

4.4.Wrong data type


Action: Enter a and b
Expected output: Error should be displayed.
Actual output: Error is displayed.
Conclusion: Test successful.
Table 5.Testing 4 of wrong data type

Figure 7.Screenshots of testing wrong data type

4.5.Wrong values
Action: Enter a and b
Expected output: Error should be displayed.
Actual output: Error is displayed.
Conclusion: Test successful.
Table 6.Testing 5 of wrong values
CS4051NI Fundamental of Computing

Figure 8.Screenshots of testing wrong value


CS4051NI Fundamental of Computing

5.Conclusion
In doing this coursework required lot of research in topics like bit adder, flowchart,
algorithm, pseudocode and arithmetic circuits as and, or, nand, xor, not and nor gate. And
also the data structures of python. This research helped me to solve the queries I have
and fixed any kind of problems. Many problems were faced while going through this
coursework which I overcome by going through the lecture, tutorial and lab classes
provided by our respected teachers. While doing the coursework also teachers have
supported and gave knowledge to complete the coursework in given interval of time. The
coursework was difficult and challenging too but it became easy with research and by
intracting with respected teachers. By doing all this I have a lot of knowledge about bit
adder and data structure of python. I am very glad and happy to complete this coursework
in given interval of time.
CS4051NI Fundamental of Computing

6.References
Akash. (2020, September 17). Data Structures You Need To Learn In Python. Retrieved
from edureka: https://www.edureka.co/blog/data-structures-in-python/#list
Allen, D., Jeffrey, E., & Chris, M. (April 2002). How to Think Like a Computer Scientist :
Learning with Python. Wellesley, Massachusetts: Green Tea Press.
Freeman, J. (2020, September 16). Flow chart Definitation. Retrieved from Edward:
https://www.edrawsoft.com/flowchart-definition.html
Python-DS Introduction. (2020, September 17). Retrieved from tutorialspoint:
https://www.tutorialspoint.com/python_data_structure/python_data_structure_intr
oduction.htm
Rouse, M. (2016, September 27). whatis-techtarget. Retrieved from TechTarget:
https://whatis.techtarget.com/definition/algorithm
Rouse, M. (2020, September 17). defination pseudocode : whatis. Retrieved from
whatis: https://whatis.techtarget.com/definition/pseudocode
CS4051NI Fundamental of Computing

7.Appendix
Adder.py
import gates
# calls the functions of gates and adds binary number according to model
# stores sum as list
def adder(binarynum1,binarynum2):
sumval=0
cin=0
cout=0
counter=-1
sumlist=[]
for i in range ( len (binarynum1)):
#addition is done from right side to index (z) starts from -1
x = binarynum1[counter]
y = binarynum2[counter]
first = gates.XOR (x,y)
second = gates.NAND (cin,first)
third = gates.OR (cin , first)
fourth = gates.AND (second,third)
sumval = fourth
fifth = gates.AND (x,y)
sixth = gates.AND (first, cin)
seventh = gates.NOR (fifth, sixth)
eigth = gates.NOT (seventh)
cout = eigth
cin = cout
sumlist.append (sumval)
counter = counter -1
CS4051NI Fundamental of Computing

#reverse the list as final value is taken from left to right


#numbers are added from right to left
rightorder=[]
rightordercarry=[]
rev = -1
for j in range (len (sumlist)):
rightorder.append(sumlist[rev])
rev -=1
return rightorder

check.py
def check(numberlist):
conformation=""
num = ['0','1','2','3','4','5','6','7','8','9']
for i in range( len(numberlist)):
singleval= numberlist[i]
ans = singleval in num
if ans ==False:
conformation = "not"
break
else :
conformation = "ok"
return conformation

def null(number):
conformation=""
if number and not number.isspace():
conformation = "ok"
CS4051NI Fundamental of Computing

else :
conformation = "not ok "
return conformation

converter.py
def binary (numberint):
#converts integer number to binary and stores in list
binary=[]
remainder=0
for i in range(8) :
remainder = numberint % 2
binary.append(remainder)
numberint = numberint//2
counter=-1
binarylist=[]
for j in range (len(binary)):
binarylist.append(binary[counter])
counter-=1
return binarylist
def string(binarylist):
#takes binary list and coverts it to string
stringlist=[]
for k in range(len(binarylist)):
stringval=str(binarylist[k])
stringlist.append(stringval)
number = ''.join(stringlist)
return number
def decimal(binarylist):
CS4051NI Fundamental of Computing

#takes binary list and converts it into (decimal) integer


counter=-1
total=0
for l in range (len(binarylist)):
bit = binarylist[counter]
total = total + bit*(2**l)
counter-=1
return total

gate.py
# the functions act as logic gates for binary values
def AND (x,y):
if x==1 and y==1 :
return 1
else :
return 0
def XOR (x,y):
if x==y :
return 0
else :
return 1

def OR (x,y):
if x== 0 and y == 0 :
return 0
else :
return 1
CS4051NI Fundamental of Computing

def NOT(x):
if x == 0 :
return 1
else :
return 0

def NOR (x,y):


if x== 0 and y == 0 :
return 1
else :
return 0

def NAND (x,y):


if x==1 and y==1 :
return 0
else :
return 1

Main.py
import converter
import adder
import check

#answer is set to y to loop program as long as user enters y/Y


ans = "y"
while ans == "y":
print ("---------------------------------------------------------------------------")
number1 = (input ("enter a number from 0-127 : "))
CS4051NI Fundamental of Computing

number2 = (input ("enter a number from 0-127 : "))


print ("----------------------------------------")

#calls function in check module to see if value is null or not


nullcheck1 = check.null(number1)
nullcheck2 = check.null(number2)

if nullcheck1 == "ok" and nullcheck2 == "ok":#condition which determines both


variables have values
numberlist = list(number1)
numberlist2 = list(number2)

#calls function in check module to see if there are any aplhabets or characters
stringcheck = check.check(numberlist)
stringcheck2 = check.check(numberlist2)

if stringcheck == "ok" and stringcheck2 == "ok":#condition which determines both


values are numeric
numberint1= int (number1)
numberint2 = int (number2)

if numberint1>=0 and numberint1<=127 and numberint2>=0 and


numberint2<=127:#condtion set to accept only values between 0 -127
sumlist=[]

#calls function in converter module to convert number to binary


binarynum1 = converter.binary(numberint1)
binarynum2 = converter.binary(numberint2)
CS4051NI Fundamental of Computing

#calls function in converter which converts list value to string value


stringa = converter.string(binarynum1)
stringb = converter.string(binarynum2)

print (" ",numberint1, " = ", stringa)


print (" + ",numberint2, " = ", stringb)
print ("-------------------------------")

sumlist = adder.adder(binarynum1,binarynum2)#calls adder function

stringlist = converter.string(sumlist)# convverts binary sum from list to string

decimalout = converter.decimal(sumlist)# converts binary sum to decimal sum

print("Sum (Binary) = ", stringlist)


print("Sum (Decimal) = ", decimalout)
print("---------------------------------------------------------------------------")

else :
print("Invalid number. ")
print("Please enter a number in range (0 - 127)")
print("---------------------------------------------------------------------------")

else :
print("Invalid number. Do not enter alphabets and other characters. ")
print("Please enter a number in range (0 - 127)")
print("---------------------------------------------------------------------------")
CS4051NI Fundamental of Computing

else :
print("Do not leave blank space ")
print("Please enter a number in range (0 - 127)")
print("---------------------------------------------------------------------------")

an = input ("Do you want to restart the program?(press y or Y to continue)")


ans = an.lower()
if ans != "y" or "Y":
print("Program ended. Thank you")
print("--------------------------------------X-------------------------------------")

You might also like