1-Dimensional Arrays:

Introduction:

An array is defined by its name size and type of variables it stores. Missing any of these will result in an error. An array can be initialized along with declaration for any array initialization it is required to place the elements separated by commas enclosed within braces.

int a[5]={1,2,3,4,5};

It is possible to leave the array size open. The compiler will count array size.

int b[ ]={9,8,7,6,5};

1-D array using for loop:

Repetition loops like for loops are very helpful for this purpose especially because an array is a consecutive memory block.

Example 1:

Untitled

q1.

Example 2:

q2

q2..png

Assignment 1:

Q 1:Determine the maximum value in couple of numbers,

Code:

q4

q4..png

2-Dimensional Arrays

Introduction:

This method is very efficient to declare larger dimensions. An array of size 3*3 means 3 rows and 3 columns. The array [3][3] will have the locations a[0][0],a[0][1],a[0][2],a[1][0],a[1][1],a[1][2],a[2][0],a[2][1],a[2][2]. 2-D arrays are normally used to model matrices for this thing we use nested for loop.

Nested For Loop:

One for loop can be nested inside another for loop. This happens when there is a need to control the rows and columns of the output.

Example:

q5.png ‘

q5.

Example:

q6

q6..png

Assignment 2:

Q 1: In order to initialize and display the 2-D array

Code:

q7

q7..png

Q 2:Determine the transpose of a 3*3 matrix

Code:

q1.png

q1..png

Q 3:Determine the maximum number from 2*2 matrix.

Code:

q2.png

q2.

 

Previous page

-Basics of Programming

https://www.electricalengineering4u.com/fundamentals-of-programming/basics-of-programming/

Next page:

C++ Loops and Statements

https://www.electricalengineering4u.com/fundamentals-of-programming/c-loops-and-statements/