0% found this document useful (0 votes)
20 views

Unit-3_arrays_6NamELgGXk

basics of computer science engineering in cpp or c++ undergrad courtesy SVKM's NMIMS

Uploaded by

nishantparwani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit-3_arrays_6NamELgGXk

basics of computer science engineering in cpp or c++ undergrad courtesy SVKM's NMIMS

Uploaded by

nishantparwani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Unit-3

S 1 – D AND 2 - D ARRAYS, STRINGS


 It is group of logically related data, stored in contiguous blocks of memory under common name.
 An Array is homogeneous or similar type of data under common name.
 Data items or elements of arrays are separated by subscript or index.
 Array is an indirect pointer.
 C++ Supports following arrays:
1. One Dimensional Arrays
2. Two or Multi-Dimensional Arrays.
3. One-dimensional arrays are represented as set of values in one row.
4. Multi-dimensional arrays are views as table-containing data i.e. rows & columns.

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];

data_type : int,float,double etc.


array_name : name of the variable
array_size : integer values which determines size of the array or memory locations to be allocated
to an array

Eg. int a[10];

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.

#include <iostream> //Addition of all Diagonal Elements


using namespace std; for (i = 0; i < row; i++)
int main() {
{ for (j = 0; j < col; j++)
int i, j, matrix[10][10], row, col,sum = 0; {
cout<<"\nEnter the number of Rows : "; if(j<i)
cin>>row; {
cout<<"\nEnter the number of Columns : "; sum+=matrix[i][j];
cin>>col; }
//Accept the Elements in m x n Matrix }
for (i = 0; i < row; i++) }
{ cout<<"sum of lower triangular matrix="<<sum;
for (j = 0; j < col; j++) }
{
cout<<"\nEnter the Element : "<<endl;
cin>>matrix[i][j];
}}
Addition of two matrix // Adding Two matrices
for(i = 0; i < r; ++i)
#include <iostream> for(j = 0; j < c; ++j)
using namespace std; sum[i][j] = a[i][j] + b[i][j];
int main() // Displaying the resultant sum matrix.
{ cout << endl << "Sum of two matrix is: " << endl;
int r, c, a[100][100], b[100][100], sum[100][100], i, j; for(i = 0; i < r; ++i)
cout << "Enter number of rows "; for(j = 0; j < c; ++j)
cin >> r; {
cout << "Enter number of columns "; cout << sum[i][j] << " ";
cin >> c; if(j == c - 1)
cout << endl << "Enter elements of 1st matrix: " << endl; cout << endl;
for(i = 0; i < r; ++i) }
for(j = 0; j < c; ++j)
{ return 0;
cin >> a[i][j]; }
}
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cin >> b[i][j];
}
BY PROF.AVANI BHUVA 35
Write a C++ program to find largest number in matrix. Enter the number of rows and columns:
#include <iostream> 2
using namespace std; 2
int main() Enter a value:2
{ Enter a value:1
int m,n,i,j,a[10][10],large; Enter a value:3
cout<<"Enter the number of rows and columns:"<<endl; Enter a value:4
cin>>m>>n; The largest element in the matrix is4
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
cout<<"Enter a value:";
cin>>a[i][j];
}}
large=a[0][0];
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(large<a[i][j])
large=a[i][j];
}}
cout<<"The largest element in the matrix is"<<large;} BY PROF.AVANI BHUVA 36
Finding transpose of matrix.

#include <iostream> Enter the number of rows and columns of matrix


using namespace std; 2
int main() 2
{ Enter elements of the matrix
int m, n, c, d, matrix[10][10], transpose[10][10]; 1
cout<<"Enter the number of rows and columns of matrix\n"; 2
cin>>m>>n; 3
cout<<"Enter elements of the matrix\n"; 4
for (c = 0; c < m; c++) Transpose of the matrix:
for(d = 0; d < n; d++) 1
cin>>matrix[c][d]; 3
for (c = 0; c < m; c++) 2
for( d = 0 ; d < n ; d++ ) 4
transpose[d][c] = matrix[c][d];
cout<<"Transpose of the matrix:\n";
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
cout<<transpose[c][d]<<endl;
}
return 0;
}
BY PROF.AVANI BHUVA 37
#include <iostream>
using namespace std;
Sum of Diagonal elements in 2d array
int main()
Output:
{
int i, j, matrix[10][10], row, col,sum = 0; Enter the number of Rows : 2
cout<<"\nEnter the number of Rows : ";
cin>>row; Enter the number of Columns : 2
cout<<"\nEnter the number of Columns : ";
cin>>col; Enter the Element :
//Accept the Elements in m x n Matrix 1
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
Enter the Element :
{ 2
cout<<"\nEnter the Element : "<<endl;
cin>>matrix[i][j]; Enter the Element :
}} 3
//Addition of all Diagonal Elements
for (i = 0; i < row; i++) Enter the Element :
{
4
for (j = 0; j < col; j++)
{
if (i == j) Addition of Diagonal Array Elements in the Matrix is: 5
sum = sum + matrix[i][j];
}}
//Print out the Result
cout<<"\nAddition of Diagonal Array Elements in the Matrix is: "<<sum;
} By Prof.Avani Bhuva 38
C++ program to find sum of each row and columns of a matrix
#include <iostream>
using namespace std; for(row=0; row<size; row++)
int main() {
{ sum = 0;
int size=2; for(col=0; col<size; col++)
int a[size][size]; {
int row, col, sum; sum = sum + a[row][col];
cout<<"Please Enter elements in array of size }
"<<size<<"x"<<size<<endl<<endl; cout<<"Sum of elements of Row: "<< row+1<<" is "<<
for(row=0; row<size; row++) sum<<endl;
{ }
for(col=0; col<size; col++) for(col=0; col<size; col++)
{ {
cin>>a[row][col]; sum = 0;
} for(row=0; row<size; row++)
} {
for(row=0; row<size; row++) sum += a[row][col];
{ }
for(col=0; col<size; col++) cout<<"Sum of elements of Column: " <<row+1<<" is
{ "<<sum<<endl;
cout<<a[row][col]<<" "; }
} }
cout<<endl; BY PROF.AVANI BHUVA 39
#include <iostream> // Storing elements of second matrix.
using namespace std; Multiplication of two matrix cout << endl << "Enter elements of matrix 2:" << endl;
int main() for(i = 0; i < r2; ++i)
{ for(j = 0; j < c2; ++j)
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; {
cout << "Enter rows and columns for first matrix: "; cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> r1 >> c1; cin >> b[i][j];}
cout << "Enter rows and columns for second matrix: "; // Initializing elements of matrix mult to 0.
cin >> r2 >> c2; for(i = 0; i < r1; ++i)
// If column of first matrix in not equal to row of second matrix, for(j = 0; j < c2; ++j)
// ask the user to enter the size of matrix again. {
while (c1!=r2) mult[i][j]=0;}
{ // Multiplying matrix a and b and storing in array mult.
cout << "Error! column of first matrix not equal to row of second."; for(i = 0; i < r1; ++i)
cout << "Enter rows and columns for first matrix: "; for(j = 0; j < c2; ++j)
cin >> r1 >> c1; for(k = 0; k < c1; ++k)
cout << "Enter rows and columns for second matrix: "; {
cin >> r2 >> c2; mult[i][j] += a[i][k] * b[k][j];}
} // Displaying the multiplication of two matrix.
// Storing elements of first matrix. cout << endl << "Output Matrix: " << endl;
cout << endl << "Enter elements of matrix 1:" << endl; for(i = 0; i < r1; ++i)
for(i = 0; i < r1; ++i) for(j = 0; j < c2; ++j)
for(j = 0; j < c1; ++j) {
{ cout << " " << mult[i][j];
cout << "Enter element a" << i + 1 << j + 1 << " : "; if(j == c2-1)
cin >> a[i][j]; cout << endl; }
} return 0;}
41

You might also like