OOP CPP Programming language Array by Nadir Nadeem: Part-4

OOP CPP Programming language Array by Nadir Nadeem: Part-4

Array in c/cpp(c++):

Array:

  • Store more than one values in a single variable.
  • Array index start from zero

Syntax:

Datatype variable[]; à int x[]

Datatype variable[size]; à int x[4];

2D Array:

Syntax:

Datatype variable[][]; à int x[][];

Datatype variable[size][size]; à int x[3][4];

Example:

int x[]={12,461,753,6};

x:

values

12

461

753

6

index

0

1

2

3

 

Example:

 

17,18,19,20,21,22,23,24,25 è with also while loop

26,27,28,29,30,31,32,33,34

Ass#35,36: print variable value in [] à for and while

x[0]= 12

x[1]=461

Array:

Syntax:

Datatype varisble[];

Datatype variable[size in int];

Datatype variable[]={v1,v2,v3…};

Int x[]={10,20,30,40};

X:

values

10

20

30

40

Index

0

1

2

3

 

Example:

Example:

Example:

Example with loop:

Example:

Example:

2D Array:

Syntax:

Datatype variable[size][size];

Datatype variable[size][size]={{},{},…};

Example:

Example: with loop

 

Example:

Ass#38: Sum of required values:

How many values you want to sum: = 4

Enter v1= 3

Enter v2 = 2

Enter v3 = 2

Enter v4 = 4

3,2,2,4 values have sum = 11

Ass#40: Calculate the average

How many values you want to calculate average: = 4

Enter v1= 3

Enter v2 = 3

Enter v3 = 3

Enter v4 = 3

The average of above values := 3