Write a C++ program to display array elements.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {7, 5, 6, 12, 35};
cout<<“\nThe array elements are: “;
for (int i = 0; i < 5; i++) {
cout <<arr[i] << ” “;
}
return 0;
}
Output:
The array elements are: 7Â 5Â 6Â 12Â 35
Write a program in C++ to read and print elements of 1D array.
#include <iostream>
using namespace std;
int main() {
int arr[5];
cout <<“Enter 5 elements in the array: “;
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << “\nThe array elements are: “;
for (int i = 0; i < 5; i++) {
cout << arr[i] << ” “;
}
return 0;
}
Output:
Enter 5 elements in the array: 2 1 9 6 3
The array elements are:Â 2Â 1Â 9Â 6Â 3
Write a C++ program print the First and Last  element of an array.
#include <iostream>
using namespace std;
int main()
{
int num[5],i;
cout<<“Enter 5 elements in the array: “;
for(i=0;i<5;i++)
{
cin>>num[i];
}
cout<<“First Elemet: “<< num[0]<<endl;
cout<<“Last Element: “<<num[4]<<endl;
}
Output:
Enter 5 elements in the array: 4 5 2 1 8
First Elemet: 4
Last Element: 8
Write a program in C++ to display sum and average of array elements.
#include <iostream>
using namespace std;
int main() {
int arr[5],avg, sum=0;
cout <<“Enter 5 elements in the array: “;
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << “\nThe array elements are: “;
for (int i = 0; i < 5; i++) {
cout << arr[i] << ” “;
sum=sum+arr[i];
}
avg=sum/5;
cout<<“\nSum = “<<sum;
cout<<“\nAverage = “<<avg;
return 0;
}
Output:
Enter 5 elements in the array: 2 1 4 7 6
The array elements are: 2 1 4 7 6
Sum = 20
Average = 4
Write a C++ program to read the marks of 5 subjects of a student and find the total marks and average using array.
#include <iostream>
using namespace std;
int main()
{
float marks[5], sum=0,avg;
for(int i=0; i<5; i++)
{
cout<< “Enter Mark ” << i+1 << “= “;
cin>> marks[i];
sum=sum+marks[i];
}
avg=sum/5.0;
cout<< “\n The Total Marks: ” << sum;
cout<< “\n The Average Mark: ” <<avg;
}
Output:
Enter Mark 1= 56
Enter Mark 2= 67
Enter Mark 3= 54
Enter Mark 4= 78
Enter Mark 5= 89
The Total Marks: 344
 The Average Mark: 68.8
Write a program to find the size and length of an array.
#include <iostream>
using namespace std;
int main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
// Size of one element of an array
cout << “Size of arr[0]: ” << sizeof(arr[0]) << endl;
// Size of array ‘arr’
cout << “Size of arr: ” << sizeof(arr) << endl;
// Length of an array
int n = sizeof(arr) / sizeof(arr[0]);
cout << “Length of an array: ” << n << endl;
return 0;
}
Output:
Size of arr[0]: 4
Size of arr: 20
Length of an array: 5
Write a C++ program to reverse array elements.
#include <iostream>
using namespace std;
int main() {
int arr[5];
cout <<“Enter 5 elements in the array: “;
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << “\nThe array elements are: “;
for (int i = 0; i < 5; i++) {
cout << arr[i] << ” “;
}
cout << “\nThe reverse array elements are: “;
for (int i = 4; i >=0; i–) {
cout << arr[i] << ” “;
}
return 0;
}
Output:
Enter 5 elements in the array: 2 3 1 5 4
The array elements are: 2 3 1 5 4
The reverse array elements are:Â 4Â 5Â 1Â 3Â 2
Write a program in C++ to find maximum and minimum value of a one dimensional array.
#include <iostream>
using namespace std;
int main() {
int arr[5],max,min;
cout <<“Enter 5 elements in the array: “;
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << “\nThe array elements are: “;
for (int i = 0; i < 5; i++) {
cout << arr[i] << ” “;
}
max=min=arr[0];
for(int i=0;i<5;i++){
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
cout<<“\nMaximum value: “<<max;
cout<<“\nMaximum value: “<<min;
return 0;
}
Output:
Enter 5 elements in the array: 2 1 5 3 9
The array elements are: 2 1 5 3 9
Maximum value: 9
Maximum value: 1
Write a C++ program to insert an element at specific index position.
#include<iostream>
using namespace std;
int main()
{
int arr[10], i, n,element, pos;
cout<<“Enter number of elements in the array: “;
cin>>n;
cout<<“Enter “<<n<<” Elements: “;
for(i=0; i<n; i++)
cin>>arr[i];
cout<<“\nEnter Element to Insert: “;
cin>>element;
cout<<“At What Position want to insert? “;
cin>>pos;
for(i=n-1; i>=pos-1; i–)
arr[i+1] = arr[i];
arr[pos-1] = element;
cout<<“\nResultant Array is:\n”;
for(i=0; i<=n; i++)
cout<<arr[i]<<” “;
cout<<endl;
return 0;
}
Output:
Enter number of elements in the array: 4
Enter 4 Elements: 1 2 3 4
Enter Element to Insert: 7
At What Position want to insert? 2
Resultant Array is:
1Â 7Â 2Â 3Â 4
Write a C++ program to sort elements of an array in ascending order.
#include <iostream>
using namespace std;
int main()
{
int arr[6],i=0,j=0,temp=0;
cout<<“Enter 6 elements in the array: “;
for(i=0;i<6;i++)
{
cin>>arr[i];
}
//Sorting array elements
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<“Elements in ascending order: “;
for(i=0;i<6;i++)
{
cout<<arr[i]<<” “;
}
}
Output:
Enter 6 elements in the array: 6 4 2 9 1 3
Elements in ascending order: 1 2 3 4 6 9
Write a program in C++ to sort elements of an array in descending order.
#include <iostream>
using namespace std;
int main()
{
int arr[6],i=0,j=0,temp=0;
cout<<“Enter 6 elements in the array: “;
for(i=0;i<6;i++)
{
cin>>arr[i];
}
//Sorting array elements
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<“Elements in descending order: “;
for(i=0;i<6;i++)
{
cout<<arr[i]<<” “;
}
}
Output:
Enter 6 elements in the array: 6 4 2 9 1 3
Elements in descending order: 9 6 4 3 2 1
Write a C++ program to search an element from an array
#include <iostream>
using namespace std;
int main() {
int arr[5],data;
cout <<“Enter 5 elements in the array: “;
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout<<“Enter the element to be searched: “;
cin>>data;
cout << “\nThe array elements are: “;
for (int i = 0; i < 5; i++) {
cout << arr[i] << ” “;
}
cout<<“\nElement to be searched is: “<<data;
for (int i = 0; i < 5; i++) {
if(arr[i]==data){
cout<<“\nSearch is successful”;
cout<<“\nData found at position: “<<i+1;
return 0;
}
else
i++;
}
cout<<“\nSearch is unsuccessful”;
return 0;
}
Output:
Enter 5 elements in the array: 21 34 23 56 43
Enter the element to be searched: 50
The array elements are: 21 34 23 56 43
Element to be searched is: 50
Search is unsuccessful
Write a program to read and print elements in two-dimensional array
#include<iostream>
using namespace std;
int main()
{
// Declaring 2D array
int arr[4][4];
// Initialize 2D array using loop
cout<<“Enter elements in the array: \n”;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cin>>arr[i][j];
}
}
// Printing the element of 2D array
cout<<“\nThe array elements are: \n”;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << ” “;
}
cout << endl;
}
return 0;
}
Output:
Enter elements in the array:
1 2 5 1
4 2 3 6
7 4 2 3
8 4 2 3
The array elements are:
1 2 5 1
4 2 3 6
7 4 2 3
8 4 2 3
Write a program to find the maximum and minimum values in two-dimensional array.
#include<iostream>
using namespace std;
int main()
{
// Declaring 2D array
int arr[4][4],max,min;
// Initialize 2D array using loop
cout<<“Enter elements in the array: \n”;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cin>>arr[i][j];
}
}
// Printing the element of 2D array
cout<<“\nThe array elements are: \n”;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << ” “;
}
cout << endl;
}
max=min=arr[0][0];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(arr[i][j]>max)
max=arr[i][j];
if(arr[i][j]<min)
min=arr[i][j];
}
}
cout<<“\nMaximum value: “<<max;
cout<<“\nMaximum value: “<<min;
return 0;
}
Output:
Enter elements in the array:
1 2 3 5
4 3 2 1
7 6 5 4
3 2 4 1
The array elements are:
1 2 3 5
4 3 2 1
7 6 5 4
3 2 4 1
Maximum value: 7
Maximum value: 1
Write a program to add two matrices.
#include<iostream>
using namespace std;
int main()
{
int a[2][2], b[2][2], sum[2][2];
int i,j;
cout<<“Enter Elements for First Matrix: \n”;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
cin>>a[i][j];
cout<<“\nEnter Elements for Second Matrix: \n”;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
cin>>b[i][j];
// Adding Two matrices
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
sum[i][j] = a[i][j] + b[i][j];
}
// Displaying the resultant sum matrix.
cout <<“\nSum of two matrix is: ” << endl;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
cout << sum[i][j] << ” “;
cout<<“\n”;
}
return 0;
}
Output:
Enter Elements for First Matrix:
2 3
4 5
Enter Elements for Second Matrix:
1 2
5 6
Sum of two matrix is:
3 5
9Â 11
Write a C++ program to subtract two matrices.
#include<iostream>
using namespace std;
int main()
{
int a[2][2], b[2][2], sub[2][2];
int i,j;
cout<<“Enter Elements for First Matrix: \n”;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
cin>>a[i][j];
cout<<“\nEnter Elements for Second Matrix: \n”;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
cin>>b[i][j];
// Adding Two matrices
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
sub[i][j] = a[i][j] – b[i][j];
}
// Displaying the resultant sum matrix.
cout <<“\nSubtraction of two matrix is: ” << endl;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
cout << sub[i][j] << ” “;
cout<<“\n”;
}
return 0;
}
Output:
Enter Elements for First Matrix:
5 4
3 2
Enter Elements for Second Matrix:
2 2
3 1
Subtraction of two matrix is:
3 2
0Â 1
Write a program in C++ to multiply two matrices.
#include<iostream>
using namespace std;
int main()
{
int a[2][2], b[2][2], c[2][2];
int i, j, k, sum=0;
cout<<“Enter Elements for First Matrix: \n”;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
cin>>a[i][j];
}
cout<<“\nEnter Elements for Second Matrix: \n”;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
cin>>b[i][j];
}
// Multiplying two matrices…
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
sum=0;
for(k=0; k<2; k++)
sum = sum + (a[i][k] * b[k][j]);
c[i][j] = sum;
}
}
cout<<“\nMultiplication Result:\n”;
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
cout<<c[i][j]<<“\t”;
cout<<endl;
}
cout<<endl;
return 0;
}
Output:
Enter Elements for First Matrix:
2 3
4 5
Enter Elements for Second Matrix:
1 2
2 3
Multiplication Result:
8 13
14Â Â Â Â Â 23