Programming Course Material
Programming Course Material
1
Outline
4
Cont. …
5
Cont. …
6
Cont. …
7
• Levels of Programming
8
Levels of Programming
9
Levels of Programming
11
High level programming
12
Programming paradigms
13
Procedural Programming Languages
• Procedural programming
specifies a list of operations that
the program must complete to
reach the desired state.
14
Procedural Programming Languages
• Procedural programming
specifies a list of operations that
the program must complete to
reach the desired state.
15
Procedural Programming Languages
16
Object-oriented Programming Languages
• Object-oriented programming
is one the newest and most
powerful paradigms.
• Every thing represents in the
form of class and object.
17
Object-oriented Programming Languages
18
Object-oriented Programming Languages
OOP principles/concepts
19
Problem Solving
20
Problem Solving
21
Problem Solving
23
Flowchart Cont. ….
24
Flowchart Cont. ….
25
Flowchart Cont. ….
Algorithm description
Example 1: - Draw flow ▪ Read the rules of the two numbers (A and
chart of an algorithm to add B)
two numbers and display ▪ Add A and B
their result. ▪ Assign the sum of A and B to C
▪ Display the result ( c)
26
Flowchart Cont. ….
27
Example 2: Write an algorithm description and draw a flow chart to check a number is negative
or not.
Algorithm description.
1/ Read a number x
2/ If x is less than zero write a message negative
else write a message not negative
28
Write the algorithmic description and draw a flow chart to find the following sum.
Sum = 1+2+3+…. + 50
Algorithmic description
1. Initialize sum too and counter to 1
1.1. If the counter is less than or equal to 50
29
30
Flowchart Exercise
Home Work for next class
1. What is an algorithm?
2. Explain need of an algorithm?
3. Explain steps involve in drawing of a flowchart.
Draw a flowchart for the following
1. Write an algorithm to find average age of a group of 10 players?
2. Receive 3 numbers and display them in ascending order from smallest to largest
3. Add the numbers from 1 to 100 and display the sum
4. Add the even numbers between 0 and any positive integer number given by the user.
5. Find the average of two numbers given by the user. 31
1. Find the average, maximum, minimum, and sum of three numbers given by the user.
2. Find the area of a circle where the radius is provided by the user.
3. Swap the contents of two variables using a third variable.
4. Swap the content of two variables without using a third variable.
5. Read an integer value from the keyboard and display a message indicating if this number is odd or even.
33
Pseudocode Example
• For example: Write a program that obtains two integer numbers from the user. It will
print out the sum of those numbers.
Pseudocodes
• Prompt the user to enter the first integer
• Prompt the user to enter a second integer
• Compute the sum of the two user inputs
• Display an output prompt that explains the
answer as the sum
• Display the result
34
Program Development Life Cycle
1. Requirement Analysis
2. Design/Planning
3. Implementation or testing
4. Testing and debugging
5. Deploying and Maintaining the Program
35
36
Next Class
▪ Assignment Operators
▪ Arithmetic Operators
▪ Relational Operators
▪ Logical Operators
▪ Increment/decrement Operators
38
Structure of C++ Program
▪ [Comments]
▪ [Preprocessor directives]
▪ [Global variable declarations]
▪ [Prototypes of functions]
▪ [Definitions of functions]
39
Showing Sample program
43
Keywords (reserved words)
44
Comments
45
Data Types, Variables, and Constants
• A variable is a symbolic name for a memory location in which data can be stored and
subsequently recalled.
46
Variable Declaration
47
Variable Declaration
48
Basic Data Types
• When you define a variable in C++, you must tell the compiler what kind of
variable it is.
• Basic C++ data types, int, float, double, short, long, char
49
Operators
51
Arithmetic Operators
52
Relational Operators
53
Logical Operators
54
Increment/decrement Operators
• Introduction
• Conditional Statements
• If Statement
• If-else statement
• If-else-if statement
• Switch statement 58
Introduction
59
Cont.… ..
60
If Statement
• Syntax
• if (expression)
statement;
• First expression is evaluated. If the outcome is nonzero (true) then
statement is executed. Otherwise, nothing happens.
• Example: Display the number if it is greater than 10
61
If-else statement
satisfied
62
If-else statement
67
Switch Statement
68
Questions
Content Outline
• For Loop
• While Loop
• Do-while Loop
• Jumping Statement
71
• For Loop
74
While Loop
75
Do…while Statement
77
Continue Statement
• A break statement may appear inside a loop (while, do, or for) or a switch
statement. It causes a jump out of these constructs, and hence terminates them
• Like the continue statement, a break statement only applies to the loop or
switch immediately enclosing it. It is an error to use the break statement
outside a loop or a switch
79
Exercise Question (do it three types of loop)
80
81
Chapter Four: Array and String
▪ Declaration of Arrays
▪ Accessing Array Elements
▪ Initialization of arrays 82
Cont. …
▪ C++ String
▪ String object and C-String
▪ String input and output
83
Introduction
86
What is array
87
Declaration of Arrays
90
Accessing Array Elements
92
Initialization of arrays
93
Multidimensional arrays
Syntax:
datatype arrayName[size][size];
For example
int num[3][2];
• Arrays can have any number of dimensions, although most of the arrays that you
create will likely be of one or two dimensions.
95
Array Exercise
• Create an array that can hold ten integers, and get input from user. Display those
values on the screen and count the result.
• Write a C++ program to find the next greater element of every element of a given
array of integers. Ignore those elements which have no greater element.
• Write a C++ program to find the most occurring element in an array of integers.
• Write a C++ program to find the largest element of a given array of integers.
• Write a C++ program to find the largest three elements in an array.
• Write a C++ program to find the second smallest elements in a given array of
integers.
• Write a C++ program to find a number which occurs odd number of times of a
given array of positive integers.
96
C++ String
98
C++ String to read a line of text
99
C++ String to read a line of text
#include <iostream>
using namespace std;
int main() {
char str[100];
cout << "Enter a string: ";
[Link](str, 100);
cout << "You entered: " << str << endl; return
0;
}
100
C++ String to read a line of text
• To read the text containing blank space, [Link] function can be used. This function
takes two arguments.
• First argument is the name of the string (address of first element of string) and
second argument is the maximum size of the array.
• In the above program, str is the name of the string and 100 is the maximum size of
the array.
101
string Object
• Unlike using char arrays, string objects has no fixed length, and can be
extended as per your requirement.
• It needs to include string library
• C++ String to read a word
• C++ String to read a line of text
102
string Object
#include <iostream>
#include <string>
using namespace std;
int main() {
// Declaring a string object
string str;
cout << "Enter a string: ";
getline(cin, str);
cout << "You entered: " << str << endl; return 0;
} 103
104
Chapter Five: Function
Content Outline
• Introduction to Function
• Types of function
• Parts of function
• User Defined Function
• Standard Function
105
Chapter Five: Function
Content Outline
• Function declaration
• Function return type
• Function parameter
• Function calling
• Function Argument
106
Introduction to Function
• A function is a block of code that • You can divide up your code into separate
performs a specific task. functions
• Every C++ program has at least • How you divide up your code among
one function, which is main(), different functions is up to you,
• The most trivial programs can • Logically the division usually is such that
define additional functions. each function performs a specific task.
107
C++ User-defined Function
108
Parts of function
109
Parts of function
110
Parts of function
111
Function Declaration
} 112
//
Function Declaration
• Example
function declaration
void greet() {
cout << "Hello World";
}
113
//
Function Declaration
114
//
Function Calling
115
//
Function Calling
Function Calling
117
//
Function Calling
118
Function Arguments
119
Function Parameters
}
• Here, the int variable num is the function parameter 120
Function Parameters
121
Function Parameters
122
Function Parameters
123
Return Statement
124
Return Statement
• It's also possible to return a value from a function. For this, we need to
specify the returnType of the function during function declaration.
• Then, the return statement can be used to return a value from a function.
125
Benefits of Using User-Defined Functions
• Functions make the code reusable. We can declare them once and use them
multiple times.
• Functions make the program easier as each small task is divided into a function.
• Functions increase readability.
126
C++ Library Functions
• For instance, in order to use mathematical functions such as sqrt() and abs(), we
need to include the header file cmath.
128
C++ Library Functions
129
Question
• Write a program that ask for two numbers, compare them and show the maximum. Declare a
function called max_two that compares the numbers and returns the maximum.
• Write a program that asks the user for an integer number and find the sum of all natural numbers
upto that number (create function sum).
• Write C++ program to check even or odd using functions
• Write C++ program to find cube of a number using function
• Write C++ program to find sum of natural numbers in given range using recursion
• Write C++ program to find power of a number using recursion
130
Question
131
132