Computer Programming and Numerical Methods
Computer Programming and Numerical Methods
NUMERICAL METHODS
What is a Program?
A set of Instructions to perform a task
• A computer can perform what ever is being told to be done but within its
hardware limits.
• A computer is nothing in itself without programs
All the tasks are performed through programs
• Programs can be classified in to two- 1. System Programs
- 2. Application Programs
System Programs: The programs that manage the hardware resources of the
computer.
E.g.: Operating system (Windows, Linux, Unix, MAC OS, DOS
etc….), Anti Virus (Kaspersky, BitDefender, Avira, AVG etc…)
• Application Programs: An application program is any program designed to
perform a specific function directly for the user or, in some cases, for another
application program
E.g.: Notepad, MS Word, Calculator, Browser, Image editing programs etc…
Binary
• A computer can only understand binary (0,1) and at lowest level, any
communication has to happen in binary
• A binary is also called a bit.
• 1 is called a set bit & 0 is called an unset bit
• With one bit, there are only two possible values 0 & 1
• With 2 bits, there are 4 possible values : 0 0
• With 3 bits 0 0 0 0 1
8 possible values: 0 0 1 1 0
0 1 0 1 1
1 0 0
0 1 1 • In general, for n bits , 2𝑛 possible combinations of 0 & 1
1 0 1 • The values can vary from 0 to 2𝑛 -1
1 1 0
1 1 1
FLOWCHART SYMBOLS
PROCEDURE ORIENTED PROGRAMMING (POP)
• Conventional programming using High Level Language such as COBOL, FORTRAN, C etc.
• The problem is viewed as a sequence of a things to be done
• Large number of Functions up on which attention is concentrated (Functions are instructions
organized in groups)
• The technique of hierarchical decomposition is used (More emphasis is ion algorithm)
• The functions can have their local data
• Global data is shared by all functions
• Very little attention given to data
• Data is vulnerable to inadvertent change
• Difficult to model real world problems
• Employs top down approach in program design
OBJECT ORIENTED PROGRAMMING (OOP)
• To take care of the flaws encountered in procedure oriented programming
• Here data is treated as critical elements
• Data is not allowed flow freely around the system
• Data is tied closely to the function that operates on it
• Data is hidden and cannot be accessed by external functions
• The problem is decomposed in to a number of entities – objects
• Data & functions are built around the objects
• Objects can communicate through functions
• New data and functions can easily be added whenever necessary
• Follows bottom-up approach in program design
• E.g.: C++, Java etc.
Introduction to C++
• An Object Oriented Programming Language
• Developed by Bjarne Stroustrup at AT & T bell Laboratories
• Extension of C with a major addition
• Initially named : C with Classes
• Standardized by ANSI/ISO standards committee during 1997
• Applications:
Can handle very large problems
Special object oriented libraries can be built
Able to map real world problems
Programs are easily maintainable and expandable
Structure of a C++ Program
Preprocessor directives
Function prototypes
Global variables
main()
{
Local variable declaration
Statements & Function calls
}
Function definitions
Tokens
• Tokens are the smallest individual units in a program
• They include:
• Keywords
• Identifiers
• Constants
• Strings
• Operators
Keywords
• The word that is reserved by a program because it has a special meaning
• They are explicitly reserved identifiers and cannot be used as names of programming variables
• Eg. If, else, int, switch, for, do, while bool, char, float, long, double etc.
Identifiers
• They refer to the name of the variables, functions, arrays, classes ets created by the programmer
• Rules for naming identifiers:
• Only alphabets, digits & underscores
• Name cannot start with a digit
• Uppercase & Lowercase are distinct
• A declared Keyword cannot be used as an identifier
Standard Identifiers
• Not a part of C++ language (not a reserved word)
• It is a part of C++ standard library but can be used as a variable name
• E.g: cout, cin, open, close, sin, cos, log ,min, max etc.
Constants
• C++ supports five basic types of constants-
integer, boolean, floating point, character and string constant
• The constant modifier: To declare variables which are not permitted to change their initial values
• Eg.: const int k=100;
Variables
• An identifier used to represent a single data item
• The value of a variable may change with in the program
Data Types
• The variables used in C++ are to be declared in prior what data it contains
• Different Data Types in C++ :
• int : can hold integer values
• float: can hold large numbers/decimals
• char: used to hold ASCII characters like a, A, * etc
• double: same purpose of float but with a higher magnitude and precission
• void: used along with functions that donot return any value
• bool: can hold either true or false
• Examples:
int a,b,sum;
float x,y;
double root;
char A,B;
Data Type Qualifiers
• Also called as data type modifiers
• E.g.: const, unsigned, signed, long , short etc. applied upon int
• long applied to float (long float is same as double)
Operators
• There are different categories:
Arithmetic Operators : + - * / %
Increment and decrement Operators: ++ --
Assignment Operators: += -= *= /= %=
Logical Operators: && II !
Relational Operators: == != > < >= <=
Conditional Operators/Ternary Operator: Expression ? True: False;
Statements
• It causes computer to carry out some action
• Three different types:
Expression statements : An expression followed by a semicolon
Compound statements : Several individual statements enclosed with in
curly braces {}
Control statements: To alter natural flow of control in a program
Library Functions
• Used to carryout commonly used operations/calculations
• They are not part of the C++ language
• Users can also add library functions
• E.g.: exp(2.5) , log(25) , pow(a,b) , cout<<
Header File
• Header file provides definitions for standard library functions
• Included in program by using the pre-processor #include statement
• E.g.: #include<iostream> : several standard stream objects
#include<cmath> : Common Mathematical functions
Input/Output Functions
• C++ do not have directly defined i/o statements
• Implemented by functions developed and included in standard library
• E.g.: cin, cout included along with iostream
Q. Write an algorithm and draw a flow chart to find the largest among three numbers
Q. Write a program and draw a flowchart to find the
average of n given numbers
Practice problems
Write an algorithm and draw a flow chart to calculate the factorial of a number N.
Write an algorithm to find the roots of a quadratic equation