Unit 5 Arrays
Unit 5 Arrays
Arrays: 5
Introduction to Arrays
One-Dimensional Arrays
Two-Dimensional Arrays
Arrays and Functions
Strings:
Introduction to Strings
String Input/Output
String Operations with and without built-in functions
1
Arrays in C:
⮚ An array is homogenous: Every value within the array must share the
same data type.
⮚ In other words, an int array can only hold integer values, not doubles.
2
Application of Arrays
C array is beneficial if you have to store similar elements.
For example:
⮚ if we want to store the marks of a student in 6 subjects, then we don't need to
define different variables for the marks in the different subject.
⮚ Instead of that, we can define an array which can store the marks in each
subject at the contiguous memory locations.
3
Advantage of C Array:
Disadvantage of C Array:
Fixed Size: Whatever size, we define at the time of declaration of the array, we
can't exceed the limit. So, it doesn't grow the size dynamically.
4
Properties of Array
⮚Each element of an array is of same data type and carries the same size, i.e., int
= 4 bytes.
⮚Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
⮚Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of the
data element.
5
Types of arrays:
⮚In C, arrays can be classified based on how the data items are arranged
for human understanding.
⮚Arrays are broadly classified into three categories,
1. One Dimensional Arrays
2. Two Dimensional Arrays
3. Multi Dimensional Arrays
6
How to create an array:
7
One Dimensional Arrays:
9
Option 1: Initializing all memory locations:
⮚ If you know all the data at compile time, you can specify all your data
within brackets:
int temp [5] = {75, 79, 82, 70, 68};
⮚ During compilation, 5 contiguous memory locations are reserved by the
compiler for the variable temp and all these locations are initialized as
shown below.
⮚If the size of integer is 2 bytes, 10 bytes will be allocated for the
10
variable temp.
Option 2: Initialization without size:
⮚ If you omit the size of an array, but specify an initial set of data, then
the
compiler will automatically determine the size of an array.
int temp [] = {75, 79, 82, 70, 68};
⮚ In the above declaration, even though you have not specified exact
number of elements to be used in array temp, the array size will be set
with the total number of initial values specified.
11
Option 3 Partial Array Initialization:
12
Initializing One Dimensional Arrays (Cont…)
⮚ This will initialize every element within the array to 0 as shown below.
13
Accessing elements of one dimensional array(Cont…)
//Example for accessing array elements.
#include<stdio.h>
main() {
int arr[10];
int i = 0;
for(i=0;i<sizeof(arr);i++) {
arr[i] = i; // Initializing each element separately
}
int j = arr[5]; // Accessing the 5th element of integer array arr
//and assigning its value to integer 'j'.
printf(“Value at 5th location is: %d”,j);
}
⮚ As we can see above, the 5th element of an array is accessed as ‘arr[5]‘.
#include <stdio.h>
void main()
{
int a[10];
int i, size, total=0;
printf("\n Enter the size of the array : ");
scanf("%d", &size);
printf("\n Enter the elements of an array : ");
for (i = 0; i < size ; i++)
scanf("%d",&a[i]);
for (i = 0; i < size ; i++)
total += a[i];
printf(“Sum of all array elements: %d", total);
}
15
Two Dimensional Arrays:
⮚ Two-dimensional array are those type of array, which has finite number
of rows and finite number of columns.
16
Declaration of Two Dimensional Arrays:
Example:
int c[2][3] = {
{1,3,0},
{-1,5,9}
};
OR
int c[][3] = {
{1,3,0},
{-1,5,9}
};
OR
20
//Program to print sum of elements of a matrix.
#define M 3 /* Number of rows */
#define N 4 /* Number of columns */
main() {
, int a [ M ] [ N ], i, j, sum = 0;
for ( i = 0; i < M; ++i ) {
for (j = 0; j < N, ++j ){
scanf (%d”, &a [i] [j]);
}
}
for ( i = 0; i < M; ++i ) {
for (j = 0; j < N, ++j ) {
printf(“a [ %d ] [ %d ] = %d “, i, j, a[ i ] [ j ]);
}
printf (“\n”);
}
for ( i = 0; i < M; ++i ) {
for (j = 0; j < N, ++j ) {
sum += a[ i ] [ j ];
}
printf(“\nsum = %d\n\n”);
}
21
}
Multi Dimensional Arrays:
22
Example:
int table[3][5][4] = {
{
{000,001,002,003},
{010,011,012,013},
{020,021,022,023},
{030,031,032,033},
{040,041,032,043}
},
{
{100,101,102,103},
{110,111,112,113},
{120,121,122,123},
{130,131,132,133},
{140,141,142,143}
},
{
{200,201,202,203},
{210,211,212,213},
{220,221,222,223},
{230,231,232,233},
{240,241,242,243}
}
}; 23
Functions with Arrays:
⮚ Like the values of variable, it is also possible to pass values of an array
to a function.
⮚ For example, the function call findMax(a, n); will pass all the elements
contained in the array a of size n.
25
//Program to read an array of elements and find max value.
#include<stdio.h>
int findMax(int[],int); int findMax(int x[],int size)
void main() {
{ int temp;
int a[10], n ,i , max; temp=x[0];
printf(“\n Enter the size of the array “); for(i=1;i<size; i++)
scanf(“%d”,&n); {
printf(‘\n Enter the elements of the array : “); if(x[i]>temp)
for(i=0;i<n;i++) {
scanf(“%d”,&a[i]); temp=x[i];
max=findMax(a, n); }
printf(“\n The Maximum value =%d”, max); }
} return temp;
}
26