C++ best
C++ best
1
Outline
• Programming paradigms
• Flow Chart, Pseudocode, Algorithm
• Program Development Life Cycle 2
What is computer programming
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
Flowchart Exercise
• Draw a flowchart to input two numbers from user and display the largest of two
numbers
• Draw a flowchart that take one input number and check the number is even or odd.
• Draw a flowchart that take one input number and display the square of the number
• Draw a flowchart that take the temperature in Fahrenheit, calculate and display the
the celisus .
• Flowchart for the calculate the sum of 1 up to 100.
• Draw a flowchart to find the largest of three numbers x, y and z
• Write an algorithm to find average age of a group of 10 players?
28
Pseudocode
29
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
30
Program Development Life Cycle
1. Requirement Analysis
2. Design/Planning
3. Implementation or testing
4. Testing and debugging
5. Deploying and Maintaining the Program
31
32
Next Class
34
Structure of C++ Program
[Comments]
[Preprocessor directives]
[Global variable declarations]
[Prototypes of functions]
[Definitions of functions]
35
Showing Sample program
39
Keywords (reserved words)
40
Comments
41
Data Types, Variables, and Constants
• A variable is a symbolic name for a memory location in which data can be stored and
subsequently recalled.
• All variables have two important attributes.
• A type, which is, established when the variable is defined (e.g., integer, float,
character).
• A value, which can be changed by assigning a new value to the variable. The kind of
values a variable can assume depends on its type.
42
Variable Declaration
43
Variable Declaration
44
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
45
Operators
47
Arithmetic Operators
48
Relational Operators
49
Logical Operators
50
Increment/decrement Operators
• Introduction
• Conditional Statements
• If Statement
• If-else statement
• If-else-if statement
• Switch statement 54
Introduction
55
Cont.… ..
56
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
57
If-else statement
satisfied 58
If-else statement
63
Switch Statement
64
Questions
Content Outline
• For Loop
• While Loop
• Do-while Loop
• Jumping Statement
67
• For Loop
70
While Loop
71
Do…while Statement
73
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
75
Exercise Question (do it three types of loop)
76
77
Chapter Four: Array and String
Declaration of Arrays
Accessing Array Elements
Initialization of arrays 78
Cont. …
C++ String
String object and C-String
String input and output
79
Introduction
83
Declaration of Arrays
86
Accessing Array Elements
89
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.
91
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.
92
C++ String
94
C++ String to read a line of text
95
C++ String to read a line of text
#include <iostream>
using namespace std;
int main() {
char str[100];
cout << "Enter a string: ";
cin.get(str, 100);
cout << "You entered: " << str << endl; return
0;
}
96
C++ String to read a line of text
• To read the text containing blank space, cin.get 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.
97
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
98
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;
} 99
100
Chapter Five: Function
Content Outline
• Introduction to Function
• Types of function
• Parts of function
• User Defined Function
• Standard Function
101
Chapter Five: Function
Content Outline
• Function declaration
• Function return type
• Function parameter
• Function calling
• Function Argument
102
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.
103
C++ User-defined Function
104
Parts of function
105
Parts of function
106
Parts of function
107
Function Declaration
Function Declaration
• Example
function declaration
void greet() {
cout << "Hello World";
}
109
//
Function Declaration
110
//
Function Calling
111
//
Function Calling
Function Calling
113
//
Function Calling
114
Function Arguments
}
116
• Here, the int variable num is the function parameter
Function Parameters
117
Function Parameters
118
Function Parameters
119
Return Statement
120
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.
121
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.
122
C++ Library Functions
• For instance, in order to use mathematical functions such as sqrt() and abs(), we
need to include the header file cmath.
124
C++ Library Functions
125
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 126
Question
127
128