0% found this document useful (0 votes)
51 views4 pages

Experiment#13: Introduction To Arrays Objective

This document provides an introduction to arrays in C++, including multidimensional arrays. It defines single and multidimensional arrays, and explains how to declare, initialize, access elements of, and pass two-dimensional arrays as parameters to functions. It also lists three lab tasks involving performing operations on matrices represented as two-dimensional arrays, including summation, multiplication, and transposition.

Uploaded by

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

Experiment#13: Introduction To Arrays Objective

This document provides an introduction to arrays in C++, including multidimensional arrays. It defines single and multidimensional arrays, and explains how to declare, initialize, access elements of, and pass two-dimensional arrays as parameters to functions. It also lists three lab tasks involving performing operations on matrices represented as two-dimensional arrays, including summation, multiplication, and transposition.

Uploaded by

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

HITEC UNIVERSITY, TAXILA

FACULTY OF MECHANICAL ENGINEERING

ME DEPARTMENT

Experiment#13
Introduction to Arrays
Objective
An introduction with
1. Multidimensional Array.

Software Tools
1. Code Blocks

Theory
C++ Program
A computer program is a sequence of instructions that tell the computer what to do.

Arrays
Array is a collection of elements of same type referred using a unique name. Each element of an
array is stored in memory locations indexed from 0 through n number of its elements. The lowest
indexed will be the first element and highest indexed the last element of an array.
 Single / One Dimensional Array is an array having a single index value to represent the
arrays element.
 Multi-Dimensional Array is an array with two or more index values. It is also known as
array of arrays. A two dimensional array is also a multi-dimensional array.

Declaration of two-dimensional array


Type arrayName[numberOfRows][numberOfColumn];
For example,
int Sales[3][5];

Initialization of two-dimensional array


An two-dimensional array can be initialized along with declaration. For two-dimensional array
initialization, elements of each row are enclosed within curly braces and separated
by commas. All rows are enclosed within curly braces.

int A[4][3] = {{22, 23, 10},


{15, 25, 13},

Computing Fundamentals 1st Semester-ME HITEC UNI Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF MECHANICAL ENGINEERING

ME DEPARTMENT

{20, 74, 67},


{11, 18, 14}};

Referring to Array Elements


To access the elements of a two-dimensional array, we need a pair of indices: one for
the row position and one for the column position. The format is as simple as:
name[rowIndex][columnIndex]

Examples:
cout<<A[1][2]; //print an array element
A[1][2]=13; // assign value to an array element
cin>>A[1][2]; //input element

Using loop to input an two-dimensional array from user


int mat[3][5], row, col ;
for (row = 0; row < 3; row++)
for (col = 0; col < 5; col++)
cin >> mat[row][col];

Arrays as Parameters
Two-dimensional arrays can be passed as parameters to a function, and they are passed by
reference. When declaring a two-dimensional array as a formal parameter, we can omit the size of
the first dimension, but not the second; that is, we must specify the number of columns.

For example.
void print(int A[][3],int N, int M)
In order to pass to this function an array declared as:
int arr[4][3];
we need to write a call like this:
print(arr);
Here is a complete example:
#include <iostream>
using namespace std;
void print(int A[][3],int N, int M)
{
Computing Fundamentals 1st Semester-ME HITEC UNI Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF MECHANICAL ENGINEERING

ME DEPARTMENT

for (int R = 0; R < N; R++)


for (int C = 0; C < M; C++)
cout << A[R][C];
}
int main ()
{
int arr[4][3] ={{12, 29, 11},
{25, 25, 13},
{24, 64, 67},
{11, 18, 14}};
print(arr,4,3);
return 0;
}

Lab Tasks

1. Write a program that will find the sum of all the elements of given matrix B.

2. Write a program that will get two matrices A and B of size (R1, C1) and (R2, C2) from user
and perform following operation on two matrices.
 Point multiplication.
 Addition.
 Subtraction
 Division
Note: If any operation is not possible then proper error must be displayed.
3. Write a program that will get a matrix A of size (R1, C1) from user and perform following
operation on this matrix.
 Display this matrix
 Sum of Diagonal Elements.
 Product of Diagonal Elements.
 Sum of all Elements.
 Product of all Elements.
 Show either matrix is square or not.
 Count number of non-zero elements in matrix.
 Transpose of matrix
Computing Fundamentals 1st Semester-ME HITEC UNI Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF MECHANICAL ENGINEERING

ME DEPARTMENT

Note: If any operation is not possible then proper error must be displayed.

Computing Fundamentals 1st Semester-ME HITEC UNI Taxila

You might also like