Unit-3_arrays_6NamELgGXk
Unit-3_arrays_6NamELgGXk
For example:
Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27 separate variables, we
can simply create an array:
double grade[27];
Here, grade is an array that can hold a maximum of 27 elements of double type.
In C++, the size and type of arrays cannot be changed after its declaration.
Syntax of declaring an array
data_type array_name [array_size];
BY PROF.AVANI BHUVA 3
1. Write a C++ program to accept n integers from user and print them one in each line.
#include<iostream>
using namespace std;
int main()
{
int n,i,a[100];
cout<<"Enter the number of elements:";
cin>>n;
for(i=0;i<=n-1;i++)
{
cout<<"Enter a value:"<<endl;
cin>>a[i];
}
cout<<"The numbers entered are"<<endl;
for(i=0;i<=n-1;i++)
{
cout<< endl<<a[i];
}
return 0;
}
BY PROF.AVANI BHUVA 8
2.Write a C++ program to accept n integers and find average of all these numbers
#include<iostream>
using namespace std;
int main()
{
int n,i,a[100];
float avg, sum=0.0;
cout<<"Enter the number of elements:");
cin>>n;
for(i=0;i<=n-1;i++)
{
cout<<"Enter a value:“<<endl;
cin>>a[i];
}
for(i=0;i<=n-1;i++)
{
sum=sum+a[i];
}
avg=sum;
avg=avg/n;
cout<<"The average of the numbers entered is and sum is“<<avg<<endl<<sum;
return 0;}
BY PROF.AVANI BHUVA 9
3- Write a program to copy one 1D array into 4- Implement a program to reverse elements
another 1D array and display copied array. of 1D array and display it.
#include<iostream>
using namespace std; #include<iostream>
int main() using namespace std;
{ int main()
int initA[100],finA[100],i,size; {
cout<<"Input the size of the array : "; int arr[10], i;
cin>>size; cout<<"Enter 10 Array Elements: ";
cout<<"Input the elements of the first array"; for(i=0; i<10; i++)
for(i=0;i<size;i++) cin>>arr[i];
{ cout<<"\nThe Original Array is:\n";
cin>>initA[i]; for(i=0; i<10; i++)
} cout<<arr[i]<<" ";
for(i=0;i<size;i++) cout<<"\n\nThe Reverse of Given Array is:\n";
{ for(i=(10-1); i>=0; i--)
finA[i]=initA[i]; cout<<arr[i]<<" ";
} cout<<endl;
cout<<"The final array is\n"; return 0;
for(i=0;i<size;i++) }
cout<<finA[i]<<" ";
return 0;
}
5. C++ Program to Find Largest and Smallest Element of an Array
#include<iostream>
using namespace std;
int main ()
{
int a[10], n, i, max, min;
cout << "Enter the size of the array : ";
cin >> n;
cout << "Enter the elements of the array : ";
for (i = 0; i <= n-1; i++)
cin >> a[i];
max = a[0];
for (i = 0; i <=n-1; i++)
{
if (max < a[i])
max = a[i];
}
min = a[0];
for (i = 0; i <=n-1; i++)
{
if (min > a[i])
min = a[i];
}
cout << "Largest element : " << max<<endl;
cout << "Smallest element : " << min;
return 0;
11
} By Prof.Avani Bhuva
6. Decimal to binary conversion
The below solved example along with step by step calculation for decimal to binary conversion
let the users to understand how to perform such conversion manually.
Step by step conversion:
step 1: For decimal to binary conversion by successive division, divide the decimal number by 2
until the quotient reach to 1 or 0.
step 2: Note down every remainder (normally 1 or 0) for each successive division by 2. The first
& last remainder is the LSD (least significant digit or bit) & MSD (most significant digit or bit)
respectively.
step 3: Arranging the remainder from MSD to LSD is the equivalent binary for the given
decimal.
BY PROF.AVANI BHUVA 12
Write a program to print binary equivalent of a decimal number using array. Lab Excercise
#include <iostream>
using namespace std;
int main()
{
int a[10],n,i;
cout<<"Enter the number to convert: ";
cin>>n;
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
} 25
cout<<"\nBinary of Given Number is="<<endl; 2 25 1 <----- first remainder
for(i=i-1;i>=0;i--) 0 <----- second remainder
2 12
{
cout<<a[i]; 2 6
0 <----- third remainder
} 2 3 1 <----- fourth remainder
return 0; 1 <----- fifth remainder
2 1
}
0
By Prof.Avani Bhuva 13
7. Linear Search in C++
To search any element present inside the array in C++ programming using linear search technique, you have to ask from user
to enter any 10 numbers as 10 array elements and then ask to enter a number to search as shown in the program given below.
This program doesn't allows user to define the size of an array. Later on, you will go through the program that allows user to
define the size and also prints all the indexes of an element, if found multiple times.
This is the simplest program to implement linear search in C++
BY PROF.AVANI BHUVA 14
Linear search is also known as sequential search. It is straightforward and works as follows: We keep on comparing each element with the element
to search until it is found or the list ends.
#include <iostream>
using namespace std;
int main()
{ int array[100], search, c, n;
cout<<"Enter number of elements in array\n";
cin>>n;
for (c = 0; c < n; c++)
{
cout<<“enter the elements in an array”;
cin>>array[c];
}
cout<< "Enter a number to search\n";
cin>>search;
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
cout<<"element is present at location"<<search<<"index at"<<c+1;
break;
} }
if (c == n)
cout<<"element isn't present in the array.\n", search;
return 0;} By Prof.Avani Bhuva 15
Lab Exercise-1
#include<iostream>
using namespace std;
int main()
{
int a[5]={1,2,3,4,5};
int n=5;
int num=2;
for(int i=0;i<=n-1;i++)
a[i]=a[i]*num;
for(int i=0;i<=n-1;i++)
cout<<arr[i]<<" ";
return 0;
}
Output:
2 4 6 8 10
By Prof.Avani Bhuva 16
Lab Exercise-2 Write a program to count and display number of odd & even elements from an array
(1D) separately.
#include <iostream>
using namespace std;
int main()
{
Output:
int arr[10],even[10],odd[10],evncnt=0,oddcnt=0,i;
Input numbers in the array4
cout<<"Input numbers in the array";
1
for(i=0;i<10;i++)
3
cin>>arr[i];
5
for(i=0;i<10;i++)
6
{
5
if(arr[i]%2==0)
4
even[evncnt++]=arr[i];
12
else
45
odd[oddcnt++]=arr[i];
33
}
The even numbers are: 4 6 4 12
cout<<"The even numbers are: ";
The odd numbers are: 1 3 5 5 45 33
for(i=0;i<evncnt;i++)
cout<<even[i]<<" ";
cout<<"\nThe odd numbers are: ";
for(i=0;i<oddcnt;i++)
cout<<odd[i]<<" ";
}
Implement a program to find the intersection of two arrays cout<<"\nThe intersection of the two arrays: ";
for(i=0;i<n1;i++)
#include<iostream>
{
using namespace std;
for(j=0;j<n2;j++)
int main()
{
{
if(arr1[i]==arr2[j])
int n1,n2,i,j;
{
cout<<"Enter the no. of elements of the 1st array: ";
cout<<arr1[i]<<" ";
cin>>n1;
}
int arr1[n1];
}
cout<<"Enter the elements of the 1st array: ";
}
for(i=0;i<n1;i++)
{
cin>>arr1[i];
}
cout<<"\nEnter the no. of elements of the 2nd array: ";
cin>>n2;
int arr2[n2];
cout<<"Enter the elements of the 2nd array: ";
for(i=0;i<n2;i++)
{
cin>>arr2[i];
}
Create a program to exchange first and last element of the 1D array of size N.
#include<iostream>
using namespace std;
int main()
{
int n, temp;
cout<<"Enter Number of elements you want to enter :: ";
cin>>n;
int arr[n];
for(int i = 0;i<n;i++){
cin>>arr[i];
}
temp = arr[0];
arr[0] = arr[n-1];
arr[n-1] = temp;
cout<<"Array after swapping first and last elements "<<endl;
for(int i = 0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Example 1: Taking Input for Two Dimensional Array Output:
21
The numbers are:
#include <iostream> numbers[0][0]: 12
using namespace std; numbers[0][1]: 23
int main() { numbers[0][2]: 45
int numbers[2][3]; numbers[1][0]: 67
cout << "Enter 6 numbers: " << endl; numbers[1][1]: 89
// Storing user input in the array numbers[1][2]: 21
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cin >> numbers[i][j];
}
cout << "The numbers are: " << endl;
// Printing array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3;j++) {
cout << "numbers[" << i << "][" << j << "]: " << numbers[i][j] << endl;
}
}
return 0;
}
By Prof.Avani Bhuva 30
Develop
5 a program to perform sum of elements of matrix (2D array) of order MXN.
Develop
6 a program to find sum of elements of lower triangular matrix of order MxN.
Write
7 a program to perform addition of two matrix (2D array) and display the resultant matrix.
Implement
8 a program to find the largest element 3X3 matrix.
Write a C++ program to calculate and display the average of all the elements in 2d array.
#include <iostream>
Enter the number of rows and columns:2
using namespace std;
2
int main()
Enter a value:1
{
Enter a value:2
int m,n,i,j,a[10][10];
Enter a value:3
float avg,sum=0.0;
Enter a value:4
cout<<"Enter the number of rows and columns:";
The average is equal to 2.5
cin>>m>>n;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
cout<<"Enter a value:";
cin>>a[i][j];
}}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
sum=sum+a[i][j];
}}
avg=1.0 * sum/(m*n);
cout<<"The average is equal to "<<avg;} BY PROF.AVANI BHUVA 33
Develop a program to find sum of elements of lower triangular
matrix of order MxN.