Professional Documents
Culture Documents
▪ Topics –
Array in C++, Array with Loops, Array Calculation, Array as Argument, MD Array
1
What is ARRAY?
▪ An Array is a group of consecutive memory locations with same name and
data type.
▪ Simple variable is a single memory location with unique name and a type.
But an Array is collection of different adjacent memory locations. All these
memory locations have one collective name and type.
▪ The memory locations in the array are known as Element of array. The total
number of element in the array is called Length.
▪ The element of array is access with reference to its position in array, that is
called index or subscript.
2
Declaration of an ARRAY
▪ Like regular variable, an array must be declared before it is used. A typical
declaration for array in C++ is :
3
Explanation of Syntax
int num[7]
4
Initialization of Array
6
Types of Array
▪ Single Dimensional Array
▪ Two Dimensional Array
▪ Multi dimensional Array
7
One Dimensional Array
▪ A one dimensional array is one in which one subscript/indices specification is
needed to specify a particular element of array.
▪ Declaration:
▪ Eg:
int num[10];
8
Memory Representation
9
Arrays with Loops
#include<iostream>
using namespace std;
int main(){
int myarray[9];
int main(){
int calc[5]={20,54,76,832,546};
int sum = 0;
11
Arrays as Argument
#include<iostream>
using namespace std;
void printArray(int myArray[], int sizeOfArray);
int main(){
int mango[3]={22,11,45};
int juice[5]={23,45,567,21,22};
printArray(mango,3);
}
void printArray(int myArray[ ], int sizeOfArray){
for (int x=0;x<sizeOfArray;x++){
cout << myArray[x]<<endl;
}
} 12
Two Dimensional Array
▪ A 2-D array is an array in which each element is itself an array.
▪ i.e. int num[4][3]
13
Multi Dimensional Array
▪ An array with dimensions more than two. The maximum limit of array is
compiler dependent.
▪ Declaration:
data_type array_name[a][b][c][d][e]…….[n];
▪ Array of 3 or more dimensional are not often use because of huge memory
requirement and complexity involved.
14
Class Work
The areas of a ship’s water plane are as follows:
Draft (m) 0 1 2 3 4
Area of WP (m2) 650 660 662 661 660
Write a program in C++ to find the ship’s displacement in tonnes when floating in salt
water at 4 metres draft by taking WPA values from user.
15
//Applicable for Simpson's first rule cases only
#include<iostream>
#define rho 1.025 h = draft[num-1]/(num-1);
using namespace std; cout << "Spacing = " << h <<endl;
17
Decimal to Any: (Change 2 to 8 for octal)
18
Any to Decimal: (Change 2 to 8 for octal)
19
Octal to Binary (replace 8 with 2 and 2 with 8 for vice versa)
20