Professional Documents
Culture Documents
Task #1: Define Array. How An Element Isaccessed in Array. Write The Use of Array. Array
Task #1: Define Array. How An Element Isaccessed in Array. Write The Use of Array. Array
Define array. How an element isaccessed in array. Write the use of array.
ARRAY:
An array is a data structure that contains a group of elements. Typically these elements are all of
the same data type, such as an integer or string. Arrays are commonly used in computer
programs to organize data so that a related set of values can be easily sorted or searched.
SyntaxDatatype arrayname [array size];
Exp:
Int mark [5];
Array Elements are accessed by their addresses. Here we have declared an array named as mark.
The first element is mark [0], the second element is mark [1] and so on.
Elements of an array are accessed by specifying the index of the desired element within square
[ ] brackets after the array name.
Uses of Array:
An array is used to store a collection of data, but it is often more useful to think of an array as
a collection of variables of the same type.
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
Array stores a fixed-size sequential collection of elements of the same type.
Using Array will increase code readability and we can easily access the data from array using
array index.
TASK#2:
Define two-dimensionalarray. Explain with proper example. Write the use of 2-D
array.
TWO-DIMENSIONAL ARRAY
TWO-DIMENSIONAL ARRAY:
Like a 1-D array, a 2-D array is a collection of data cells, all the same type, which can be given a
single name. However, a 2D array is organized as a matrix with several rows and columns. The
size of the array is described as the number of rows and number of columns.
Example
Int matrix [2][3];
If you declare an array named as matrix of order 3*4 and apply for loops as stated below , the
compiler will put the entries in 3 rows and 4 columns.
Coding:
#include <iostream>
using namespace std;
main()
{
int arry[2][3] = { {18,101,30},{44,85,60} };
for(int i=0;i<2;i++) {
for(int j=0;j<3;j++) {
return 0;
}
OUTPUT:
Uses of Two-Dimensional Array
We can use 2D array to encode information about an image. For example, grayscale image.
A two-dimensional array can also be used to store objects, which is especially convenient for
programming sketches that involve some sort of "grid" or "board."
Often data come naturally in the form of a table, e.g., spreadsheet, which need a two-
dimensional array.
We can use 2D array to do the work of reading scores, getting the weights, computing the
weighted averages, printing scores, averaging each set of scores, and printing the averages.
Task #3:
Distinguish between 1-D array and 2-D array.
Task #4:
Write a program that initializes a two-dimensional array of 2 rows and 3
columns and then displays its values.
CODING:
#include <iostream>
using namespace std;
main()
{
int arry[2][3] = { {18,101,30},{44,85,60} };
for(int i=0;i<2;i++) {
for(int j=0;j<3;j++) {
return 0;
}
PROGRAM:
OUTPUT:
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.
CODE:
#include <iostream>
using namespace std;
main()
{
int arry[2][4];
return 0;
}
PROGRAM:
OUTPUT:
Task #6 :
Write a program that declaresa two-dimensional array of 2 rows and 4
columnsand then display the minimum and maximum number in the
array.The values of array should be user defined.
CODING:
#include <iostream>
using namespace std;
main()
{
int arry[2][4];
int max=arry[0][0],min=arry[0][0];
for(int i=0;i<2;i++) {
for(int j=0;j<3;j++) {
cout <<"Enter Array["<<i<<"]["<<j<<"] = ";
cin>>arry[i][j]; }
}
for(int i=0;i<2;i++) {
for(int j=0;j<3;j++) {
if ( max<arry[i][j])
max=arry[i][j];
else min<arry[i][j];
min=arry[i][j]; }
}
return 0;
}
PROGRAM:
OUTPUT:
Task #7:
Write a program that takes the values of matrix from user and then tells
wither the matrix is symmetric or non-symmetric(two-dimensional array). The
matrix should be of 4 rows and 4 columns.
CODING:
#include<iostream>
using namespace std;
main()
{
int a[4][4];
int i,j,temp;
int count=0;
cout<<"Enter Elements of [4*4] Matrix\n"<<endl;
for(i=0;i<4;i++) {
for(j=0;j<4;j++) {
cout <<"Enter Element of ["<<i+1<<" Row]["<<j+1<<" Column] = ";
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;
}
cout<<"\nThe Entered Matrix:";
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<<"\nIs Symmetric";
else
cout<<"\nIs Not Symmetric, ("<<count<<") Values are not Matched ";
return 0;
}
PROGRAM:
OUTPUT:
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.
CODING:
#include<iostream>
using namespace std;
main()
{
int i,j,r,c;
cout << "Enter number of rows: ";
cin>>r;
cout << "Enter number of columns: ";
cin>>c;
int a[r][c], b[r][c], sum[r][c];
cout<<"\nEnter Elements of 1st Matrix (A)\n"<<endl;
for( i=0;i<r;i++) {
for( j=0;j<c;j++) {
cout <<"Enter Element of ["<<i+1<<" Row]["<<j+1<<" Column] = ";
cin>>a[i][j]; }
}
cout<<"\nEnter Elements of 2st Matrix (B)\n"<<endl;
for( i=0;i<r;i++) {
for( j=0;j<c;j++) {
cout <<"Enter Element of ["<<i+1<<" Row]["<<j+1<<" Column] = ";
cin>>b[i][j]; }
}
for(i = 0; i< r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
return 0;
}
PROGRAM:
OUTPUT:
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.
CODING:
#include<iostream>
using namespace std;
main()
{
int sumr=0, sumc=0, i, j, r, c;
cout << "Enter number of rows: ";
cin>>r;
cout << "Enter number of columns: ";
cin>>c;
int a[r][c];
cout<<"\nEnter Elements of the Matrix: \n"<<endl;
for( i=0;i<r;i++) {
for( j=0;j<c;j++) {
cout <<"Enter Element of ["<<i+1<<" Row]["<<j+1<<" Column] = ";
cin>>a[i][j]; }
}
cout<<"\nThe Entered Matrix:";
for(i=0;i<r;i++) {
cout<<"\n";
for(j=0;j<c;j++)
cout<<a[i][j]<<" "; }
cout <<endl;
for (i = 0; i< r; i++) {
for (j = 0; j < c; j++) {
sumr += a[i][j]; }
cout<<"\nSum of "<<i+1<<" Row ="<<sumr;
sumr = 0; }
cout <<endl;
for (i = 0; i< r; i++) {
for (j = 0; j < c; j++) {
sumc += a[j][i]; }
cout<<"\nSum of "<<i+1<<" column ="<<sumc;
sumc = 0; }
return 0;
}
PROGRAM:
OUTPUT:
Task #10:
Write a program that take values of 4*4 matrix from user and display the sum
of diagonal elements of a matrix.
CODING:
#include<iostream>
using namespace std;
main()
{
int a[4][4];
int i,j,sum;
cout<<"Enter Elements of [4*4] Matrix\n"<<endl;
for(i=0;i<4;i++) {
for(j=0;j<4;j++) {
cout <<"Enter Element of ["<<i+1<<" Row]["<<j+1<<" Column] = ";
cin>>a[i][j]; }
}
cout<<"\nThe Entered Matrix:";
for(i=0;i<4;i++) {
cout<<"\n";
for(j=0;j<4;j++)
cout<<a[i][j]<<" "; }
cout<<endl;
for (i = 0; i< 4; i++) {
for (j = 0; j < 4; j++) {
if (i==j)
sum += a[i][j]; }
}
cout<<"Sum of the Diaognal of the Matrix is: "<<sum;
return 0;
}
PROGRAM:
OUTPUT: