Unit III Array and Types of Array pointers
Unit III Array and Types of Array pointers
ARRAYS
• An array is a fixed size sequenced collection of elements of
the same data type.
• Examples
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
element element element element element element element element element element
• To access all the elements of the array, you must use a loop.
That is, we can access all the elements of the array by
varying the value of the subscript into the array.
int i, marks[10];
for(i=0;i<10;i++)
marks[i] =20;
Types of Arrays
• One-dimensional arrays
• Two-dimensional arrays
• Multidimensional arrays
One Dimensional Array
99 67 78 56 88 90 34 85
99 67 78 56 88 90 34 85
Here,
lower_bound = 0, upper_bound = 7
Therefore, length = 7 – 0 + 1 = 8
• At Compile time
• At run time
One Dimensional Array
• syntax:
• Ex:
• int total[5]={1,2,3,4,5};
Write a Program to Read and Display N Numbers Using an
Array
#include<stdio.h> for(i=0;i<n;i++)
#include<conio.h> {
{ scanf(“%d”,&arr[i]);
}
int i=0, n, arr[20];
printf(“\n The array elements are ”);
clrscr();
for(i=0;i<n;i++)
printf(“\n Enter the number of elements : ”);
{
scanf(“%d”, &n);
printf(“Arr[%d] = %d\t”, i, arr[i]);
}
return 0;
}
Two Dimensional Arrays
Multi Dimensional Array
• At times we need to store the data in form of tables or matrices. For this, we can
use the two dimensional arrays.
• This array is specified by using two subscripts where one subscript is denoted as
the row and the other as the column.
Example Code
• Let us see how the elements of a 2D array are stored in a row major
order. Here, the elements of the first row are stored before the elements of
the second and third rows
• when we store the elements in a column major order, the elements of the first
column are stored before the elements of the second and third column.
Initialization of Two Dimensional Array
We use the following general syntax for declaring and initializing a two dimensional array
with specific number of rows and coloumns with initial values.
datatype arrayName [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ;
Example Code
int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;
The above declaration of two-dimensional array reserves 6 contiguous memory locations
of 2 bytes each in the form of 2 rows and 3 columns. And the first row is initialized with
values 1, 2 & 3 and second row is initialized with values 4, 5 & 6.
We can also initialize as follows...
Example Code
int matrix_A [2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Example: Initialization of 2D array
int score [3] [2] ={50, 60, 70, 95, 3, 36};
The elements will be seen in the following format once they are initialized.
The above example has been divided into three rows and two columns.
• The declaration statement given below is valid.
In a c programming language, to access elements of a two-dimensional array we use array name followed by row index value and column
index value of the element that to be accessed. Here the row and column index values must be enclosed in separate square braces. In
case of the two-dimensional array the compiler assigns separate index values for rows and columns.
We use the following general syntax to access the individual elements of a two-dimensional array...
Example Code
matrix_A [0][1] = 10 ;
In the above statement, the element with row index 0 and column index 1 of matrix_A array is assigned with value 10.
.
Simple program for printing the elements of 2D array.
#include <stdio.h>
void main()
{
int score[3][2]= {10,20,30,40,50,60};
int i,j;
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d\t",score[i][j]);
}
}
}
In a small company there are five salesmen. Each salesman is supposed to sell three
products. Write a program using a 2D array to print (i) the total sales by each salesman
and (ii) total sales of each item.