Lab 4
Lab 4
Lab 4
TASK #1:
Define array. How an element is accessed in array. Write the use of array.
SOLUTION:
An array is a collection of elements of the same type placed in contiguous memory locations that can
be individually referenced by using an index to a unique identifier. Five values of type int can be declared
as an array without having to declare five different variables (each with its own identifier). For example
int value[5];
Array elements are accessed by using an integer index. Array index starts with 0 and goes till size
of array minus 1. Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
Task #2:
Define two-dimensional array. Explain with proper example. Write the use of 2-D array.
SOLUTION:
A 2D array has a type such as int[][] or String[][],
with two pairs of square brackets. The elements of a 2D
array are arranged in rows and column. For example
float x[3][4];
TASK #4:
Write a program that initializes a two dimensional array of 2 rows and 3 columns and then
displays its values.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int Arr[2][3] = { {9,8,7}, {6,5,4}};
for (int i=0; i<2; i++)
for (int j=0; j<3; j++)
{
cout << "Arr[" << i << "][" << j << "]: ";
cout << Arr[i][j]<< endl;
}
return 0;
}
Page 3 of 12
TASK #5:
Write a program that store integer value in an array of 2 rows and 4 columns. The values
should be user defined and then displays its values.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int Arr[2][4];
for (int i=0; i<2; i++)
for (int j=0; j<4; j++)
{
cout << "Enter Arr[" << i << "][" << j << "]: ";
cin >> Arr[i][j];
}
cout<<"\n***Displaying***\n";
for (int i=0; i<2; i++)
for (int j=0; j<4; j++)
{
cout << "Arr[" << i << "][" << j << "]: ";
cout << Arr[i][j] << endl;
}
return 0;
}
Page 4 of 12
TASK #6:
Write a program that declares a two-dimensional array of 2 rows and 4 columns and then
display the minimum and maximum number in the array. The values of array should be user
defined.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int max, min, i, j;
int Arr[2][4];
max = Arr[0][0];
min = Arr[0][0];
TASK #7:
Write a program that takes the values of matrix from user and then tells either the matrix is
symmetric or non-symmetric (two-dimensional array). The matrix should be of 4 rows and
4 columns.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int a[4][4],i,j,temp;
int count=0;
cout<<"Enter elements of 4*4 matrix\n";
for(i=0;i<4;i++)
for(j=0;j<4;j++)
cin>>a[i][j];
for(i=0;i<3;i++)
for(j=i+1;j<4;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
for(i=0;i<4;i++)
{
cout<<"\n";
for(j=0;j<4;j++)
cout<<a[i][j];
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(a[i][j]!=a[j][i])
count++;
}
if(count==0)
cout<<"\nMatrix is symmetric";
else
cout<<"\nmatrix is not symmetric and "
<<count<<" values are not matched ";
}
Page 7 of 12
Page 8 of 12
TASK #8:
Write a program to add two matrices (two-dimensional array). Input order of matrix (i.e.
number of rows and columns). The matrices must be of same size to be added. Get the
input for each element of the first matrix and then second matrix. Add the two matrices
and store the values in third matrix. Finally, display all matrices.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
TASK #9:
Write a program that input the number of rows and columns from user. It then input the
elements to store in matrix. The program calculates the sum of each row and each column
and display on the output screen.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int A[100][100];
int row, col, r, c, sum = 0;
cout << "Sum of elements of Column " << row+1 << "= "<< sum <<endl;
}
return 0;
}
Page 11 of 12
Page 12 of 12
TASK #10:
Write a program that take values of 4*4 matrix from user and display the sum of diagonal
elements of a matrix.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int a[4][4], d1sum=0, d2sum=0, m=4, i, j;
cout<<"***Enter elements in matrix of size 4x4***\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 of 1st diagonal is = "<<d1sum;
cout<<"\nSum of 2nd diagonal is = "<<d2sum;
}