[go: up one dir, main page]

0% found this document useful (0 votes)
58 views35 pages

Unit 1 SGT

Uploaded by

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

Unit 1 SGT

Uploaded by

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

1SF04

Programming for Problem Solving

Prof. S. G. Taley
Syllabus: 1SF04 Programming for Problem
Solving (L-2, C-2)
Unit I Introduction (Hours:5)
Characteristics of Computer, Organization of Computer, Input–Process Output Cycle
Various Types of Memory (Primary Memory & Secondary Memory), Introduction to
C language, Basic structure.

Unit II C Fundamentals (Hours:5)


Program Execution First C Program, Input Output using scanf() and printf(), Data
type, Variables, Keywords Operators and their precedence.

Unit III C Control construct (Hours:5)


Decision making using if, if-else,if-else-if and switch. Looping using for, while, do
while, Jumps Statements (break, continue, goto, return), Basic Programs.

Unit IV Array and Strings (Hours:5)


Introduction to Array, 1-D array: Declaration & Initialization, 2-D array: Declaration
& Initialization, Strings: Declaration & Initialization, String functions.
Unit V Functions (Hours:5)
Function: Pre defined functions; User defined functions, Multi-
function Program, Elements of user defined functions, Return value
and their types, Function calls, Function Declaration.

Unit VI Pointers (Hours:5)


Definition and uses of pointers, pointer declaration, pointer
assignment, pointer arithmetic, Pointers and Functions: Call by
value and call by reference.

Text Book:
1) R. S. Salaria : Programming for Problem Solving , Khanna
Publication 2) E Balagurusamy: Computing Fundamentals and C
Programming- Tata McGraw Hill, Second Edition
Reference Books:
1. Herbert Schildt- C Complete reference (Tata McGraw Hill)
2. Yashwant Kanetkar- Let us C- Seventh Edition
Unit I Introduction

Characteristics of Computer, Organization of


Computer, Input–Process Output Cycle
Various Types of Memory (Primary Memory
& Secondary Memory), Introduction to C
language, Basic structure.
Introduction
• A computer is an electronic machine that takes input from the user, processes the
given input and generates output in the form of useful information. A computer
accepts input in different forms such as data, programs and user reply. Data refer
to the raw details that need to be processed to generate some useful information.
Programs refer to the set of instructions that can be executed by the computer in
sequential or non-sequential manner. User reply is the input provided by the user
in response to a question asked by the computer. The main task of a computer
system is to process the given input of any type in an efficient manner. Therefore,
computer is also known by various other names such as data processing unit, data
processor and data processing system.
• A computer includes various devices that function as an integrated system to
perform several tasks described above. These devices are:
• Central Processing Unit (CPU) It is the processor of the computer that is
responsible for controlling and executing instructions in the computer. It is
considered as the most signifi cant component of the computer. It is the “brain” of
the computer.
• Monitor It is a screen, which displays information in visual form, after receiving the
video signals from the computer.
• Keyboard and Mouse These are the devices, which are used by the computer, for
receiving input from the user.
• Figure 1.1 shows the various components of a computer.
• The unique capabilities and characteristics of a computer
have made it very popular among its various users,
including engineers, managers, accountants, teachers,
students, etc.
The characteristics and capabilities of a modern digital computer include,
among others:
• Speed A computer is a fast electronic device that can solve large and
complex problems in few seconds. The speed of a computer generally
depends upon its hardware configuration.
• Storage capacity A computer can store huge amount of data in its different
storage components in many different formats. The storage area of a
computer system is generally divided into two categories, main memory
and secondary storage.
• Accuracy A computer carries out calculations with great accuracy. The
accuracy achieved by a computer depends upon its hardware configuration
and the instructions.
• Reliability A computer produces results with no error. Most of the errors
generated in the computer are human errors that are created by the user
itself. Therefore, they are very trustworthy machines.
• Versatility Computers are versatile machines. They can perform many
different tasks and can be used for many different purposes.
• Diligence Computers can perform repetitive calculations any number of
times with the same accuracy. Computers do not suffer from human traits,
such as tiredness, fatigue, lack of concentration, etc.
Basic Architecture of Computer
• Over the several years the size, processing speed,
cost and reliability of computers has changed.
• But, the basic structure of computer is still the
same as was given by Von Neumann in the year
1945 has not changed. According to Von
Neumann architecture of computer every digital
computer with these components:
1. Input Unit
2. Output Unit
3. Storage Unit
4. Arithmetic Logic Unit
5. Control Unit
Input Unit
Input unit connects the external environment with internal
computer system. It provides data and instructions to the
computer system. Commonly used input devices are keyboard,
mouse, magnetic tape, scanner, webcam, joystick etc.
Input unit performs following tasks:
• Accept the data and instructions from the outside environment.
• Convert it into machine language.
• Supply the converted data to computer system.

Output Unit
• It connects the internal system of a computer to the external
environment. It provides the results of any computation, or
instructions to the outside world. Some output devices are
printers, monitor, plotter, speaker etc.
Storage Unit
This unit holds the data and instructions. It also stores the
intermediate results before these are sent to the output devices. It
also stores the data for later use.

The storage unit of a computer system can be divided into two


categories:
Primary Storage: This memory is used to store the data which is
being currently executed. It is used for temporary storage of data.
The data is lost, when the computer is switched off. RAM and ROM
are used as primary storage memory.
Secondary Storage: The secondary memory is slower and cheaper
than primary memory. It is used for permanent storage of data.
Commonly used secondary memory devices are hard disk, CD etc.
CPU (Central Processing Unit)
It is Central Processing Unit of the computer. The control unit
and ALU are together known as CPU. CPU is the brain of
computer system. It performs following tasks:
• It performs all operations.
• It takes all decisions.
• It controls all the units of computer

Arithmetic Logical Unit


All the calculations are performed in ALU of the computer
system. The ALU can perform basic operations such as
addition, subtraction, division, multiplication etc. Whenever
calculations are required, the control unit transfers the data
from storage unit to ALU. When the operations are done, the
result is transferred back to the storage unit.
Control Unit
It controls all other units of the computer. It controls the
flow of data and instructions to and from the storage unit
to ALU. Thus it is also known as central nervous system of
the computer.

Memory Units
Computer store data in binary format, binary format
contain only two characters 0 and 1. All the information
stored/represented in the computer is a code which
contain a specific combination of 0’s and 1’s. The memory
units are used to measure the size of data in computer. The
memory units are as follow.
The Input-Process-Output Cycle of a
computer
• The most elementary computing concepts include receiving input—
known as data— from the user, manipulating the input according
to the given set of instructions and delivering the output—known
as information—to the user. Figure 1.12 shows the functioning of a
computer based on these concepts. The various functions
performed by the computer are briefly described below:
• Accepting the raw data The first task to be performed by a
computer is to accept the data from the user, with the help of an
input device, such as mouse and keyboard. Mouse is used to enter
the data through point-and-click operation while keyboard is used
to enter the character data by typing the various keys. We need to
enter the data into the computer so as to obtain the required result
as output.
• Processing the data The data is processed with the help of
specific instructions known as programs after taking the
input from the user. The manipulation of data is handled by
the CPU of the computer. CPU is considered as the brain of
the computer because it controls the execution of various
instructions. The raw data entered by the user through
input devices is processed by the CPU to generate
meaningful information.
• Storing the data The data is stored in the main memory of
a computer in its processed form. The various external
storage devices—such as hard disk and magnetic disk—can
also be used for storing the processed data so that it can
again be fetched later.
• Delivering the output The processed data is delivered as
useful information to the user with the help of output
devices, such as printer and monitor.
Computer Memory
A computer system needs memory to store the data and instructions
for processing. Whenever we talk about the ‘memory’ of a computer
system, we usually talk about the main or primary memory. The
secondary memory (also called storage device) is used to store data,
instructions and results permanently for future use.

Types of Memory
Human beings memorise many things over a lifetime, and recall from
memory to make a decision or some action. However, we do not rely
on our memory completely, and we make notes and store important
data and information using other media, such as notebook, manual,
journal, document, etc. Similarly, computers have two types of
memory
— primary and secondary.
(A) Primary Memory
Primary memory is an essential component of a computer system.
Program and data are loaded into the primary memory before processing.
The CPU interacts directly with the primary memory to perform read or
write operation. It is of two types viz. (i) Random Access Memory
(RAM) and (ii) Read Only Memory (ROM).
RAM is volatile, i.e., as long as the power is supplied to the computer, it
retains the data in it. But as soon as the power supply is turned off, all the
contents of RAM are wiped out. It is used to store data temporarily while
the computer is working. Whenever the computer is started or a software
application is launched, the required program and data are loaded into
RAM for processing. RAM is usually referred to as main memory and it
is faster than the secondary memory or storage devices.
On the other hand, ROM is non-volatile, which means its contents are
not lost even when the power is turned off. It is used as a small but faster
permanent storage for the contents which are rarely changed. For
example, the startup program (boot loader) that loads the operating
system into primary memory, is stored in ROM.
B) Cache Memory

RAM is faster than secondary storage, but not as fast as a computer


processor. So, because of RAM, a CPU may have to slow down. To
speed up the operations of the CPU, a very high speed memory is
placed between the CPU and the primary memory known as cache.
It stores the copies of the data from frequently accessed primary
memory locations, thus, reducing the average time required to
access data from primary memory. When the CPU needs some
data, it first examines the cache. In case the requirement is met, it
is read from the cache, otherwise the primary memory is accessed.
(C) Secondary Memory
Primary memory has limited storage capacity and is
either volatile (RAM) or read-only (ROM). Thus, a
computer system needs auxiliary or secondary memory
to permanently store the data or instructions for future
use. The secondary memory is non-volatile and has
larger storage capacity than primary memory. It is
slower and cheaper than the main memory. But, it
cannot be accessed directly by the CPU. Contents of
secondary storage need to be first brought into the
main memory for the CPU to access. Examples of
secondary memory devices include Hard Disk Drive
(HDD), CD/ DVD, Memory Card, etc., as shown in
Figure 1.7.
However, these days, there are secondary storage
devices like SSD which support very fast data transfer
speed as compared to earlier HDDs. Also, data transfer
between computers have become easier and simple due
to the availability of small-sized and portable flash or
pen drives.
Algorithm
• In our day-to-day life we perform activities by following
certain sequence of steps. Examples of activities include
getting ready for school, making breakfast, riding a
bicycle, wearing a tie, solving a puzzle and so on. To
complete each activity, we follow a sequence of steps.
• Suppose following are the steps required for an activity
‘riding a bicycle’:
1) remove the bicycle from the stand,
2) sit on the seat of the bicycle,
3) start peddling,
4) use breaks whenever needed and
5) stop on reaching the destination.
Some Examples of algorithms:
Example 1 : Write an algorithm to print „Good Morning‟.
Step 1: Start
Step 2: Print „Good Morning‟
Step 3: Stop

Example 2 : Write an algorithm to find area of a rectangle.


Step 1: Start
Step 2: Take length and breadth and store them as L and B?
Step 3: Multiply by L and B and store it in area
Step 4: Print area
Step 5: Stop
Flowchart — Visual Representation of
Algorithms
A flowchart is a visual representation of an
algorithm. A flowchart is a diagram made up
of boxes, diamonds and other shapes,
connected by arrows. Each shape represents a
step of the solution process and the arrow
represents the order or link among the steps.
• There are standardised symbols to draw flowcharts. Some
are given in Table 4.1.
Flowchart symbol Function Description
Start/End Also called “Terminator” symbol. It indicates
where the flow starts and ends.

Process Also called “Action Symbol,” it represents a


process, action, or a single step.

Decision A decision or branching point, usually a


yes/no or true/ false question is asked, and
based on the answer, the path gets split into
two branches.

Input/Output Also called data symbol, this parallelogram


shape is used to input or output data

Arrow Connector to show order of flow between


shapes.
Write an algorithm to find the square of a number.
Before developing the algorithm, let us first identify the input,
process and output:
• Input: Number whose square is required
• Process: Multiply the number by itself to get
its square
• Output: Square of the number

Algorithm to find square of a number.


Step 1: Input a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Print square
Write an algorithm to display the sum of two numbers
entered by user, using both pseudocode and flowchart.
Pseudocode for the sum of two numbers will be:
input num1
input num2
COMPUTE Result = num1 + num2
PRINT Result
The flowchart for this algorithms is
given in Figure 4.4.
Introduction to C Language
• C is a programming language developed at AT & T‟s Bell
Laboratories of USA in 1972. It was designed and written
by Dennis Ritche. Dennis Ritchie is known as the founder
of c language.
• It was developed to overcome the problems of previous
languages such as B, BCPL etc.
• Initially, C language was developed to be used in UNIX
operating system.
• Features of C
1. Portability or machine independent
2. Sound and versatile language
3. Fast program execution.
4. An extendible language.
5. Tends to be a structured language.
Characteristic features of C Language
• Integrity
This refers to accuracy of calculation
• Clarity
This refers to overall readability of program, with particular emphasis on
its underlying logic.
• Simplicity
Keeping things as simple as possible, consistent with overall program
objectives usually enhances clarity and accuracy of a program.
• Efficiency
This is concerned with execution speed & efficient memory utilization.
• Modularity
Many programs can be broken down into series of identifiable subtasks. It
is good programming practice to implement each of these subtasks as a
separate program module.
• Generality
Usually we want program to be as general as possible, within reasonable
limits.
• General Structure of a C program:
Documentation section
Link section
Definition section
Global declaration section
Function prototype declaration section
main()
{
Declaration part
Executable part (statements)
}
User defined function definition section

• The documentation section is used for displaying any information


about the program like the purpose of the program, name of the
author, date and time written etc, and this section should be
enclosed within comment lines. The statements in the
documentation section are ignored by the compiler.
• The link section consists of the inclusion of header files.
• The definition section consists of macro definitions,
defining constants, variables are defined & values are set to
these, etc,.
• Anything declared in the global declaration section is
accessible throughout the program, i.e. accessible to all the
functions in the program.
• Function prototype gives many information about function
like return type, parameter names used inside function.
• main() function is mandatory for any program and it
includes two parts, the declaration part and the executable
part.
• User can define their own functions in User defined
function definition section which perform particular task as
per user requirement.
First C Program

Before starting the abcd of C language, you need to learn how to


write, compile and run the first c program.

To write the first c program, open the C console and write the
following code:

#include <stdio.h>
#include <conio.h>
void main(){
printf("Hello C Language");
getch();
}
• #include <stdio.h>
includes the standard input output library functions. The
printf() function is defined in stdio.h .
• #include <conio.h>
includes the console input output library functions. The
getch() function is defined in conio.h file.
• void main()
The main() function is the entry point of every program
in c language. The void keyword specifies that it returns no
value.
• printf()
The printf() function is used to print data on the console.
• getch()
The getch() function asks for a single character. Until you
press any key, it blocks the screen.

You might also like