Programming Fundamentals
C++
Introduction
Agenda of 1st Week
What is Computer Programming?
Steps to evaluate and solve the problems using computer programming languages.
There are different types of computer system:
Personal Computer
Workstation
Minicomputer
Mainframe
Supercomputer
There are five basic components of the computer
1958: ALGOL
1959: COBOL
1964: BASIC
1964: BASIC
1972: C
Problem Introduction
Problem Statement: Modified Armstrong Number
Example
A number is considered to be modified Armstrong if it is equal to the sum of digits raised to the power of total number of digits in num and it doesn't end with 0. check if the given number is armstrong or not
For example :153
As 1 + 125 + 27 = 153
Analysis
I/O
Constraints
Flow Chart
Flow chart Symbols
Flow chart for Armstrong number
Pseudocode
Pseudocode - Armstrong Number
Algorithm
Algorithm of checking Armstrong number
What is C++?
C++ Programming Language
C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C language
C++ gives programmers a high level of control over system resources and memory.
Why use C++?
To start with C++ you need
C++ code example
Explaination
#include <iostream>
using namespace std
Line 3: A blank line. C++ ignores white space. But we use it to make the code more readable.
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function. Any code inside its curly brackets {} will be executed.
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to output/print text. In our example it will output "Hello World".
Cout and operator << together use for output
I/O in C++
Cin and operator >> together use for input
C++ Comments and new line
Variables
Data types
C++ Variables and Data Types
Type cast is basically a conversion from one data type to another. following Two types
Type Conversion (type cast)
The work of control structures is to give flow and logic to a program. There are three types of basic control structures in C++.
Control Structure
Sequence structure refers to the sequence in which programs execute instructions one after another. An example diagram for the sequence structure is shown in figure.
Sequence Structure
Selection Structure
Loop structure refers to the execution of an instruction in a loop until the condition gets false. An example diagram for loop structure is shown in figure.
Loop Structure
A function is a block of code that performs a specific task.
Suppose we need to create a program to create a circle and color it. We can create two functions to solve this problem:
Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.
There are two types of function:
Functions in C++
returnType functionName (parameter1,parameter2,...){
// function body
}
// for example
// function declaration
void greet() {
cout << "Hello World";
}
Function Declaration
Here,
greet()void{}In the previous program, we declared a function named greet(). To use the greet() function, we need to call it.
Here's how we can call the above greet() function.
Calling a function
int main() {
// calling a function
greet();
}
Function Parameters
void printNum(int num) {
cout << num;
}
int main() {
int n = 7;
// calling the function
// n is passed to the function as argument
printNum(n);
return 0;
}
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;}
Example: Function with Parameter
The int number is 5
The double number is 5.5
continued example:
returnType of the function during function declaration.return statement can be used to return a value from a function.Return Statement
// program to add two numbers using a function
#include <iostream>
using namespace std;
// declaring a function
int add(int a, int b) {
return (a + b);
}
int main() {
int sum;
// calling the function and storing
// the returned value in sum
sum = add(100, 78);
cout << "100 + 78 = " << sum << endl;
return 0;
}
double grade[27];
double type.
Arrays
int - type of element to be stored6 - size of the arrayDeclaration of Array
Initialization of an Array
// declare and initialize and array
int x[6] = {19, 10, 8, 17, 9, 15};
/ Another method to declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};
we have not mentioned the size of the array. In such cases, the compiler automatically computes the size.
Array with Empty members
// store only 3 elements in the array
int x[6] = {19, 10, 8};
6. However, we have initialized it with only 3 elements. In such cases, the compiler assigns random values to the remaining places. Oftentimes, this random value is simply 0.Accessing Individual Array Elements
// Accessing individual elements of the array
Array_name[Index];
Insert and print array Elements
int mark[5] = {19, 10, 8, 17, 9}
// change 4th element to 9
mark[3] = 9;
// take input from the user
// store the value at third position
cin >> mark[2];
// take input from the user
// insert at ith position
cin >> mark[i-1];
// print first element of the array
cout << mark[0];
// print ith element of the array
cout >> mark[i-1];
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
// Printing array elements // using traditional for loop
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";}
return 0;
}
Example;
output
The numbers are: 7 5 6 12 35
Take input from the user and store in an array
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl; // store input from user to array
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];}
cout << "The numbers are: "; // print array elements
for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}
return 0;}
Enter 5 numbers:
11
12
13
14
15
The numbers are: 11 12 13 14 15
Recursion
void recurse(){ ... .. ...
recurse();
... .. ...}
int main(){
... .. ...
recurse();
... .. ...}
Recursion : Factorial of a number
// Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int);
int main() {
int n, result;
cout << "Enter a non-negative number: ";
cin >> n;
result = factorial(n);
cout << "Factorial of " << n << " = " << result;
return 0;
}
int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}
Enter a non-negative number: 4
Factorial of 4 = 24
Working of Factorial Program
Character Array (C-string)
Declaration of Character Array (string array)
Binary Search
Binary Search Algorithm (Iterative)
do until the pointers low and high meet each other.
mid = (low + high)/2
if (x == arr[mid])
return mid
else if (x > arr[mid]) // x is on the right side
low = mid + 1
else // x is on the left side
high = mid - 1
Binary Search Algorithm (Recursive)
binarySearch(arr, x, low, high)
if low > high
return False
else
mid = (low + high) / 2
if x == arr[mid]
return mid
else if x > arr[mid] // x is on the right side
return binarySearch(arr, x, mid + 1, high)
else // x is on the right side
return binarySearch(arr, x, low, mid - 1)
Binary Search working
x = 4 be the element to be searched.Binary Search working
Binary Search working
arr[(low + high)/2] = 6. x > mid, compare x with the middle element of the elements on the right side of mid. This is done by setting low to low = mid + 1.high = mid - 1.x = 4 is found.
Binary Search Complexity
Time Complexities
O(1)
O(log n)
O(log n)
Bubble Sort
Bubble sort is a sorting algorithm that compares two adjacent elements
swaps them until they are in the intended order.
Just like the movement of air bubbles in the water that rise up to the surface,
each element of the array move to the end in each iteration. Therefore, it is called a bubble sort.
Bubble Sort Working
Suppose we are trying to sort the elements in ascending order.
Bubble Sort Working
2. Remaining Iteration
The same process goes on for the remaining iterations.
After each iteration, the largest element among the unsorted elements is placed at the end.
Bubble Sort Working
In each iteration, the comparison takes place up to the last unsorted element.
In each iteration, the comparison takes place up to the last unsorted element.
Bubble Sort Complexity
| Best | O(n) |
| Worst | O(n2) |
| Average | O(n2) |
Hence, the number of comparisons is
(n-1) + (n-2) + (n-3) +.....+ 1 = n(n-1)/2
nearly equals to n2
Hence, Complexity: O(n2)
Also, if we observe the code, bubble sort requires two loops. Hence, the complexity is n*n = n2
Time Complexity
O(n2)
If we want to sort in ascending order and the array is in descending order then the worst case occurs.O(n)
O(n2)