Professional Documents
Culture Documents
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.
ME DEPARTMENT
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
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
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.