1.
Initialization
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"input size: ";
cin>>n;
int array[n];
cout<<"enter elements of an array:";
for(int i=0; i<n; i++){
cin>>array[i]; }
cout<<"Output:";
for(int i=0; i<n; i++){
cout<<array[i]<<" ";}}
2. Reversed.
#include<iostream> cout<<array[i]<<" ";
using namespace std; }}
int main(){
int n;
cout<<"input size: ";
cin>>n;
int array[n];
cout<<"enter elements of an array:";
for(int i=0; i<n; i++){
cin>>array[i]; }
cout<<"Normal order: ";
for(int i=0; i<n; i++){
cout<<array[i]<<" ";}
cout<<endl<<"reverse order: ";
for(int i=n-1; i>=0; i--){
3. Finding Minimum
#include<iostream> min= array[i];}}
using namespace std; cout<<"min:"<<min;
int main(){ }
int n;
cout<<"input size: ";
cin>>n;
int array[n];
cout<<"enter elements of an array:";
for(int i=1; i<n; i++){
cin>>array[i];}
int min=array[0];
for(int i=1; i<n; i++){
if (array[i]<min){
4. Finding Maximum
#include<iostream> max= array[i];}}
using namespace std; cout<<"max:"<<max;
int main(){ }
int n;
cout<<"input size: ";
cin>>n;
int array[n];
cout<<"enter elements of an array:";
for(int i=0; i<n; i++){
cin>>array[i];}
int max=array[0];
for(int i=0; i<n; i++){
if (array[i]>max){
5. Position and element
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"input size: ";
cin>>n;
int A[n];
cout<<"enter elements of an array:";
for(int i=0; i<n; i++){
cin>>A[i];}
cout<<"Output:"<<endl;
for(int i=0; i<n; i++){
cout<<"A["<<i<<"]="<<A[i]<<endl;
}}
6. 2D array input output
#include<iostream>
using namespace std;
int main(){
int m,n, M[m][n];
cout<<"input array size:";
cin>>m>>n;
cout<<"Enter elements of an array:";
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
cin>>M[i][j];}}
cout<<"Output:";
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
cout<<M[i][j]<<" ";
}}}
7. Transpose
#include<iostream> cout<<M[j][i]<<" ";
using namespace std; }
int main(){ cout<<endl;
int m,n; }
cout<<"input array size:"; }
cin>>m>>n;
int M[m][n], T[n][m];
cout<<"Enter elements of an array:";
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
cin>>M[i][j];
}
}
cout<<"Inputed Array:"<<endl;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
cout<<M[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<"Transpose of inputed
matrix:"<<endl;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
8. Encode
#include<iostream> cin>>j;
#include<string> string encoded=encode(s, j);
using namespace std; cout<<"Encoded
string encode(string s, int j) { string:"<<encoded<<endl;
for (int i=j;i<s.length();i=i+(j + 1)) { return 0;
s[i]=s[i]+2; }
}
return s;
}
int main(){
string s;
int j;
cout<<"Enter the string:";
getline(cin, s);
cout<<"the number of characters to
skip:";
9. Binary Search
#include <iostream> bubbleSort(Arr, size);
using namespace std; cout << "Sorted Array: ";
void bubbleSort(int Arr[], int size) { for (int i = 0; i < size; i++) {
for (int i = 0; i < size - 1; i++) { cout << Arr[i] << " ";}
for (int j = 0; j < size - i - 1; j++) { cout << endl;
if (Arr[j] > Arr[j + 1]) { int target;
int temp = Arr[j]; cout << "Enter the element to search: ";
Arr[j] = Arr[j + 1]; cin >> target;
Arr[j + 1] = temp;}}}} int result = binarySearch(Arr, size,
int binarySearch(int Arr[], int size, int target);
target) { if (result != -1) {
int left = 0, right = size - 1; cout << "Element found at index " <<
while (left <= right) { result << " (in the sorted array)" << endl;
int mid = left + (right - left) / 2; } else {
if (Arr[mid] == target) { cout << "Element not found in the
return mid; } array" << endl;}
if (Arr[mid] < target) { return 0;
left = mid + 1; }
} else {
right = mid - 1; }}
return -1;}
int main() {
int Arr[] = {13, 24, 56, 67, 32, 67, 78,
96};
int size = sizeof(Arr) / sizeof(Arr[0]);
10. Struct
#include <iostream> cout<<"ID:
using namespace std; "<<students[i].id<<endl;}}}
struct Std { int main() {
int id; int numStudents=10;
int completedCredits; Std students[10] = {{121, 112,
double cgpa;}; 3.8},{102, 30, 3.9},{123, 75, 3.6},{234,
void printHighCGStudents(Std 45, 3.85},
students[],int n) { {125, 60, 3.95},{346, 82, 3.4},{237, 50,
cout<<"Students with CGPA greater 3.76},{238, 65, 3.5},{119, 90, 3.78},{130,
than 3.75:"<<endl; 25, 4.0}};
for (int i=0; i<n;i++) { printHighCGStudents(students,
if (students[i].cgpa>3.75) { numStudents);
cout<<"ID: printStudentsCredit_50(students,
"<<students[i].id<<endl;}}} numStudents);
void printStudentsCredit_50(Std }
students[],int n) {
cout<<"Students who have completed
more than 50 credits:" <<endl;
for (int i=0;i<n;i++){
if (students[i].completedCredits>50)
{
11. Swapping
#include <iostream> function_ptr1();
using namespace std; function_ptr2();
void function1(){ cout<<"Address of function 1:
cout<<"Function 1"<<endl;} "<<(void*)function_ptr1<<endl;
void function2(){ cout<<"Address of function 2:
cout<<"Function 2"<<endl;} "<<(void*)function_ptr2<<endl;
int main() { return 0;
void(*function_ptr1)()=&function1; }
void(*function_ptr2)()=&function2;
cout<<"Address of Function of before
swapping"<<endl;;
cout<<"Address of function 1:
"<<(void*)function_ptr1<<endl;
cout<<"Address of function 2:
"<<(void*)function_ptr2<<endl;
void (*temp)()=function_ptr1;
function_ptr1=function_ptr2;
function_ptr2=temp;
cout<<endl<<"Final function addresses
after swapping:"<<endl;
12. Bubble short
#include <iostream> print(arr, n);
using namespace std; bubble(arr, n);
void print(int arr[], int n) { cout<<endl<<"Sorted(Bubble)
for (int i=0; i<n; i++) { Array:"<<endl;
cout<<arr[i]<<" ";} print(arr, n);
cout<<endl;} return 0;
void bubble(int arr[], int n) { }
for (int i=0; i<n; i++) {
for (int j=0; j<n- i - 1; j++) {
if (arr[j]>arr[j+1]) {
int temp=arr[j];
arr[j]=arr[j + 1];
arr[j+1]=temp;
}}}}
int main() {
int n;
cout<<" the number of elements:";
cin>>n;
int arr[n];
cout << "Input an Array: ";
for (int i=0; i<n; i++) {
cin>>arr[i];
}
cout<<endl<<"Original Array:"<<endl;
13. Insertion sort
#include <iostream> arr[m+1]=arr[m];
using namespace std; m--;
int main() { }
int n; arr[m+1]=temp;
cout<<"number of elements:"; }
cin>>n; cout << "After Insertion Sort:" << endl;
int arr[n]; for (int i = 0; i<n; i++) {
cout << "Enter 5 elements for the array: " cout<<arr[i]<<" ";
<< endl; }}
for (int i=0; i < n; i++) {
cin >> arr[i];
}
cout << "Original Array: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout<<endl;
for (int j = 1; j < n; j++) {
int m=j-1;
int temp=arr[j];
while (m>=0 && arr[m]>temp) {
14. Selection Sort
#include <iostream> A[minIndex]=temp;}}
using namespace std; cout<<endl<<"Sorted array:";
int main() { for (int i=0; i<n; i++) {
int n ; cout <<A[i]<< " ";}
cout<<"Enter the number of elements:"; cout<<endl;
cin>>n; return 0;
int A[n]; }
cout<<"Input an array:";
for (int i=0; i<n; i++) {
cin>>A[i];}
cout <<endl<<"Original array: ";
for (int i=0; i<n; i++) {
cout<<A[i]<<" ";}
cout<<endl;
for(int i=0;i<n-1; i++) {
int minIndex=i;
for (int j=i+1; j<n; j++) {
if (A[j]<A[minIndex]) {
minIndex=j;
}}
if (minIndex!=i) {
int temp=A[i];
A[i]=A[minIndex];