1 Intro
1 Intro
Introduction to Computer
Science
2020-21: Semester 2
1
What is computer science?
MISCONCEPTION 1:
Computer science is the study of computers.
MISCONCEPTION 2:
Computer science is the study of how to write computer programs.
MISCONCEPTION 3:
Computer science is the study of the uses and applications of
computers and software.
2
What is computer science?
Computer science is the study of
algorithms including
3
An example of a very simple
algorithm
1. Wet your hair. Observe:
2. Lather your hair.
Operations need not be
3. Rinse your hair.
executed by a computer
4. Stop.
only, but by any entity
capable of carrying out the
operations listed.
4
A more complex example
Searching for a word in a dictionary
1. Start with page 1
2. Start with the first word on the page
3. If this word is the same as the target word, then
read its meaning
go to step 7
4. If more words on this page
move to the next word on this page
go to step 3
5. If reached the end of this page and more pages remain
move to next page
go to step 2
6. Declare target word as not found
7. Stop.
5
A more efficient algorithm
https://www.youtube.com/watch?v=OUP-
F5Oeng8
6
Formal and mathematical
properties of algorithms
Properties of an algorithm:
How efficient is it?
What kinds of resources must be used to
execute it?
How does it compare to other algorithms
that solve the same problem?
7
Linguistic realizations to
express algorithms
How do we represent algorithms?
Pseudocode, high level programming
languages, low level programming
languages
8
Course Materials
Additional books:
1. Programming with C, by Byron Gottfried
2. The C Programming Language, by Brian W Kernighan, Dennis
M Ritchie
10
Linguistic realizations to
express algorithms
Algorithms operate on data. Closely
intertwined is the representation of data
12
Simple data structure example
Find correlations
between interests,
use this to “pre fill” the
UxI matrix or to
reduce the interest
dimensions
At heart: Matrix
operations on very
large matrices.
More challenges:
Efficient search given
many ads to choose
from? 13
Hardware realizations to
execute algorithms
Algorithms need not execute on machines. All
we really need are computing entities.
Anything that can compute – e.g., a human.
But, ultimately, most of our interest will lie with
algorithms that execute on computing entities
called "computers".
How are these entities constructed?
14
Applications of algorithms
What are some of the many important
and popular applications of computers
in current use including:
modeling and simulation
information retrieval
numerical problem solving
data analysis
artificial intelligence
networking
graphics
15
What is computer science?
Computer science is the study of
algorithms including
16
A brief history of computing
Abacus
2nd century BC
Not really a computing device, but a
counting device to count large numbers
18
Napier’s Bones
46732 x 5
Add:
10
150
3500
30000
200000
1616 AD
Closer to computing
19
Pascaline: 1642 AD
20
Joseph Jacquard
In the late 1700s in France, Joseph Jacquard
invented a way to control the pattern on a weaving
loom used to make fabric.
Jacquard punched pattern holes into paper cards.
The cards told the loom what to do.
Instead of a person making every change in a
pattern, the machine made the changes all by
itself.
Jacquard's machine didn't count anything. So it
wasn't a computer or even a computing device. His
ideas, however, led to many other computing
inventions later.
22
Jacquard’s Loom
Intricate textile
patterns were
prized in France in
early 1800s.
Jacquard’s loom
(1805-6) used
punched cards to
allow only some
rods to bring the
thread into the
loom on each
shuttle pass.
23
Jacquard’s Loom
http://www.vam.ac.uk/content/videos/j/video-jacquard-weaving/
24
Charles Babbage
Babbage is known as the father
of modern computing because
he was the first person to design
a general purpose computing
device.
In 1822, Babbage began to
design and build a small working
model of an automatic
mechanical calculating machine,
which he called a "difference
engine".
25
Charles Babbage
Babbage continued work to produce a full scale working Difference
Engine for 10 years, but in 1833 he lost interest because he had a
"better idea"--the construction of what today would be described
as a general-purpose, fully program-controlled, automatic
mechanical digital computer.
28
Hollerith Tabulator
Hollerith founded the
Tabulating Machine
Company.
In 1924, the name of
the company was
changed to
International
Business Machines
Corporation (IBM).
This is the 1890
version used in
tabulating the 1890
federal census.
29
Punched cards
The punched card used
by the Hollerith
Tabulator for the 1890
US census.
30
The digital revolution: Mark 1
An electro-mechanical computer:
1939-1944
34
ENIAC – (Electrical Numerical Integrator And
Calculator), built by Presper Eckert and John
Mauchly at Moore School of Engineering,
University of Pennsylvania, 1941-46
36
ENIAC’s vacuum tubes
37
Vacuum tubes
38
Transistors! 1947 – Shockley,
Bardeem, and Brattain @ Bell Labs
39
From gates to logic circuits
1 bit adder
40
Left: Typical wiring in an early
mainframe computer
Right: An integrated circuit
Von Neumann architecture
There are 3 major units in a computer tied together by buses:
1) Memory: The unit that stores and retrieves instructions and data.
Processor
46
Executing instructions
47
Memory
Memory is a
collection of cells,
each with a unique
physical address
The size of a cell is
normally a power of
2, typically a byte (8
bites) today.
RAM: Random
Access Memory
32-bit vs. 64-bit
architecture 48
Control unit
49
Examples of OpCodes
Arithmetic OpCodes
0000 load
0001 store
0010 clear
0011 add
0100 increment
0101 subtract
0110 decrement
I/0 OpCodes
1101 in
1110 out
Logic/Control OpCodes
0111 compare
1000 jump
1001 jumpgt
1010 jumpeq
1011 jumplt
1100 jumpneq 50
All a computer does is:
Notion of clockspeed
51
How to Instruct a Computer ?
compile execute
Program Executable Output
53
The Language of Instructions
Humans can understand
Complicated sentences
English, French, Spanish
54
Features of an ISA
Example of instructions in an ISA
Arithmetic instructions : add, sub, mul, div
Logical instructions : and, or, not
Data transfer/movement instructions
55
Problems with programming using
instruction sets directly
Instruction sets of different types of CPUs are
different
Need to write different programs for computers with
different types of CPUs even to do the same thing
Solution: High level languages (C, C++,
Java,…)
CPU neutral, one program for many
Compiler to convert from high-level program to low
level program that CPU understands
Easy to write programs
56
Fundamentals: Binary
Numbers
57
Representing Positive Integers
Ancient Roman System
Symbol I V X L C D M
Issues :
There was no notion of 0
Very difficult to represent large numbers
Addition, and subtraction (very difficult)
58
Indian System
Example in base 10
59
Number Systems in Other Bases
Why do we use base 10 ?
because ...
60
What if we had a world in which
...
People had only two fingers.
61
Representing Numbers
base
Binary
1710 = 1 * 24 + 0 * 23 + 0 * 22 + 0 * 21 + 1 * 20 = 100012
Base = 2
62
Binary Number System
They would use a number system with
base 2.
Number in decimal Number in binary
5 101
100 1100100
500 111110100
1024 10000000000
63
MSB and LSB
MSB (Most Significant Bit) The leftmost bit
of a binary number. E.g., MSB of 1110 is 1
LSB (Least Significant Bit) The rightmost
bit of a binary number. E.g.,
LSB of 1110 is 0
64
Hexadecimal and Octal
Numbers
Hexadecimal numbers
Base 16 numbers – 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Start with 0x
Octal Numbers
Base 8 numbers – 0,1,2,3,4,5,6,7
Start with 0
65
Examples
Decimal Numbers:
10 Symbols {0,1,2,3,4,5,6,7,8,9}, Base or Radix is 10
136 = 1 102 + 3 101 + 6 100
Binary Numbers:
2 Symbols {0,1}, Base or Radix is 2
101 = 1 22 + 0 21 + 1 20
Octal Numbers:
8 Symbols {0,1,2,3,4,5,6,7}, Base or Radix is 8
621 = 6 82 + 2 81 + 1 80
Hexadecimal Numbers:
16 Symbols {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}, Base is 16
6AF = 6 162 + 10 161 + 15 160
66
Examples
Decimal Binary Octal Hexadecimal
9 1001 0 11 0x 9
12 1100 0 14 0x C
17 10001 0 21 0x 11
28 11100 0 34 0x 1C
n-1
D= b i 2i
i = -m
68
Examples
101011 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20
= 43
(101011)2 = (43)10
(101)2 = (5)10
69
Decimal to Binary: Integer Part
Consider the integer and fractional parts separately.
For the integer part:
Repeatedly divide the given number by 2, and go on
accumulating the remainders, until the number becomes zero.
Arrange the remainders in reverse order.
Base NumbRem
2 89
2 44 1
2 22 0
2 11 0
2 5 1
2 2 1
2 1 0
0 1
(89)10 = (1011001)2
70
Decimal to Binary: Integer Part
Consider the integer and fractional parts separately.
For the integer part:
Repeatedly divide the given number by 2, and go on
accumulating the remainders, until the number becomes zero.
Arrange the remainders in reverse order.
Base NumbRem
2 66
2 89 2 33 0
2 44 1 2 16 1
2 22 0 2 8 0
2 11 0 2 4 0
2 5 1 2 2 0
2 2 1 2 1 0
2 1 0 0 1
0 1
(66)10 = (1000010)2
(89)10 = (1011001)2
71
Decimal to Binary: Integer Part
Consider the integer and fractional parts separately.
For the integer part:
Repeatedly divide the given number by 2, and go on
accumulating the remainders, until the number becomes zero.
Arrange the remainders in reverse order.
0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111
73
Binary-to-Hexadecimal
Conversion
For the integer part,
Scan the binary number from right to left
Translate each group of four bits into the
corresponding hexadecimal digit
Add leading zeros if necessary
74
Example
1. (1011 0100 0011)2 = (B43)16
2. (10 1010 0001)2 = (2A1)16
75
Hexadecimal-to-Binary
Conversion
Translate every hexadecimal digit into its
4-bit binary equivalent
Examples:
(3A5)16 = (0011 1010 0101)2
(12)16 = (0001 0010)2
(1)16 = (0001)2
76
Bits and Bytes
Computers do not understand natural human languages, nor
programming languages
They only understand the language of bits
Bit 0 or 1
Byte 08 or
bits1
Word 40bytes
or 1
kiloByte 1024
0 or 1bytes
megaByte 1006 or
bytes
1
77
77
Fundamentals of C
78
First C program – print on screen
#include <stdio.h>
void main()
{
printf ("Hello, World! \n") ;
}
79
More printing … (code and see)
#include <stdio.h>
void main()
{
printf ("Hello, World! ") ;
printf ("Hello \n World! \n") ;
}
80
Some more printing
#include <stdio.h>
void main()
{
printf ("Hello, World! \n") ;
printf ("Hello \n World! \n") ;
printf ("Hell\no \t World! \n") ;
}
81
Reading values from keyboard
#include <stdio.h>
void main()
{
int num ;
scanf ("%d", &num) ;
printf (“No. of students is %d\n”, num) ;
}
82
Centigrade to Fahrenheit
#include <stdio.h>
void main()
{
float cent, fahr;
scanf(“%f”,¢);
fahr = cent*(9.0/5.0) + 32;
printf( “%f C equals %f F\n”, cent, fahr);
}
83
Largest of two numbers
#include <stdio.h>
void main()
{
int x, y;
scanf(“%d%d”,&x,&y);
if (x>y) printf(“Largest is %d\n”,x);
else printf(“Largest is %d\n”,y);
}
largest-1.c 84
What does this do?
#include <stdio.h>
void main()
{
int x, y;
scanf(“%d%d”,&x,&y);
if (x>y) printf(“Largest is %d\n”,x);
printf(“Largest is %d\n”,y);
}
largest-2.c 85
The C Character Set
The C language alphabet
Uppercase letters ‘A’ to ‘Z’
Lowercase letters ‘a’ to ‘z’
Digits ‘0’ to ‘9’
Certain special characters:
! # % ^ & * ( )
- _ + = ~ [ ] \
| ; : ‘ “ { } ,
. < > / ? blank
87
Variables
Very important concept for programming
An entity that has a value and is known to the
program by a name
Can store any temporary result while executing a
program
Can have only one value assigned to it at any given
time during the execution of the program
The value of a variable can be changed during the
execution of the program
88
Contd.
Variables stored in memory
Remember that memory is a list of storage
locations, each having a unique address
A variable is like a bin
The contents of the bin is the value of the variable
The variable name is used to refer to the value of
the variable
A variable is mapped to a location of the memory,
called its address
89
Example
#include <stdio.h>
void main( )
{
int x;
int y;
x=1;
y=3;
printf("x = %d, y= %d\n", x, y);
}
90
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 10
m
e
X = X +1
X = X*5
91
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 20
m
e
X = X +1
X = X*5
92
Variables in Memory
Instruction executed Memory location allocated
to a variable X
X = 10
T
i X = 20 21
m
e
X = X +1
X = X*5
93
Variables in Memory
Instruction executed
Memory location allocated
to a variable X
X = 10
T
i X = 20 105
m
e
X = X +1
X = X*5
94
Variables (contd.)
X = 20
X
Y=15 20
X = Y+3 ? Y
Y=X/6
95
Variables (contd.)
X = 20
X
Y=15 20
X = Y+3 15 Y
Y=X/6
96
Variables (contd.)
X = 20
X
Y=15 18
X = Y+3 15 Y
Y=X/6
97
Variables (contd.)
X = 20
X
Y=15 18
X = Y+3 3 Y
Y=X/6
98
Data Types
Each variable has a type, indicates what
type of values the variable can hold
Four common data types in C
int - can store integers (usually 4 bytes)
float - can store single-precision floating
point numbers (usually 4 bytes)
double - can store double-precision floating
point numbers (usually 8 bytes)
char - can store a character (1 byte)
99
Contd.
Must declare a variable (specify its type and
name) before using it anywhere in your program
All variable declarations should be at the
beginning of the main() or other functions
A value can also be assigned to a variable at the
time the variable is declared.
int speed = 30;
char flag = ‘y’;
100
More Data Types in C
Some of the basic data types can be augmented
by using certain data type qualifiers:
short
size qualifier
long
signed
unsigned sign qualifier
Typical examples:
short int (usually 2 bytes)
long int (usually 4 bytes)
unsigned int (usually 4 bytes, but no way to store + or
-)
101
Some typical sizes (some of these can vary
depending on type of machine)
Integer data Bit
Minimum value Maximum value
type size
char 8 -27=-128 27-1=127
short int 16 -215=-32768 215-1=32767
int 32 -231=-2147483648 231-1=2147483647
long int 32 -231=-2147483648 231-1=2147483647
-263=- 263-
long long int 64
9223372036854775808 1=9223372036854775807
unsigned char 8 0 28-1=255
unsigned short int 16 0 216-1=65535
unsigned int 32 0 232-1=4294967295
unsigned long int 32 0 232-1=4294967295
unsigned long lon 264-
g int
64 0
1=18446744073709551615
102
Variable Names
Sequence of letters and digits
First character must be a letter or ‘_’
No special characters other than ‘_’
No blank in between
Names are case-sensitive (max and Max are two
different names)
Examples of valid names:
i rank1 MAX max Min class_rank
Examples of invalid names:
a’s fact rec 2sqroot class,rank
103
More Valid and Invalid Identifiers
int x, y, sum;
scanf(“%d%d”,&x,&y); Values assigned
sum = x + y;
printf( “%d plus %d is %d\n”, x, y, sum );
}
106
Example - 2
#include <stdio.h>
void main()
{ Assigns an initial value to d2,
can be changed later
float x, y;
int d1, d2 = 10;
scanf(“%f%f%d”,&x, &y, &d1);
printf( “%f plus %f is %f\n”, x, y, x+y);
printf( “%d minus %d is %d\n”, d1, d2, d1-d2);
}
107
Read-only variables
Variables whose values can be initialized during
declaration, but cannot be changed after that
Declared by putting the const keyword in front of
the declaration
Storage allocated just like any variable
Used for variables whose values need not be
changed
108
Correct
void main() {
const int LIMIT = 10;
int n;
scanf(“%d”, &n); Incorrect: Limit changed
if (n > LIMIT)
printf(“Out of limit”); void main() {
const int Limit = 10;
}
int n;
scanf(“%d”, &n);
Limit = Limit + n;
printf(“New limit is %d”, Limit);
}
109
Constants
Integer constants
Consists of a sequence of digits, with possibly a plus
or a minus sign before it
Embedded spaces, commas and non-digit characters
are not permitted between digits
Floating point constants
Two different notations:
Decimal notation: 25.0, 0.0034, .84, -2.234
Exponential (scientific) notation
3.45e23, 0.123e-12, 123e2
e means “10 to the power of”
110
Contd.
Character constants
Contains a single character enclosed within a pair of
single quote marks.
Examples :: ‘2’, ‘+’, ‘Z’
Some special backslash characters
‘\n’ new line
‘\t’ horizontal tab
‘\’’ single quote
‘\”’ double quote
‘\\’ backslash
‘\0’ null 111
Input: scanf function
Performs input from keyboard
It requires a format string and a list of variables into
which the value received from the keyboard will be
stored
format string = individual groups of characters
(usually ‘%’ sign, followed by a conversion
character), with one character group for each
variable in the list
Examples
scanf ("%d", &size) ;
scanf ("%c", &nextchar) ;
scanf ("%f", &length) ;
scanf (“%d%d”, &a, &b);
113
Reading a single character
A single character can be read using scanf with
%c
It can also be read using the getchar() function
char c;
c = getchar();
117
Acknowledgements
Slides adapted from several sources:
http://www.cs.tulane.edu/~carola/teaching/cmps1500/fall13/slides/
http://www.cs.kent.edu/~jbaker/CS10051-Sp09/
http://www.computersciencelab.com/ComputerHistory/History.htm
https://cse.iitkgp.ac.in/~ppd/
118