Unit I Ip
Unit I Ip
History of Computers, Basic organization of a computer: ALU, input-output units, memory, program counter,
Introduction to Programming Languages, Basics of a Computer Program Algorithms, flowcharts (Using Dia
Tool), pseudo code. Introduction to Compilation and Execution, Primitive Data Types, Variables, and Constants,
Basic Input and Output, Operators, Type Conversion, and Casting.
Problem solving techniques: Algorithmic approach, characteristics of algorithm, Problem solving strategies:
Top-down approach, Bottom-up approach, Time and space complexities of algorithms.
History of Computers
What is a Computer?
Definition: Computer is an advanced electronic device that takes raw data as an input from the user and processes
it under the control of a set of instructions (called program), produces a result (output), and saves it for future
use
Earliest Computer
• Originally calculations were computed by humans, whose job title was computers.
• These human computers were typically engaged in the calculation of a mathematical expression.
• The calculations of this period were specialized and expensive, requiring years of training in
mathematics.
• The first use of the word "computer" was recorded in 1613, referring to a person who carried out
calculations, or computations, and the word continued to be used in that sense until the middle of the
20th century.
Tally Sticks
A tally stick was an ancient memory aid device to record and document numbers, quantities, or even messages.
Abacus
• An abacus is a mechanical device used to aid an individual in performing mathematical calculations.
• The abacus was invented in Babylonia in 2400 B.C.
• The abacus in the form we are most familiar with was first used in China in around 500 B.C.
• It used to perform basic arithmetic operations.
Napier’s Bones
• Invented by John Napier in 1614.
• Allowed the operator to multiply, divide and calculate square and cube roots by moving the rods around
and placing them in specially constructed boards.
Slide Rule
• Invented by William Oughtred in 1622.
• Is based on Napier's ideas about logarithms.
• Used primarily for
– multiplication
– division
– roots
– logarithms
– Trigonometry
• Not normally used for addition or subtraction.
Pascaline
• Invented by Blaise Pascal in 1642.
• It was its limitation to addition and subtraction.
• It is too expensive.
Stepped Reckoner
• Invented by Gottfried Wilhelm Leibniz in 1672.
• The machine that can add, subtract, multiply and divide automatically.
Jacquard Loom
• The Jacquard loom is a mechanical loom, invented by Joseph-Marie Jacquard in 1881.
• It an automatic loom controlled by punched cards.
Arithmometer
• A mechanical calculator invented by Thomas de Colmar in 1820.
• The first reliable, useful and commercially successful calculating machine.
• The machine could perform the four basic mathematic functions.
• The first mass-produced calculating machine.
Havard Mark 1
• Also known as IBM Automatic Sequence Controlled Calculator (ASCC).
• Invented by Howard H. Aiken in 1943 and it is the first electro-mechanical computer.
Z1
• The first programmable computer and Created by Konrad Zuse in Germany from 1936 to 1938.
• To program the Z1 required that the user insert punch tape into a punch tape reader and all output was
also generated through punch tape.
UNIVAC 1
• The UNIVAC I (UNIVersal Automatic Computer 1) was the first commercial computer.
• Designed by J. Presper Eckert and John Mauchly.
EDVAC
• EDVAC stands for Electronic Discrete Variable Automatic Computer
• The First Stored Program Computer • Designed by Von Neumann in 1952.
• It has a memory to hold both a stored program as well as data.
Computer Generations
There are five generations of computer:
First generation – 1946 – 1958
Second generation – 1959 – 1964
Third generation – 1965 – 1970
Fourth generation – 1971 – today
Fifth generation – Today to future
• Assembly languages were invented to allow machine level/processor operations to be expressed with
mnemonic abbreviations
For example, to add two numbers, you might write an instruction in assembly code like this:
ADDF3 R1, R2, R3
• A program called assembler is used to convert assembly language programs into machine code
• Assemblers were eventually augmented with elaborate “macro expansion” facilities to permit
programmers to define parameterized abbreviations for common sequences of instructions.
• Problem: each different kind of computer had to be programmed in its own assembly language
• People began to wish for machine-independent languages. These wishes led in the mid-1950s to the
development of standard higher-level languages compiled for different architectures by compilers
Today there are thousands of high-level programming languages, and new ones continue to emerge
Most Popular Programming Languages –
• C
• Python
• C++
• Java
• SCALA
• C#
• R
• Ruby
• Go
• Swift
• JavaScript
Classifications of Programming Languages
Structure of a C Program
The structure of a C program can be mainly divided into six parts, each having its purpose. It makes the program
easy to read, easy to modify, easy to document, and makes it consistent in format.
Section Description
Consists of the description of the program, programmer's name,
Documentation and creation date. These are generally written in the form of
comments.
All header files are included in this section which contains
Link different functions from the libraries. A copy of these header
files is inserted into your code before compilation.
Includes preprocessor directive, which contains symbolic
Definition constants. E.g.: #define allows us to use constants in our code.
It replaces all the constants with its value in the code.
Global Includes declaration of global variables, function declarations,
Declaration static global variables, and functions.
For every C program, the execution starts from
Main()
the main() function. It is mandatory to include
Function
a main() function in every C program.
Includes all user-defined functions (functions the user
provides). They can contain the inbuilt functions and the
Subprograms
function definitions declared in the Global Declaration section.
These are called in the main() function.
Let's look at an example to understand the structure of a C program:
Example: Write a program to calculate our age.
/** //Documentation
* file: age.c
* author: you
* description: program to find our age.
*/
#include <stdio.h> //Link
#define BORN 2000 //Definition
int age(int current); //Global Declaration
void main() //Main() Function
{
int current = 2021;
printf("Age: %d", age(current));
return 0;
}
int age(int current)
{ //Subprograms
return current - BORN;
}
Output
Age: 21
Let's explore the code: Different sections of the above code
Documentation
In a C program, single-line comments can be written using two forward slashes i.e., //, and we can
create multiline comments using /* */. Here, we've used multi-line comments.
/**
* file: age.c
* author: you
* description: program to find our age.
*/
Link
All header files are included in this section. A header file is a file that consists of C declarations that can be used
between different files. It helps us in using others' code in our files. A copy of these header files is inserted into
your code before compilation.
#include <stdio.h>
Definition
A preprocessor directive in C is any statement that begins with the "#" symbol. The #define is a preprocessor
compiler directive used to create constants. In simple terms, #define basically allows the macro definition, which
allows the use of constants in our code.
#define BORN 2000
We've created a constant BORN which is assigned a value of 2000. Generally, uppercase letters
are preferred for defining the constants. The above constant BORN will be replaced by 2000 throughout our
code wherever used. #define is typically used to make a source program easy to modify and compile in different
execution environments. The define statement does not end with a semicolon.
Global Declaration
This section includes all global variables, function declarations, and static variables. The variables declared in
this section can be used anywhere in the program. They're accessible to all the functions
of the program. Hence, they are called global variables.
int age(int current);
We've declared our age function, which takes one integer argument and returns an integer.
Main() Function
In the structure of a C program, this section contains the main function of the code. The compiler starts execution
from the main() function. It can use global variables, static variables, inbuilt functions, and user-defined
functions. The return type of the main() function can be void and also not necessarily int.
int main(void)
{
int current = 2022;
printf("Age: %d", age(current));
return 0;
}
Here, we've declared a variable named current and assigned the value as 2022. Then we've called the printf()
function, with calls the age() function, which takes only one parameter.
Subprograms
This includes the user-defined functions called in the main() function. User-defined functions are
Generally written after the main() function irrespective of their order. When the user-defined function is called
from the main() function, the control of the program shifts to the called function, and when it encounters a return
statement, it returns to the main() function. In this case, we've defined the age() function, which takes one
parameter, i.e., the current year.
int age(int current)
{
return current - BORN;
}
This function is called in the main function. It returns an integer to the main function
Conclusion
• To conclude, the structure of a C program can be divided into six sections, namely –
Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms.
• The main() function is compulsory to include in every C program, whereas the rest are optional.
• A well-structured C program makes debugging easier and increases the readability and modularity of the
code.
Elements of C Programming Language
As every language has some basic geometrical rules and elements, similarly C language has some elements and
rules for building a program which has some meaning.
Character Set:
In Real world to communicate with people we use language like Hindi English Urdu extra which is constructed
and Defined by some characters, words extra. Similarly in C programming language we have various characters
to communicate with the computer in order to produce a meaningful program and can produce an output.
Character Set in C Language:
Keywords:
• They are those elements of C language whose meaning has already being defined or explained.
• Keyword are called pre-defined words.
32 keywords in C language
What is algorithm?
An algorithm is a procedure or step-by-step instruction for solving a problem. They form the foundation of
writing a program.
For writing any programs, the following has to be known:
• Input
• Tasks to be performed
• Output expected
Flowchart:
Flowchart is a diagrammatic representation of sequence of logical steps of a program. Flowcharts use
simple geometric shapes to depict processes and arrows to show relationships and process/data flow.
Flowchart Symbols:
Here is a chart for some of the common symbols used in drawing flowcharts.
Guidelines for Developing Flowcharts:
These are some points to keep in mind while developing a flowchart –
• Flowchart can have only one start and one stop symbol
• On-page connectors are referenced using numbers
• Off-page connectors are referenced using alphabets
• General flow of processes is top to bottom or left to right
• Arrows should not cross each other
Here is a flowchart to calculate the average of two numbers:
Flow chart for prime numbers:
Pseudocode
Pseudocode is an artificial and informal language that helps programmers develop algorithms.
Definition: Pseudocode is an informal way of programming description that does not require any strict
programming language syntax or underlying technology considerations.
void main()
{
int a=10;
clrscr();
printf("Value of a=%d",a);
getch();
}
Expected Output
Value of a=10
Constants: Symbolic name or an entity that does not change its value during the execution of a program.
There are two major categories of a constant:
Constants are classified into two types they are:
1. Primary Constant
2. Secondary Constant
Primary Constant: This category consist basic constants of C language like integer constant, float or real integer
constant, character constant.
Secondary Constant: This category consist arrays, pointers, structure, union, enum etc.
a) Integer Constant:
const int rate= 50;
In this const is a keyword, int is a data type, rate is a value , = is a assignment operator, 50 is a value, ; is a
terminate operator to terminate the statement. In this value of variable rate will not be changed throughout the
program.
b) const float or real integer:
const float pi= 3.1415;---> this is called the declaration with complete statement
c) Character Constant:
const char ch= 'A'; a character data type value is always assigned with “quotes.
Variables
“A variable is the name given to memory location to store data value in it.”
Unlike constant, the value of a variable can be changed.
A variable name must be chosen in such a way that it improves the readability of a program.
Syntax Data_type var_name=var_value;
Example
int x=10;
The above variable ‘x’ contains value 10 and it is of integer data type
C Programming Operators
An operator is a symbol that operates on a value or a variable
For example: + is an operator to perform addition.
C has a wide range of operators to perform various operations.
Arithmetic Operators:
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division
etc on numerical values (constants and variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* Multiplication
/ division
% remainder after division (modulo division)
Example 1: Arithmetic Operators
// Working of arithmetic
operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+
b = %d
\n",c); c =
a-b;
printf("a-
b = %d
\n",c); c
= a*b;
Outputprintf("a*
b = %d
a+b = 13
\n",c);
a-b = 5c =
a/b;
a*b = 36
printf("a/
a/b = 2
b = %d
Remainder when a divided by b=1
\n",c); c
= a%b;and Decrement Operators:
Increment
printf("Remainder when a divided by b = %d \n",c);
C programming
return 0; has two operators increment ++ and decrement -- to change the value of an operand
}
(constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement-- decreases the value by 1. These two operators
are
unary operators, meaning they only operate on a single operand.
Example 2: Increment and Decrement Operators
++a = 11
--b = 99
++c = 11.500000
Here, the operators ++ and -- are used as prefixes. These two operators can also be used as
postfixes like a++ and a--
Assignment Operators:
An assignment operator is used for assigning a value to a variable. The most common assignment operator
is =
Operator Example
= a=b
+= a+=b or a=a + b
-= a -=b or a=a - b
*= a*=b or a=a * b
/= a/=b or a=a / b
%= a%=b or a=a % b
Example 3: Assignment Operators:
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
Relational Operators:
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation
is false, it returns value 0.
Normally Relational operators are used in decision making and loops.
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Bitwise Operators:
• During computation, mathematical operations like: addition, subtraction, multiplication, division, etc
are converted to bit-level which makes processing faster and saves power.
• Bitwise operators are used in C programming to perform bit-level operations.
^ Bitwise exclusive OR
~ Bitwise complement
Special Operators:
Comma Operator:
Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
The sizeof operator:
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).
Example 6: sizeof Operator
#include <stdio.h>
int main()
{
int a; float b;
double c; char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Ternary operator
It is also known as condition operator.
Syntax exp1?exp2:exp3;
The value of exp1 is evaluated first, If it is true, value of exp2 is evaluated otherwise exp3 is evaluated
Example Program
Write a C program to find the biggest of two numbers using Conditional operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,big;
clrscr();
a=20;
b=10;
big=(a>b)?a:b;
printf("Big number is %d",big);
getch();
}
Expected Output
Big number is 20
Type Casting: In typing casting, a data type is converted into another data type by the programmer using
the casting operator during the program design. In typing casting, the destination data type may be smaller
than the source data type when converting the data type to another data type, that’s why it is also called
narrowing conversion.
Syntax/Declaration:-
destination_datatype = (target_datatype)variable;
(): is a casting operator.
target_datatype: is a data type in which we want to convert the source data type.
Type Casting example –
float x;
byte y;
...
...
y=(byte)x; //Line 5
In Line 5: you can see that, we are converting float(source) data type into byte(target) data type.
Type conversion: In type conversion, a data type is automatically converted into another data type by a
compiler at the compiler time. In type conversion, the destination data type cannot be smaller than the source
data type, that’s why it is also called widening conversion. One more important thing is that it can only be
applied to compatible data types.
Type Conversion example –
int x=30;
float y;
y=x; // y==30.000000.
In type casting, a data type is converted Whereas in type conversion, a data type is
1. into another data type by a programmer converted into another data type by a
using casting operator. compiler.
Type casting takes place during the Whereas type conversion is done at the
5.
program design by programmer. compile time.
Type casting is also called narrowing Whereas type conversion is also called
conversion because in this, the destination widening conversion because in this, the
6.
data type may be smaller than the source destination data type can not be smaller than
data type. the source data type.
In this approach, the problem is broken In this approach, the smaller problems are
1.
down into smaller parts. solved.