Application of Residue Theorem to evaluate real integrations.pptx
C Programming for Problem Solving Guide
1. C Programming for Problem
Solving
Dr. Anuradha T
Department of Computer Science and
Engineering
PDA College of Engineering, Kalaburagi.
07-06-2019 1
2. Contents
• Generations of Computers
• Computer types
• Bits, bytes and words
• CPU
• Primary and Secondary memory
• Ports and Connection
• Input devices and Output Devices
• Computers in a network
• Network Hardware , Software basics
• Software types
• Basic Structure of C Programming
• Executing a C Programming
• Constant , Variables and Data Types
• Operators and Expressions
• Managing Input and Output operations
• Conditional branching and Loops
07-06-2019 2
3. Generations of Computers
• First Generation (1940-1956): Vaccum tubes as
circuitry and magnetic drums for memory, very big in
size they were taking entire room, produces more heat
like bulbs.
• Relied on ‘machine language’ (which is the most
basic programming language that can be understood
by computers). These computers were limited to
solving one problem at a time.
• Input was based on punched cards and paper tape.
Output came out on print-outs.
• The two notable machines of this era were the
UNIVAC and ENIAC machines
07-06-2019 3
4. • Second Generation(1956–1963): The replacement of
vacuum tubes by transistors saw the advent of the second
generation of computing.
• They were a big improvement over the vacuum tube,
despite still subjecting computers to damaging levels of
heat.
• They were hugely superior to the vacuum tubes, making
computers smaller, faster, cheaper and less heavy on
electricity use.
• They still relied on punched card for input/printouts.
• The language evolved from cryptic binary language to
symbolic (‘assembly’) languages.
• This meant programmers could create instructions in
words. About the same time high level programming
languages were being developed (early versions of
COBOL and FORTRAN). :
07-06-2019 4
5. • Third Generation(1964 – 1971): Integrated Circuits
• Transistors were now being miniaturised and put on silicon
chips (called semiconductors).
• This led to a massive increase in speed and efficiency of these
machines.
• These were the first computers where users interacted using
keyboards and monitors which interfaced with an operating
system.
• This enabled these machines to run several applications at
once using a central program which functioned to monitor
memory.
• As a result of these advances which again made machines
cheaper and smaller.
07-06-2019 5
6. Fourth Generation(1972 – 2010 ) : Microprocessors
• The chip-maker developed the Intel 4004 chip in 1971, which
positioned all computer components (CPU, memory,
input/output controls) onto a single.
• The Intel chip housed thousands of integrated circuits. The
year 1981 saw the first ever computer (IBM) specifically
designed for home use and 1984 saw the MacIntosh
introduced by Apple.
• The increased power of these small computers meant they
could be linked, creating networks.
• Which ultimately led to the development, birth and rapid
evolution of the Internet. During this period the Graphical user
interface (GUI), the mouse and lap-top capability and hand-
held devices were introduced.
07-06-2019 6
7. Fifth Generation(2010 to till dated) – Artificial Intelligence
• Computer devices with artificial intelligence are still in
development, but some of these technologies are beginning to
emerge and be used such as voice recognition.
• AI is a reality made possible by using parallel processing and
superconductors.
• The essence of fifth generation will be using these
technologies to ultimately create machines which can process
and respond to natural language, and have capability to learn
and organise themselves.
07-06-2019 7
8. What is a Computer
• It is a machine which accepts the data, process them
and output results.
• A computer is an electronic machine and understands
1‘s and0’s represents bits i.e binary digits
07-06-2019 8
CPU Output deviceInput Device
Main
Memory
Storage
Peripherals
Fig. 1. Block diagram of Computer
9. Computer types
• Computers can be generally classified by size and power as
follows:
• Workstation: A powerful, single-user computer, a workstation
is like a personal computer, but it has a more powerful
microprocessor and, in general, a higher-quality monitor.
• Minicomputer: A multi-user computer capable of supporting
up to hundreds of users simultaneously.
• Mainframe: A powerful multi-user computer capable of
supporting many hundreds or thousands of users
simultaneously.
• Supercomputer: An extremely fast computer that can perform
hundreds of millions of instructions per second.
07-06-2019 9
10. Bits, Bytes and Words
• Bit
• Byte=8bits
• Word=2bytes=16bits
07-06-2019 10
11. • Bit (Abbreviated as Binary Digit) is defined as a
smallest unit of data on a binary computer or digital
system. It can hold either 0 or 1.
• Bytes is defined as a group of 8 bits is called a byte.
Bits in a byte are represented from 0 to 7.
• Bit 0 is the low order bit or least significant bit.
• Bit 7 is the high order bit or most significant bit.
• Word is a group of 16 bits, bits in a word are numbered
starting from 0 to 15.
07-06-2019 11
13. • The CPU, Buses, Controllers, and Main Memory.
Other sections will examine input devices, output
devices, and secondary memory.
• The computer does its primary work in a part of the
machine we cannot see, a control center that converts
data input to information output called the central
processing unit (CPU), is a highly complex, extensive
set of electronic circuitry that executes stored
program instructions.
• Central processing unit (CPU): The heart of the
computer, this is the component that actually executes
instructions organized in programs ("software")
which tell the computer what to do.
07-06-2019 13
14. • Before an instruction can be executed, program instructions
and data must be placed into memory from an input device or
a secondary storage device.
• The central processing unit performs the following four steps
for each instruction:
• The control unit fetches (gets) the instruction from memory.
• The control unit decodes the instruction (decides what it
means) and directs that the necessary data be moved from
memory to the arithmetic/logic unit. These first two steps
together are called instruction time.
• The arithmetic/logic unit executes the arithmetic or logical
instruction. That is, the ALU is given control and performs the
actual operation on the data.
• Thc arithmetic/logic unit stores the result of this operation in
memory or in a register. Steps 3 and 4 together are called
execution time
07-06-2019 14
15. Primary Memory
• Memory is the brain of the computer which stores
data and information for storing and retrieving.
• Just like a human brain, memory is the storage space
of the computer – like a physical device – that is
capable of storing data or programs temporarily or
permanently.
• Memory is a fundamental component of the computer
that is categorized into primary and secondary
memory.
• Primary memory is the main memory of the computer
which can be directly accessed by the central
processing unit.
•07-06-2019 15
16. • Primary memory, also known as the main memory, is
the area in a computer which stores data and
information for fast access.
• Semiconductor chips are the principle technology used
for primary memory.
• It’s a memory which is used to store frequently used
programs which can be directly accessed by the
processing unit for further processing.
• It’s a volatile memory meaning the data is stored
temporarily and is liable to change or lose in case of
power failure.
• Every application on the computer first loads into the
random access memory (RAM) which makes is faster
to access. The term is more ambiguous, since it also
refers to internal memory such as internal storage
devices.
07-06-2019 16
17. Secondary memory
• Secondary memory refers to the external storage device
which can be used to store data or information permanently.
• It’s a non-volatile memory which means data stays even if
the computer is turned off.
• Data cannot be directly processed by the processing unit in
secondary memory; in fact, it is first transferred into the
main memory and then it’s transferred back to the
processing unit.
• Secondary memory refers to all external storage devices
that are capable of storing high volumes of data such as
hard drives, floppy disks, magnetic tapes, USB flash drives,
CDs, DVDs, etc.
• It’s generally slower than primary memory but can store
substantial amount of data, in the range of gigabytes to
terabytes.
07-06-2019 17
18. Ports and Connections
• Port refers to a hardware port or peripheral port is a
hole or connection found on the front or back of a
computer. a port serves as an interface between the
computer and other computers or peripheral devices.
• In computer terms, a port generally refers to the part
of a computing device available for connection to
peripherals such as input and output devices.
• Computer ports have many uses, to connect
a monitor, webcam, speakers, or other peripheral
devices. On the physical layer, a computer port is a
specialized outlet on a piece of equipment to which
a plug or cableconnects.
07-06-2019 18
19. • Electronically, hardware ports can almost always be
divided into two groups based on the signal transfer:
• Serial ports send and receive one bit at a time via a
single wire pair (Ground and +/-).
• Parallel ports send multiple bits at the same time over
several sets of wires.
• After ports are connected, they typically
require handshaking, where transfer type, transfer
rate, and other necessary information is shared before
data is sent.
07-06-2019 19
20. • Electronically, the several conductors where the port
and cable contacts connect, provide a method to
transfer signals between devices.
• Bent pins are easier to replace on a cable than on a
connector attached to a computer, so it was common
to use connectors for the fixed side of an interface.
07-06-2019 20
21. Input Devices
• An input device sends information to a computer
system for processing, and an output
device reproduces or displays the results of that
processing.
• Input devices only allow for input of data to a
computer and output devices only receive the output
of data from another device.
• Most devices are only input devices or output
devices, as they can only accept data input from a
user or output data generated by a computer.
However, some devices can accept input and display
output, and they are referred to as I/O devices
(input/output devices).
07-06-2019 21
22. • Keyboard and Mouse- Accepts input from a
user and sends that data (input) to the
computer. They cannot accept or reproduce
information (output) from the computer.
• Microphone - Receives sound generated by an
input source, and sends that sound to a
computer.
• Webcam - Receives images generated by
whatever it is pointed at (input) and sends
those images to a compute
07-06-2019 22
23. 07-06-2019 23
Fig.3 Input and Output examples
For example, as you can see in the top half of the image, a keyboard sends
electrical signals, which are received by the computer as input.
Those signals are then interpreted by the computer and displayed, or output, on
the monitor as text or images.
In the lower half of the image, the computer sends, or outputs, data to a printer,
which will print the data onto a piece of paper, also considered output.
24. Output Devices
• An output device is any peripheral that receives data
from a computer, usually for display, projection, or
physical reproduction.
• Monitors and printers are two of the most common
output devices used with a computer.
• Every computer has a monitor or display screen, a sound
card (or on-board sound on the motherboard), and a
video card (or on-board video on the motherboard),
which are all output devices.
• A printer is also very commonly used with computers.
Depending on the type of computer and how the
computer is used, other output devices may be used with
a computer.
07-06-2019 24
26. Computer in Networks
• Two or more computers or communication devices
connected by transmission media and channels and
follow set of rules for communication purposes that
allow users to communicate with each other and share
application
07-06-2019 26
27. Types of Networks
• LAN: Local are network
• WAN: Wide area network
• MAN: Metropolitian network
• Depending on one’s perspective we can classify
networks in different ways based on transmission
media, based on size, based on management
method(peer-to-peer and client-server), based on
topology(bus,Star and Ring)
07-06-2019 27
28. Network Hardware
• Network hardware components or devices like Network interface
card (NIC), hub, switch, routers and gateways.
• Hub is simple and cheap but wastes bandwidth, center of star
network, all nodes receive transmitted packets from hub leads to
more congestion in traffic.
• slow and insecure because all nodes will know the information of all
nodes.
• Switch: replacement of hubs, only the intended nodes receive
transmission. Fast and secure as they use MAC address of each
computer, less congestion.
• Routers: connects two or more MANs together, packets sent to
remote LANs together
• Network is segmented by IP address, connect internal network to the
internet, need to before installation.
• Bridge: connects two or more LANs together, remote packets cross
the LANs whereas other nodes wont cross LANs.
• Gateway: is a node or router is a computer network a key stopping
point for data in its way or from other network, Connets two
dissimilar network.
07-06-2019 28
30. • A program is a set of computer instructions that
perform a particular task.
• That program can be written in assembler, a very low
level computer language, or in a high level, machine
independent language such as the C programming
language.
• An operating system is a special program which
allows the user to run applications such as
spreadsheets and word processors
07-06-2019 30
31. Computer language
Assembly Languages
• The instructions that a CPU fetches from memory and
executes are not at all understandable to human beings. They
are machine codes which tell the computer precisely what to
do.
The C Programming Language and Compiler
• Writing large programs in assembly language is a difficult and
time consuming task.
• It is far better to use a machine independent language like C.
• C allows you to describe programs in terms of their logical
algorithms and the data that they operate on.
• Special programs called compilers read the C program and
translate it into assembly language, generating machine
specific code from it.
07-06-2019 31
32. Linkers
• Linkers are programs that link together several object modules
and libraries to form a single, coherent, program.
• Object modules are the machine code output from an
assembler or compiler and contain executable machine code
and data together with information that allows the linker to
combine the modules together to form a program.
• For example one module might contain all of a program's
database functions and another module its command line
argument handling functions.
07-06-2019 32
33. Software Types
• System software and Application software
• System software: It is defined as a collection of programs
that controls the overall operations and internal working
of the computer system.
• Two types of system software are:
• Operating system: controls the overall activities of a
computer
• Utility programs: are the program that assist in the
smooth functioning of a computer. Help to avoid viirus
attack
• Application software: Set of programs which are used to
perform specific types of jobs like performing calculation,
arranging data in an organized way.
07-06-2019 33
34. Managing input and output operations
• Reading a character
• Writing a character
• Formatted input
• Formatted output
07-06-2019 34
35. • Reading a character:
• Reading a single character from standard input using
getchar function.
• Syntax for variable_name=getchar();
name=getchar();
• Writing a character:
• Writing a single character to the standard output using
putchar function.
• Syntax putchar (variable_name);
answer=‘Y’;
putchar (answer);
07-06-2019 35
36. • Formatted Input: refers to an input data that
has been arranged in a particular format.
• Example: consider the following data
5.72 123 john
• This line contains three pieces of data, first is
read as float, second as int, third as char.
• Reading the formatted data using scanf
function.
scanf(“control string”,arg1,arg2,...argn);
Control string specifies field format in which data has
to be entered and arguments specify the address of
locations where data is stored.07-06-2019 36
37. Formatted output
• printf statement is used for formatted output to print
captions and numerical results on the terminal.
• printf(“control string”, arg1,arg2,…argn);
• Control strings specifies how many args follow their
types are
07-06-2019 37
38. Loops
• while loop: is an entry controlled loop statement. The test condition
is evaluated and if the condition is true, then the body of the loop is
executed.
• while(test condition)
{
body of the loop
}
• do loop: the program proceeds to evaluate the body of the loop first.
At the end of the loop the test condition in the while statement is
evaluated.
• do
{
body of the loop
}
• While(test-condition)
07-06-2019 38
39. • for loop: is another entry-controlled loop that
provides a more concise loop control structure.
The general form is
for( initialization; test-condition; increment)
{
body of the loop
}
Example: for (x=0; x<=9; x++)
07-06-2019 39
40. Branching
• If statement is a decision making statement and is
used to control the flow of execution of statements.
syntax: if(test expression)
{
statement-block;
}
statement-x;
07-06-2019 40
Test
expression
Fig. 5 Two way branching
Entry
True
False
41. If statement
07-06-2019 41
Fig.5. Flowchart for if statement
Example:
If(category= =Sports)
{
marks=marks+bonus_marks;
}
printf(“%d”, marks);
43. Nested if else statement
07-06-2019 43Fig. 7.Flowchart for netsted if else statement
44. Syntax for nested if else statement
if(condition)
{
//Nested if else inside the body of "if" if(condition2)
{
//Statements inside the body of nested "if“
}
else
{
//Statements inside the body of nested "else"
}
}
else
{
//Statements inside the body of "else“
}
07-06-2019 44
45. #include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2n");
}
else {
printf("var2 is greater than var1n");
}
}
else
{ printf("var1 is equal to var2n");
07-06-2019 45
Example
46. Switch statement
switch (variable or an integer expression)
{
case constant:
//C Statements ;
break;
case constant:
//C Statements ;
default:
//C Statements ;
break;
}
07-06-2019 46
47. Example
#include <stdio.h>
int main()
{
int month;
switch(month)
printf(“ enter the value of months from 1-12”);
Scanf(“%d”, &month);
{
case 1:
printf(" January month );
break; //transfers the control out of switch statement
case 2:
printf(“Feb month”,);
break;
case 3:
printf(“March month");
Break;
default: // when no match is found
printf("Default: value not month);
}
return 0;
}
07-06-2019 47
48. Memory
C(High level language)
07-06-2019 48
CPU
ROM
Seconddary
memory
RAM
C- Compiler
Machine
Language
Save the
program
Memory
Disk
During
execution
it goes
from the
secondary
to RAM
CPU reads
from RAM
and
executes the
pgm
Fig. 8. flow of execution of program and how memory is used
49. C Programming
• C is one of the programming and basic language.
• C is a robust language whose rich set of built-in
functions and operators can be used to write any
complex program.
• Important feature of C its ability to extend itself.
• Highly portable, means that C program can be run on
another with little or no moification
07-06-2019 49
50. Basic Structure of C Programming
Sample program #1
#include<stdio.h> // default thing, stdio means standard io
whenever we get instructions with
respect to keyboard and anything to be
printed on the screen
main() //main function is executed when you run the program
{
printf(“n hello worldn”) //printing the statements that are
within “ ”
“n denotes end of line or new line
}
07-06-2019 50
51. Sample C Program #2
#include<stdio.h>
main()
{
int a,b,c; //type declaration
a=10;
b=20;
c=a+b;
printf(“n the sum of %d and %d is %dn”,a,b,c); //this
statement is equivalent to the sum of ____and _____is ___
} %d=10 %d = 20 %d=30
07-06-2019 51
52. Constant, Variable and DataTypes
• Variable ‘a’ is of type integer a
correct wrong
• Variable ‘b’ is of type float b
correct wrong
07-06-2019 52
20 10.5
20.5 30
53. Executing a C Program
• Creating a Program
• Compiling the Program
• Linking the Program with functions that are needed
from c library and
• Executing the program
07-06-2019 53
54. Constants
• Constants in C refer to fixed values that do not
change during the execution of a program
07-06-2019 54
Constants
String
Constants
Character
constants
Numeric Constant
Integer constant
Real
Constants
Single
character
Constants
Fig.9. Types of C Constants
Eg: heights,
temp and
prices
Eg: decimal, octal
and hexadecimal
Eg: ‘5’,’X’ ,‘ ‘ Eg:“Hello”,”1987”
55. Variables
• Variable is a data name that may be used to store a
data value.
• Unlike the constant that remains unchanged during
execution of program.
• Variable may take different values at different times
during execution of a program.
• They must begin with a letter
• Uppercase and lower case letters are significant.
• It should not be a keyword.
• Declaring a variable means it tells the compiler what
the variable name is and it specifies what type of data
the variable will hold.
07-06-2019 55
56. Data Types
C supports three types of data types:
• Primary data types
• Derived data types
• User-defined data types such as Arrays, functions,
structures and pointers.
• All C compilers support five fundamental data types,
namely, integer (int)
• floating point(float)
• double-precision (double)
• void
07-06-2019 56
57. Operators and Expressions
• An operator is a symbol that tells the computer to perform certain
mathematical or logical manipulations. C operators can be classified into a
number of categories.
• Arithmetic operators: + , _, *, /, %
• Relational operators: <, <=, >, >=, = =, !=
• Logical operators: &&, ||, !,
• Assignment operators: ==,=
• Increment and decrement operators: ++, _ _
• Conditional operators and Bitwise operators: ?
07-06-2019 57
58. Expressions
• Arithmetic expressions
• a*b-c
• (m+n)*(x+Y)
• a*b/c
• x/y+c
• Evaluation of expressions using assignment statement
of the form: Variable=expression;
• X=a*b-c;
• Y=b/c*a;
07-06-2019 58
60. Finding the roots of Quadratic equation
• General form of quadratic equation is ax2 +bx+c=0
• Roots=-b±Sqrt(b2-4ac)/2a, where a,b,c are co-efficients.
• If a & b =0 then we get no roots, else if a,b,c are non
zero values. Imaginary roots
• d=b*b-4*a*c if d<0
• If d=0, (roots are equal) roots=-b+√d ,
root1=root2=-b/2a 2a
• If d>0 (distinct) root1=-b/2a, root2=√d/2a
• root1= -b+√d ,
2a root1+iroot2
• root2= = -b-√d ,
2a root1-iroot2
07-06-2019 60
61. Pascal Triangle
• Pascal triangle is a triangular array of binomial
coefficients.
• How to implement pascal triangle in c.
• Basically we will figureout number of spaces in each
row.
• Formula for no of spaces=number
i.e number of rows –nth row-1
07-06-2019 61