Array

C++ Program to Print Upperhalf and Lowerhalf Triangle of Square Matrix

Here is the C++ program to print upperhalf and lowerhalf triangle of a square matrix. #include<iostream> using namespace std; int main() { int a[10][10],i,j,m; cout<<“Enter size of the Matrix(min:3,max:5):”; cin>>m; cout<<“\nEnter the Matrix row wise:\n”; for(i=0;i<m;i++) for(j=0;j<m;++j) cin>>a[i][j]; cout<<“\n\n”; for(i=0;i<m;++i) { for(j=0;j<m;++j) { if(i<j) cout<<a[i][j]<<” “; else cout<<” “; } cout<<“\n”; } cout<<“\n\n”; for(i=0;i<m;++i) { …

C++ Program to Print Upperhalf and Lowerhalf Triangle of Square Matrix Read More »

C++ Program to Find Largest and Second Largest Number in 2D Array

Here is the C++ program to find largest and second largest number in a 2d array or matrix. #include<iostream> using namespace std; int main() { int a[5][5],big1=1,big2=0,n,m,i,j; cout<<“Enter no of rows and columns(max 5):”; cin>>m>>n; cout<<“Enter the array:\n”; for(i=0;i<m;i++) for(j=0;j<n;++j) cin>>a[i][j]; for(i=0;i<m;++i) for(j=0;j<n;++j) { if(a[i][j]>big1) big1=a[i][j]; } for(i=0;i<m;++i) for(j=0;j<n;++j) { if(a[i][j]>big2&&a[i][j]<big1) big2=a[i][j]; } cout<<“\nLargest number:”<<big1; …

C++ Program to Find Largest and Second Largest Number in 2D Array Read More »

C++ Program to Find Sum of Diagonals of Matrix

Here is the C++ program to find the sum of diagonals of a matrix. The matrix should be a square matrix. #include<iostream> using namespace std; int main() { int a[5][5],d1sum=0,d2sum=0,m,i,j; cout<<“Enter size of the square matrix(max 5):”; cin>>m; cout<<“\nEnter the Matrix row wise:\n”; for(i=0;i<m;i++) for(j=0;j<m;++j) cin>>a[i][j]; for(i=0;i<m;++i) for(j=0;j<m;++j) { if(i==j) d1sum+=a[i][j]; if(i+j==(m-1)) d2sum+=a[i][j]; } cout<<“\nSum …

C++ Program to Find Sum of Diagonals of Matrix Read More »

Linear Search in C++

Here you will get program for linear search in C++. In linear search algorithm, we compare targeted element with each element of the array. If the element is found then its position is displayed. The worst case time complexity for linear search is O(n). Program for Linear Search in C++ #include<iostream> using namespace std; int main() …

Linear Search in C++ Read More »

Binary Search in C++

Here you will learn about binary search in C++. Binary search is an algorithm used to search for an element in a sorted array. In this algorithm the targeted element is compared with middle element. If both elements are equal then position of middle element is returned and hence targeted element is found. If both …

Binary Search in C++ Read More »

C++ Program to Find Highest and Lowest Element of a Matrix

Here you will get a C++ program to find highest or largest and lowest or smallest element of a matrix. #include<iostream> using namespace std; int main() { int m,n,a[10][10],i,j,high,low; cout<<“Enter no. of rows and coloumns:”; cin>>m>>n; cout<<“\nEnter matrix:\n”; for(i=0;i<m;++i) { for(j=0;j<n;++j) cin>>a[i][j]; } high=a[0][0]; low=a[0][0]; for(i=0;i<m;++i) { for(j=0;j<n;++j) { if(a[i][j]>high) high=a[i][j]; else if(a[i][j]<low) low=a[i][j]; } …

C++ Program to Find Highest and Lowest Element of a Matrix Read More »