0% found this document useful (0 votes)
31 views79 pages

Introduction

This document provides an overview of the CSFC0101 Computer Programming course being offered in Autumn 2023-24. It lists the instructor's contact information and recommended textbooks. It also outlines the course evaluation criteria including weightage of quizzes, midterm and final exams. Details are provided about the number and scheduling of quizzes as well as attendance policy. The document ends by inviting any questions from students.

Uploaded by

Aka Radhika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views79 pages

Introduction

This document provides an overview of the CSFC0101 Computer Programming course being offered in Autumn 2023-24. It lists the instructor's contact information and recommended textbooks. It also outlines the course evaluation criteria including weightage of quizzes, midterm and final exams. Details are provided about the number and scheduling of quizzes as well as attendance policy. The document ends by inviting any questions from students.

Uploaded by

Aka Radhika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 79

CSFC0101

Computer Programming
Autumn 2023-24

Introduction & Administrivia

Dr. Lalatendu Behera


Dept. of CSE
NIT Jalandhar
Email: [email protected]

1
Textbooks

1. B. W. Kernighan and D. M. Ritchie, “The C


Programming Language”, Pearson Education, 2006.

2. Byron S. Gotfried, “Programming with C”, Schaum's


Outlines, Tata Mcgraw-hill, 2006.

3. Y. P. Kanetkar, “Let us C”, BPB Publication, 2011.

4. E. Balagurusamy, “Programming in ANSI C”, Tata


Mcgraw-hill, 2019.

2
Online Resource

Links to Source Material on Course Web Page:


https://lalatendubehera.github.io/lalatendu/
CSCI-101-23241A2.html

3
Course Evaluation
• Weightage
 Quizzes (4): 20%
 Midsem: 30%
 Endsem: 50%

4
Quiz Details
• Four Quizzes
• Dates will be announced one or two days prior
to the exam date.
• For the lab groups A3a and A3c, the quizzes
will be conducted in the lab session.
• A makeup quiz may be conducted only on
producing a genuine medical certificate from
an institute doctor.

5
Attendance
• 75% attendance is mandatory.
• The rest includes your sick leave, casual leave,
etc.
• Class bunk will be considered as absent.

6
Questions?

7
Computer Fundamentals and C
Programming

8
What Is A Computer?
A computer is an electronic device,

• Accepts input

• Processes data

• Stores data

• Produces output
9
Hardware Devices that comprise a computer system

Monitor System unit


(output) Speaker
(processor, memory…)
(output)

Printer
(output)

Storage devices
Mouse (CD-RW, Floppy,
(input) Hard disk, …)

Scanner Keyboard
(input) (input)

10
11
CPU

12
CPU
 The CPU is a silicon chip that contains millions of tiny
electrical components.

 The CPU’s three main parts are:


 Control Unit
 Arithmetic Logic Unit (ALU)
 Registers

13
Performs calculations and
decisions
Arithmetic / Logic Unit

Coordinates
Control Unit processing steps

Small, fast
Registers storage areas for
instructions and
data

14
15
Registers
 Registers are small, fast memory within the
CPU
 Different registers hold different things like
instructions and addresses of instructions, data
(operands) and results of operations
Two types
1. Special Purpose Registers contain specific information
the CPU needs.
 Instruction Register (IR) contains the actual instruction
which is currently being executed by the CPU.
• The Program Counter (PC) contains the address of the
next instruction to be executed by the program.
2.General Purpose Registers hold:
 the operands for arithmetic and logical operations (ie. the
values on which the operation will be performed)
 the results of such operations
 So General Purpose Registers are used for holding and
manipulating data used by the CPU
17
Memory

18
Memory
Computer Memory : millions/billions of on/off charges
• Bits: 0 or 1
• Bytes: Groups of 8 bits, A byte is the smallest unit of storage.
(Can hold one text character)
• Words: Groups of bits/bytes (8, 16, 32, 64-bits)
Storage is usually too large to be expressed in bytes or words.
 Kilobyte (KB) = 1024 bytes (210 bytes)
 Megabyte (MB) = 1024 x 1024 bytes or one million bytes (220 bytes)
 Gigabyte (GB) = 1024 x 1024 x 1024 bytes or 1 trillion bytes (230
bytes)
 Terabyte (TB) = 1024 x 1024 x 1024 x 1024 bytes one quadrillion
bytes (240 bytes)
19
Memory
• What is the difference between followings:
– RAM
– CACHE
– SSD
– HDD
MEMORY CLASSIFICATION
Registers
2ns
Cache (I, II, III)
Volatile SRAM
DRAM
Primary SDRAM
(Semiconductor – RAM EDRAM
chip). Main Memory
EDO
FLASH RAM
Memory Types &
Storage Devices PROM
Non-volatile EPROM
ROM EEPROM
Tape
Magnetic memory HD, Zip Disk
Secondary FDD
(Devices)
Optical
CD-ROM, CD-R, CD-RW
Memory DVD-ROM, DVD-R 5ms
DVD- RW
Memory
Main Memory

Each memory cell 5248 Each memory cell stores a


has a numeric 5249 set number of bits (some
10011010
address, which 5250 computers use 8 bits/one
uniquely identifies 5251 byte, others use words)
its location 5252
5253 A word is stored in
5254 consecutive
5255 memory bytes.
5256
Random Access Memory (RAM) –
• It is also called as read write memory or
the main memory or the primary memory.
• The programs and data that the CPU requires
during execution of a program are stored in this
memory.
• It is a volatile memory as the data loses when the
power is turned off.
– RAM is further classified into two types- SRAM (Static
Random Access Memory) and DRAM (Dynamic
Random Access Memory).
Read Only Memory (ROM) –
• Stores crucial information essential to operate
the system, like the program essential to boot
the computer.
– It is not volatile.
– Always retains its data.
• Used in embedded systems or where the
programming needs no change.
• Used in calculators and peripheral devices.
Types of Read Only Memory (ROM) –
• PROM (Programmable read-only memory) – It can be
programmed by user. Once programmed, the data and
instructions in it cannot be changed.

• EPROM (Erasable Programmable read only memory) –


It can be reprogrammed. To erase data from it, expose
it to ultra violet light. To reprogram it, erase all the
previous data.

• EEPROM (Electrically erasable programmable read


only memory) – The data can be erased by applying
electric field, no need of ultra violet light. We can erase
only portions of the chip.
Cache Memory

27
Cache Memory
• But as CPU speeds became faster, the main
memory couldn’t provide the CPU with the
instructions at a fast enough rate.
• So even faster memory ( cache memory) is now
placed between the CPU and main memory to
provide the instructions at an quicker rate to
the CPU.
Cache Memory

When an instruction or data is accessed from main memory, it is placed in


the cache. Second and subsequent use of the same instruction/data will
then be faster, since it is accessed directly from the cache.
Cache
L1 and L2 Cache
• Most modern CPUs now have a cache
memory (L1), on the same silicon wafer
as the CPU, to provide the CPU with
instructions at the same clock speed as
the CPU.

• An additional off-the-chip secondary


cache (L2) may also interact with the
CPU at a slower speed.
Main Memory Characteristics
• Very closely connected to the CPU.
• Contents are quickly and easily changed.
• Holds the programs and data that the
processor is actively working with.
• Interacts with the processor millions of times
per second.
• Nothing permanent is kept in main memory.
Secondary Storage Devices
 Storage devices hold data, even when the
computer is turned off.
 The physical material that actually holds
data is called a storage medium. The
surface of a floppy disk is a storage
medium.
 The two primary storage technologies are
magnetic and optical.

33
Secondary Storage Devices

 Magnetic Storage Devices


• Diskettes (floppy disks) (FDD)
• Hard disks (HD)
• USB flash drive
• Magnetic tape

 Optical Storage Devices


• Compact Disk Read-Only Memory (CD-ROM)
• Digital Video Disk Read-Only Memory
• (DVD-ROM
• CD-Recordable (CD-R)
• CD-Rewritable (CD-RW)
Secondary Storage Characteristics
• Connected to main memory through a bus and a
device controller.
• Contents are easily changed, but access is very
slow compared to main memory.
• Only occasionally interacts with CPU.
• Used for long-term storage of programs and data.
• Much larger than main memory (GBs vs. MBs).
CPU interaction with Memory

37
CPU and Memory
• CPU can interact with main memory in two
ways:
– It can write a byte/word to a given memory location.
• The previous bits that were in that location are
destroyed
• The new bits are saved for future use.
– It can read a byte/word from a given memory location.
• The CPU copies the bits stored at that location and
stores them in a CPU register
• The contents of the memory location are NOT
changed.
Access to Instructions

The hard disk is too slow to provide instructions to the CPU.


So programs are first loaded into main memory, which is much faster.
The CPU can then access the instructions more quickly.
How Does a Computer Know what to
do?
• It must be given a detailed list of instructions,
called a compute program or software, that tells
it exactly what to do.
• Before processing a specific job, the computer
program corresponding to that job must be
stored in memory.
• Once the program is stored in memory the
compute can start the operation by executing the
program instructions one after the other.

40
**Program**

41
Program
• A computer program is a collection
of instructions that performs a specific task
when executed by a computer.
• Most computer devices require programs to
function properly.

• Program is a sequence of instruction along


with data

42
Programming Language
• A programming language is a formal
language, which comprises a set of
instructions that produce various kinds
of output.

• Programming languages are used in computer


programming to implement algorithms.

43
Programming paradigms (models)
• Unstructured

• Structured

• Object oriented

44
Unstructured programming
• Writing small and simple programs consisting
of only one main function in the program.

• Uses goto statement to jump from any


statement to any other statement (spagetti
code)

45
Structured programming
• A program is broken down into small
independent tasks that are small enough to be
understood easily, without having to
understand the whole program at once.

• When these tasks are completed, they are


combined together to solve the problem.

• Example – C language
46
What is?
• Editor

• Translator
– Complier
– Assembler
– Interpreter
• Linker
• Loader
Translator
• A translator is a programming language
processor
– that converts a computer program from one
language to another.
• It takes a program written in source code and
converts it into machine code.
• It discovers and identifies the error during
translation.

48
49
Complier
• A compiler is a program that accepts a source
program in a “high-level language “and
produces a corresponding object program.
Assembler
• Input to an assembler is an assembly language
program.
• Output is an object program plus information
that enables the loader to prepare the object
program for execution.
Loader
• A Loader is a routine that loads an object
program and prepares it for execution.
• In general, the loader must load, relocate and
link the object program.
• Loader is a program that places programs into
memory and prepares them for execution. The
loader places into memory the machine
language version of the user’s program and
transfers control to it.
First Program
#include<stdio.h>
/*first program*/
int main()
{
printf(“Hello-CP”);
return 0;
}

53
Second Program
#include<stdio.h>
/*first program*/
int main()
{
int a;
a=4+7;
printf(“value=%d”,a);
return 0;
}
54
How a simple program (C/C++)
executes??
What happens when we turn on
computer?
• Bootstrap program is loaded at power-up or
reboot
– Typically stored in ROM or EPROM, generally
known as firmware
– Initializes all aspects of system
– Loads operating system kernel and starts
execution
System Boot
• When power initialized on system, execution starts at a
fixed memory location
– Firmware ROM used to hold initial boot code
• Operating system must be made available to hardware
so hardware can start it
– Small piece of code – bootstrap loader, stored in ROM or
EEPROM locates the kernel, loads it into memory, and
starts it
– Sometimes two-step process where boot block at fixed
location loaded by ROM code, which loads bootstrap
loader from disk
• Common bootstrap loader, GRUB, allows selection of
kernel from multiple disks, versions, kernel options
• Kernel loads and system is then running
What is the difference between 32 bit
and 64 bit OS??
Answer
• When talking about CPUs, the number of bits
usually refers to the size of a CPU’s register.
Each register bit can reference a byte in
memory.
• 16 bit computers can reference up to 64KB of
RAM and 32 bit computers can reference up
to 4GB of RAM.
• A 64 bit CPU can reference up to 16EB (8TB) of
RAM.
Answer
• In computing, 32-bit and 64-bit are two different types
of processors. The bit number (usually 8, 16, 32, or 64)
refers to how much memory a processor can access
from the CPU register.
• A 32-bit system can access 232 (4,294,967,296) memory
addresses. Practically speaking, a 32-bit computer is
limited to accessing 4,294,967,296 bytes (4 GB) of RAM
.
• A 64-bit processor can access 264memory addresses.
This means a 64-bit machine could theoretically access
18,446,744,073,709,551,616 memory addresses or
more than 18 billion GB of RAM.
ALGORITHMS AND FLOWCHARTS

• A typical programming task can be divided into two


phases:

• Problem solving phase


– produce an ordered sequence of steps that describe
solution of problem
– this sequence of steps is called an algorithm

• Implementation phase
– implement the program in some programming language
Steps in Problem Solving
• First produce a general algorithm (one can use
pseudo code)

• Refine the algorithm successively to get step by step


detailed algorithm that is very close to a computer
language.

• Pseudo code is an artificial and informal language


that helps programmers develop algorithms. Pseudo
code is very similar to everyday English.
Pseudocode & Algorithm
• Example 1: Write an algorithm to determine a
student’s final grade and indicate whether
he/she is passing or failing. The final grade is
calculated as the average of four marks.
Pseudocode & Algorithm
Pseudo code:

• Input a set of 4 marks


• Calculate their average by summing and dividing by 4
• if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
• Detailed Algorithm

Step 1: Input M1,M2,M3,M4


Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
• A schematic representation of a
sequence of operations, as in a
manufacturing process or computer
program.
The Flowchart
A Flowchart

– shows logic of an algorithm


– emphasizes individual steps and their
interconnections
– e.g. control flow from one action to the next
Flowchart Symbols
Basic
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Example 1
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N Y
IS
GRADE<50

PRINT PRINT
“PASS” “FAIL”

STOP
Example 2
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.

Pseudo code:
• Input the length in feet (ft)
• Calculate the length in cm (cm) by multiplying
ft with 30
• Print length in cm (cm)
Algorithm Flowchart

• Step 1: Input Lft START

• Step 2: Lcm  Lft x 30 Input


Lft

• Step 3: Print Lcm


Lcm  Lft x 30

Print
Lcm

STOP
Complier
• A compiler is a program that accepts a source
program in a “high-level language “and
produces a corresponding object program.
Assembler
• Input to an assembler is an assembly language
program.
• Output is an object program plus information
that enables the loader to prepare the object
program for execution.
Loader
• A Loader is a routine that loads an object
program and prepares it for execution.
• Loader is a program that places programs into
memory and prepares them for execution. The
loader places into memory the machine
language version of the user’s program and
transfers control to it.
First Program
#include<stdio.h>
/*first program*/
int main()
{
printf(“Hello-CP”);
return 0;
}

77
Second Program
#include<stdio.h>
/*first program*/
int main()
{
int a;
a=4+7;
printf(“value=%d”,a);
return 0;
}
78

You might also like