Problem Solving Using C
Problem Solving Using C
Text Books:
Reference Books:
3.Brian W. Kernighan and Dennis M. Ritchie, The C Programming
Language, Prentice Hall of India.
UNIT-1
Topics to be covered in
UNIT-1
Introduction to Programming:
Idea of Algorithm:
A computer is an electronic
device or programmable
machine. It allows the user to
store all sorts of information
and then process that
information or carry out
actions with the information
such as calculating numbers
or organizing words.
Computer Systems
A computer system consists of hardware and software.
Hardware refers to any physical, electrical components of the
computer.
For example keyboard, mouse, cabinet of computer is considered as
hardware.
Software refers to a program or set of instructions that is written to
achieve a specified task.
Components of a Computer System:
Input Devices
Central Processing Unit (CPU)
Arithmetic and Logic Unit (ALU)
Control Unit
Storage Devices
Output Devices
Input Devices:
The input device is used to enter data into a computer.
The devices like keyboard, mouse and scanner are commonly
used input devices.
Central Processing Unit:
It is the main part of the computer.
It’s main function is to execute programs stored in the main
memory.
It consists of three functional units: ALU, CU and MU.
Arithmetic and Logic Unit (ALU):
It performs arithmetic and logical operations on the data.
Control Unit:
It controls the overall activities of the components of the
computer.
Memory Unit:
It is used to hold the waiting data to be processed.
Memory: There are two types of memories.
• Volatile Memory
• Non-volatile Memory
Cache Memory:
It is a memory placed between CPU and main memory.
It is faster compared to the primary memory.
Parts of the program or data that need to be accessed repeatedly are
stored in cache memory.
It is a volatile memory.
Registers:
Registers are small memory units internally available within the
processor.
It is a volatile memory.
Output Devices:
It is used to display or print the result.
Monitor, printer and plotter are commonly used output devices.
Operating System:
You can write a C program in text editor and save that file on to the
disk with “.c” extension. This file is called source file.
2. Compiling Program
Compiler is used to convert High Level Language instructions
into the Machine Language instructions.
It could complete its task in two steps.
i) Preprocessor
ii) Translator
Preprocessor:
It reads the source file and checks for special commands
known as preprocessor commands ( instructions which starts
with # symbols ).
The result of preprocessor is called as translation unit.
Preprocessor processes the source file before compilation only.
Translator:
It is a program which reads the translation unit and converts
the program into machine language and gives the object
module.
This module is not yet ready to run because it does not have
the required C and other functions included.
3. Linking a program with required library
functions
C program is made up of different functions in which some
functions can be written by the programmer, other functions like
input/output functions and mathematical library functions, that
exist elsewhere and must be attached to our program.
3. Compiler gives errors and warnings if any, then edit the source
file, fix it, and re-compile.
Start
Accept a, b
c=a+b
Display C
End
Algorithm to find whether a given year is a leap year or not.
Algorithm:
BEGIN
Step 1: Accept the YEAR
Step 2: if (YEAR %4 == 0) then
Display “Year is a leap year”
else
Display “Year is not a leap year”
end if
END
Flowchart to find whether a given year is a leap year or not
Start
Accept year
false
if(year%4 = = 0)
true
Display non leap year
End
Exercise:
1. Sum of the digits of a number.
2. Nature of quadratic equations.
3. Income tax problem.
4. Standard deviation.
Identifiers
Identifiers are names given to various programming elements such
as variables, constants, and functions.
It should start with an alphabet or underscore, followed by the
combinations of alphabets and digits.
No special character is allowed except underscore.
An Identifier can be of arbitrarily long. Some implementation of C
recognizes only the first 8 characters and some other recognize first
32 Characters.
Percent Legal
y2x5__fg7h Legal
Variable initialization:
datatype identifier = initial value;
Examples:
int a=10;
float b=2.1;
float pi=3.14;
char ch=‘A’;
Variables (contd…)
When you want to process some information, you can save the values
temporarily in variables.
#include<stdio.h>
main()
{
int a=10,b=20,c;
c=a+b;
printf(“sum of a and b=%d\n”,c);
return 0;
}
Case matters (that is, upper and lowercase letters). Thus, the names
count and Count refer to two different identifiers.
Data types in C
Data types are used to indicate the type of value represented or
stored in a variable, the number of bytes to be reserved in memory,
the range of values that can be represented in memory, and the
type of operation that can be performed on a particular data value.
Floating-point Types
Type Modifiers:
The basic data types may have various modifiers (or qualifiers)
preceding them, except type ‘void’.
A modifier is used to alter the meaning of the base data type to fit
the needs of various situations more precisely.
They are:
Arrays
Pointers
Structures
Unions
Enumeration
Note: Number of bytes and range given to each data type is platform
dependent.
Data types in C
size smallest largest format
type precision constant
(byte) number number specifier
1.2345L,
long double 10 3.4E-4932 1.1E+4932 1.08E-19 %Lf, %Le, %Lg
1.234E-5L
1.2345,
double 8 1.7E-308 1.7E+308 2.22E-16 %lf, %le, %lg
1.234E-5
1.2345F,
float 4 1.7E-38 3.4E+38 1.19E-7 %f, %e, %g
1.234E-5F
Types of Errors:
1. Syntax Errors
2. Logical Errors
Syntax Errors:
A syntax error is an error in the source code of a program.
Examples:
Spelling mistakes.
Missing out quotes.
Missing out brackets.
Using upper case characters in key words e.g. IF instead of if.
Missing out a colon or semicolon at end of a statement.
Logic errors:
A logic error (or logical error) is a ‘bug’ or mistake in a program’s
source code that results in incorrect or unexpected behaviour.
It is a type of runtime error that may simply produce the wrong output
or may cause a program to crash while running.
Example: