CSF101 Unit-01-1
CSF101 Unit-01-1
Solving
Session: 2023-24
PRIMARY MEMORY
(RAM, ROM etc…)
SECONDARY MEMORY
(Hard disk, Pen drive
etc…)
Block diagram of computer (Input Section)
• The devices used to enter data into computer system are
called input devices.
• It converts human understandable input to computer
controllable data.
• CPU accepts information from user through input devices.
• Examples: Mouse, Keyboard, Touch screen, Joystick etc…
Block diagram of computer (Output Section)
• The devices used to send the information to the outside world
from the computer is called output devices.
• It converts data stored in 1s and 0s in computer to human
understandable information.
• Examples: Monitor, Printer, Plotter, Speakers etc…
Block diagram of computer (Central Processing Unit
(CPU))
• It contains electronics circuit that processes the data based on instructions.
• It also controls the flow of data in the system.
• It is also known as brain of the computer.
• CPU consists of,
• Arithmetic Logic Unit (ALU)
• It performs all arithmetic calculations such as add, subtract, multiply, compare, etc. and takes
logical decision.
• It takes data from memory unit and returns data to memory unit, generally primary memory
(RAM).
• Control Unit (CU)
• It controls all other units in the computer system. It manages all operations such as reads
instruction and data from memory.
• Primary Memory
• It is also known as main memory.
• The processor or the CPU directly stores and retrieves information from it.
• Generally, currently executing programs and data are stored in primary memory.
Block diagram of computer (Secondary Memory)
Decimal Octal
Binary Hexadecimal
• Example
2510 = 110012 = 318 = 1916
Base
Decimal to Binary
• Technique
• Divide by two, keep track of the remainder
• The remainders read from bottom to top give the equivalent binary integer
number.
• Example - 1 Example - 2
12510 = ?2 2 12 1
5 0.687510 integer fraction
2 62 0 = ?2
2 31 1 0.6875 x 21.3750 1 + 0.3750
2 15 1 = x 20.7500
0.3750 0 + 0.7500
2 7 1 = x 21.5000
0.7500 1 + 0.5000
2 3 1 = x 21.0000
0.5000 1 + 0.0000
2 1 1 =
0 0.687510 0.10112
12510 =11111012 =
Binary to Decimal
• Technique
• Multiply each bit by 2n, where n is the “weight” of the bit
• The weight is the position of the bit, starting from 0 on the right. Finally, Add the results.
• Example - 1 Example -
1010112 2 2 = ?10
11.11
= ?10 1 0 1 0 1 1 1 . 1
1 1
8 12 5 = ?8 integer fraction
8 5
15 7
0.6875 x 85.5000 5 + 0.5000
8 1 1
= x 84.0000
0.5000 4 + 0.0000
0
=
Ax + Bx + Cx 4x + 3x + 2x + 5x
162 + 161 16 0
+ 12 x 161
160
16-1
16-2
10 x 11 x 64 + 3 + 0.125+ 0.019
162
2560 + 16
176
1 16
+ 12
0
5
Subroutine Arrows
Number is positive or negative
Step 1: Read no.
Start
Step 2: If no is greater than equal
zero, go to step 4.
Read no Step 3: Print no is a negative
number, go to step 5.
True False Step 4: Print no is a positive
Is no >= 0 number.
Step 5: Stop.
Print no is Print no is
Positive Negative
Stop
Number is odd or even
Step 1: Read no.
Start
Step 2: If no mod 2 = 0, go to
step 4.
Read no Step 3: Print no is a odd, go to
step 5.
True Is no % 2 = False Step 4: Print no is a even.
0 Step 5: Stop.
Print no is Print no is
Even Odd
Stop
Largest number from 2 numbers
Step 1: Read a, b.
Start
Step 2: If a>b, go to step 4.
Step 3: Print b is largest number,
Read a, b go to step 5.
Step 4: Print a is largest number.
True False Step 5: Stop.
Is a>b
Print a is Print b is
largest largest
Stop
Largest number from 3 numbers (Flowchart)
Start
Read a, b, c
True False
Is a>b
Stop
Largest number from 3 numbers (Algorithm)
Step 1: Read a, b, c.
Step 2: If a>b, go to step 5.
Step 3: If b>c, go to step 8.
Step 4: Print c is largest number, go to step 9.
Step 5: If a>c, go to step 7.
Step 6: Print c is largest number, go to step 9.
Step 7: Print a is largest number, go to step 9.
Step 8: Print b is largest number.
Step 9: Stop.
Print 1 to 10
Step 1: Initialize a to 1.
Start
Step 2: Print a.
Step 3: Repeat step 2 until
a=1 a<=10.
Step 3.1: a=a+1.
a=a+1 Print a Step 4: Stop.
True False
Is a<=10
Stop
Pseudo-code
• A program planning tool where program logic is written in
an ordinary natural language using a structure that
resembles computer instructions.
• “Pseudo” means imitation or false and “ Code” refers to the
instructions written in a programming language. Hence,
pseudocode is an imitation of actual computer instructions.
• Because it emphasizes the design of the program,
pseudocode is also called Program Design Language (PDL).
Basic Logic (Control) Structures
• Any program logic can be expressed by using only following
three simple logic structures:
1. Sequence logic,
2. Selection logic, and
3. Iteration (or looping) logic
• Identifiers: Identifiers are names that are given to various user defined program elements,
such as variable, function and arrays.
• Keywords: C reserves a set of 32 words for its own use. These words are called keywords (or
reserved words), and each of these keywords has a special meaning within the C language.
• Constants: Constants refer to fixed values that do not change during execution of program.
• Strings: A string is a sequence of characters terminated with a null character \0.
• Special Symbols: Symbols such as #, &, =, * are used in C for some specific function are
called as special symbols.
• Operators: An operator is a symbol that tells the compiler to perform certain mathematical
or logical operation.
Identifiers
• Identifier refers to name given to entities such as variables, functions,
structures etc. Identifiers must be unique. They are created to give a
unique name to an entity to identify it during the execution of the
program.
• Rules for naming identifiers:
A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
The first letter of an identifier should be either a letter or an
underscore.
You cannot use keywords as identifiers.
There is no rule on how long an identifier can be. However, you may
run into problems in some compilers if the identifier is longer than
31 characters.
• Examples: abc, _abc, abc123
Keywords
• Keywords are predefined, reserved words used in
programming that have special meanings to the compiler.
Keywords are part of the syntax, and they cannot be used as
an identifier. C having 32predefined keywords.
Variables and Constants
𝑥, 𝑦, 𝑎, 𝑐𝑜𝑢𝑛𝑡, 𝑒𝑡𝑐. can be variable names.
• Variable is a symbolic name given to some value which can be changed.
𝑥=5 𝑎=𝑏+𝑐
•
•
• A variable is a name given to memory location. Basically, a variable is
used to store some form of data. Different types of variables require
different amounts of memory and have some specific set of operations
which can be applied on them.
• A variable must be declared before use in the program.
• Syntax for declaring a variable in C: datatype variable_name;
datatype tells the type of variable and variable_name is a valid identifier
name.
• Example: int x; In this example x is a variable which is going to store
integer data type.
Header Description
file
stdio.h Input/Output functions (printf and scanf)
conio.h Console Input/Output functions (getch and clrscr)
math.h Mathematics functions (pow, exp, sqrt etc…)
string.h String functions (strlen, strcmp, strcat etc…)
Format Specifiers
• Format specifiers in C are used to take inputs
and print the output of a type.
• The symbol we use in every format specifier is %.
• Format specifiers tell the compiler about the type
of data that must be given or input and the type
of data that must be printed on the screen.
• Example:
#include <stdio.h>
int main()
{
int a=50;
printf("The integer value of a is %d \n",a);
return 0;
}
Escape Sequence in C
• The escape sequence in C is the characters or the sequence of characters that can be used inside the string
literal.
• The purpose of the escape sequence is to represent the characters that cannot be used normally using the
keyboard.
• Some escape sequence characters are the part of ASCII char set but some are not.
• Different escape sequences represent different characters, but the output is dependent on the compiler you are
using.
Escape Example:
Name Description
Sequence #include <stdio.h>
int main()
It moves the cursor {
\n New Line to the start of the
next line.
int a=50;
printf("The integer value of a is %d \n",a);
It inserts some return 0;
whitespace to the left }
\t Horizontal Tab of the cursor and
moves the cursor
accordingly.
Use to insert
\\ Backlash
backslash character.
Object and Executable Code
• It is important to understand that compilation and execution
are two distinct processes.
• During compilation, the computer runs the compiler
program.
• During execution, the object program is loaded into the
computer’s memory unit, replacing the compiler program.
• The computer then directly executes the object program,
doing whatever the program instructs it to do. See the
figure.
Types of Programming Errors
Programming errors are broadly classified into four categories:
1. Syntax error/Compile-time error: Errors that occur when you violate the rules of writing Programming
syntax are known as syntax errors. This compiler error indicates something that must be fixed before the code can
be compiled. All these errors are detected by compiler and thus are known as compile time errors.
4. Semantic Errors: This error occurs when the statements written in the program are not meaningful to the
compiler.
#include <stdio.h>
void main()
{
int a, b, c;
a * b = c;
}//When we have an expression on the left-hand side of an assignment operator (=), the program generates a
semantic error. Even though the code is syntactically correct, the compiler does not understand the code.
What about this?
#include <stdio.h>
void Main()
{
int var = 10;
printf("%d", var);
}