You are on page 1of 20

Military Institute of Science and Technology

Department of Naval Architecture and Marine Engineering

Course Code: NAME 430


Course Title: Application of Computer Programming
for Optimization of Ship Design

▪ 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 :

type name [elements];


▪ type is a valid data type (like int, float …. )
▪ name is a valid identifier
▪ elements field (which is always enclosed in square brackets [ ]), specifices
how many of these elements the array has to contain.

3
Explanation of Syntax

type name [elements];

int num[7]

base type name of size of the


array array

4
Initialization of Array

int usham[5] = {11, 75, 22, 3, 4}

How to call an Array element?


5
Advantages of Array

▪ Arrays can store a large number of value with single name.


▪ Arrays are used to process many value easily and quickly.
▪ These values stored in an array can be sorted easily.
▪ The search process can be applied on arrays easily.

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:

data_type array_name[size of array];

▪ Eg:

int num[10];

8
Memory Representation

9
Arrays with Loops
#include<iostream>
using namespace std;

int main(){
int myarray[9];

cout << "Element - Value" << endl;

for (int x=0; x<9; x++){


myarray[x] = x+1;
cout << x << " -------- " << myarray[x] << endl;
}
}
10
Arrays in Calculation
#include<iostream>
using namespace std;

int main(){
int calc[5]={20,54,76,832,546};
int sum = 0;

for (int x=0; x<5; x++){


sum += calc[x];
cout << x << " -------- " << myarray[x] << endl;
}
}

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;

int num, ca; simp=wp[0]+wp[num-1];


float h, simp, vol,ton;
int main(){ for (int i=1;i<num;i=i+2){
cout << "Enter Total number of Data: "; simp+=(4*wp[i]);
cin >>num; }
float wp[num], draft[num]; for (int j=2; j<num-1; j=j+2){
for (int x=0;x<num;x++){ simp+=(2*wp[j]);
ca = x+1; }
cout << "Enter Draft " << ca << ":"; vol = (h*simp)/3;
cin >> draft[x]; ton = rho * vol;
}
cout << "The Tonne Displacement is " << ton;
for (int y=0;y<num;y++){ }
ca = y+1;
cout << "Enter Waterplane Area for Draft " << ca << ":";
cin >> wp[y];
} 16
Class Work
Conversion between Binary, Octal, Decimal Using C++

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

You might also like