0% found this document useful (0 votes)
59 views

Structured Programming Language: Array

An array allows storing multiple values in a single variable. Arrays are useful for storing collections of data like student marks. An array variable is declared with the data type, name, and size in square brackets. Array elements are stored contiguously in memory. Multidimensional arrays can store arrays of arrays to represent matrices. Arrays can be initialized during declaration by providing initial values inside curly braces. Loops are used to input or output array elements by their indices.

Uploaded by

Shahed Hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Structured Programming Language: Array

An array allows storing multiple values in a single variable. Arrays are useful for storing collections of data like student marks. An array variable is declared with the data type, name, and size in square brackets. Array elements are stored contiguously in memory. Multidimensional arrays can store arrays of arrays to represent matrices. Arrays can be initialized during declaration by providing initial values inside curly braces. Loops are used to input or output array elements by their indices.

Uploaded by

Shahed Hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

CSI 124

Structured Programming Language

Lecture 07
Array
Why Arrays

• No doubt, this program will print the value of x as 10


• Because when a value 10 is assigned to x, the earlier value of
x, i.e. 5, is lost
• ordinary variables (the ones which we have used so far) are
capable of holding only one value at a time
Why Arrays
• However, there are situations in which we would want to
store more than one value at a time in a single variable
• suppose we wish to arrange the percentage marks
obtained by 100 students in ascending order. In such a
case we have two options to store these marks in
memory:
i) Construct 100 variables, each variable containing
one student’s marks.
ii) Construct one variable capable of storing or
holding all the hundred values.
Why Arrays
• the second alternative is better
• it would be much easier to handle one
variable than handling 100 different variables
• Moreover, there are certain logics that cannot
be dealt with, without the use of an array
Introducing array
• An array is a group of related data items that share a
common name.
• C Array is a collection of variables to the same data
type. An array is used to store a collection of data, but
it is often more useful to think of an array as a
collection of variables of the same type.
• It is a group of memory locations related by the fact
that they all have the same name.
• For example: int num[10];
• The individual values are called elements.
Introducing array
• Array might be belonging to any of the data types
• Array size must be a constant value.
• Always, Contiguous (adjacent) memory locations are used
to store array elements in memory.
• The lowest address corresponds to the first element and
the highest address to the last element.
Declaring array variables
• Array variables are declared identically to variables of their
data type, except that the variable name is followed by one
pair of square [ ] brackets for each dimension of the array.
data_type array_name [array_size]
• This is called a single-dimensional array. The arraySize must
be an integer constant greater than zero and type can be
any valid C data type. For example, to declare a 10-element
array called balance of type float, use this statement-
float balance [10]
Declaring array variables
• Syntax:
Type variable_name[size];
• Example:
– float height[50];
– int group[10];
– char name[10];
• int num[10];
– num[0] references the first element in the array.
– num[9] references the last element in the array.
Initialization of array during declaration
• int num[6] = { 2, 4, 12, 5, 45, 5 } ;
• The number of values between braces { } cannot be
larger than the number of elements that we declare
for the array between square brackets [ ].
• int num[ ] = { 2, 4, 12, 5, 45, 5 } ;
• If you omit the size of the array, an array just big
enough to hold the initialization is created.
• float num[ ]={2.3, 5.6, 32.6};
Initialization of array during declaration
• char name[]={‘a’,’u’,’v’,’i’,’\0’};
• Some exceptions:
– int num[3]={0,0};
• Auto insert 0 to the remaining index.
– int num[3]={0,0,0,0};
• If the initializing value is more than the size then it
would cause a syntax error.
The length of array
• Once array is created, its size is fixed. It cannot be changed.
• For example: int arr[10];
– You cannot insert any number to arr[11], because arr[11] is not
initialized.
– int num[4]={2,3,4,5};
– int num[4];
• num[0]=2;
• num[1]=3;
• num[2]=4;
• num[3]=5;
One Dimensional Array
• C stores one dimensional
array in one contiguous
memory location with first
element at the lower
address
• an array named a of 10
elements can occupy the
memory as follows-
One Dimensional Array

• an array can also be initialized by following the declaration with


an equal sign and a comma separated list of values within a pair
of curly brace
One Dimensional Array

• a program that declares an array of 10 elements and initializes


every element of that array with 0
Simple program using Array
#include <stdio.h>
void main ()
{
int n[ 10 ];
int i;
for ( i = 0; i < 10; i++ )
{
scanf(“%d”,&n[i]);
}
for (i = 0; i < 10; i++ )
{
printf("Element[%d] = %d\n", i, n[i] );
}
}
Simple program using Array
#include <stdio.h>
void main()
{
int num[20], avg = 0,sum=0,x;
for (x=0; x<=19; x++)
{
printf("enter the integer number %d\n", x);
scanf("%d", &num[x]);
}
for (x=0; x<=19; x++)
{
sum = sum+num[x];
}
avg = sum/20;
printf("%d", avg);
}
Multidimensional arrays
Two – dimensional Arrays
A Two – dimensional Arrays m x n array A is a collection of m . n data elements
such that each element is specified by a pair of integers (such as J, K), called
subscripts.
The element of A with first subscript J and second subscript K will be denoted
by A[J, K]

Columns
1 2 3
1 A[1, 1] A[1, 2] A[1, 3]
Rows 2
A[2, 1] A[2, 2] A[2, 3]
3
A[3, 1] A[3, 2] A[3, 3]

Fig: Two dimensional 3 x 3 array A


Multidimensional arrays
• C programming language supports multi dimensional Arrays.
• Multi dimensional arrays have more than one subscript
variables.
• It is also called as matrix.
• Multi dimensional arrays are array of arrays.
• data_type array_name[size1][size2]...[sizeN];
• For Example:
Declaration of two dimensional integer array data_type
array_name [row size][column size]
     int board[8][8];
Multidimensional arrays
• A two dimensional matrix is an array of one dimensional
arrays.
• Two dimensional array requires two subscript variables or
indexes.
• One subscript represents the row index while other represent
column index of an element in matrix.
• Elements of a two dimensional array are stored in contiguous
memory location. First all elements of first row are store then
all elements of second row and so on.
Initialization of Two Dimensional Array

• Two dimensional arrays can be initialized by specifying elements


for each row inside curly braces. The following declaration
initializes a two dimensional matrix of 4 rows and 3 columns.
• int matrix[4][3] = { {1, 2, 3}, /* first row */
{4, 5, 6}, /* second row */
{7, 8, 9}, /* third row */
{10, 11, 12}, /* fourth row */ }; Similarly, we can initialize any
multi dimensional array.
A two dimensional matrix can be initialized without using any
internal curly braces as follows:
int matrix[4][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
How to print 2 Dimensional array
#include <stdio.h>
void main () {

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


int i, j;

for ( i = 0; i < 5; i++ ) {

for ( j = 0; j < 2; j++ ) {


printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
}
How to take input in 2dimensional array
#include<stdio.h>
void main()
{
int disp[3][5];
int i, j;
for(i=0; i<=2; i++)
{
for(j=0;j<=4;j++)
{
scanf("%d", &disp[i][j]);
}
}
}
Input and Output in 2D array

#include <stdio.h>
void main()
{
int i, j, test[2][3];
printf("Enter values: \n");
for(i = 0; i < 2; ++i) {
for (j = 0; j < 3; ++j)
{
scanf("%d", &test[i][j]);
}
}
printf("\nDisplaying values:\n");
for(i = 0; i < 2; ++i) {
for (j = 0; j < 3; ++j)
{
printf("%d\n",test[i][j]);
}
}
}
Thanks for your time and attention.

You might also like