Вы находитесь на странице: 1из 12

Page 1 of 12

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

Here, x is a two-dimensional (2d) array. The array can hold


12 elements. You can think the array as a table with 3 rows and each row has 4 columns.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for
each value. A one-dimensional array can be seen as data elements organized in a row. A two-
dimensional array is similar to a one-dimensional array, but it can be visualized as a grid (or table) with
rows and columns. Many games use two dimensional arrays to plot the visual environment of a game.
TASK #3:
Distinguish between 1-D array and 2-D array.
SOLUTION:
A one-dimensional array can be seen as data elements organized in a row. A two-dimensional array is
similar to a one-dimensional array, but it can be visualized as a grid (or table) with rows and columns.
The main difference between 1D and 2D array is that the 1D array represents multiple data items as a
list while 2D array represents multiple data items as a table consisting of rows and columns. A variable
is a memory location to store data of a specific type.
BASIS FOR ONE-DIMENSIONAL TWO-DIMENSIONAL
Comparison
Basic Store single list of elements of Store 'list of lists' or 'array of arrays' or 'array
similar data type. of one dimensional arrays'.
Declaration type variable_name[ size ]; type variable_name[size1][size2];

Size in Bytes Total Bytes =sizeof(datatype of Total Bytes= sizeof(datatype of array


array variable)* size of array. variable)* size of 1st index*size of 2nd index.
Page 2 of 12

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

for (i=0; i<2; i++)


for (j=0; j<4; j++)
{
cout << "Enter Arr[" << i << "][" << j << "]: ";
cin >> Arr[i][j];
}

max = Arr[0][0];
min = Arr[0][0];

for (i=0; i<2; i++)


for (j=0; j<4; j++)
{

// If current element is greater than max


if(Arr[i][j] > max)
max = Arr[i][j];

// If current element is smaller than min


if(Arr[i][j] < min)
min = Arr[i][j];
}

// Print maximum and minimum element


cout<<"\nMaximum element =" << max;
cout<<"\nMinimum element =" << min;
return 0;
}
Page 5 of 12
Page 6 of 12

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 << "Enter number of rows: ";


cin >> r;
cout << "Enter number of columns: ";
cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

// Storing elements of first matrix entered by user.


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cin >> a[i][j];
}

// Storing elements of second matrix entered by user.


cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cin >> b[i][j];
}
// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
}
Page 9 of 12
Page 10 of 12

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;

/* Input rows and columns from user */


cout<<"Enter numbers of Rows: "; cin>>r;
cout<<"Enter numbers of Columns: "; cin>>c;

/* Input elements in matrix from user */


cout<<"\n***Enter elements in matrix of size " <<r<<"x"<<c<<"***\n";
for(row=0; row<r; row++)
{
for(col=0; col<c; col++)
{
cin >> A[row][col];
}
}

/* Calculate sum of elements of each row of matrix */


for(row=0; row<r; row++)
{
sum = 0;
for(col=0; col<c; col++)
{
sum += A[row][col];
}
cout << "Sum of elements of Row " << row+1 << "= "<< sum <<endl;
}

/* Find sum of elements of each columns of matrix */


for(row=0; row<r; row++)
{
sum = 0;
for(col=0; col<c; col++)
{
sum += A[col][row];
}

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

Prepared By: Khawar Khalil

Вам также может понравиться