Unit 3-Part 1 Arrays
Unit 3-Part 1 Arrays
ARRAY
&
STRUCTURE
• Arrays
SYLLABUS
• One dimensional arrays
• Two dimensional arrays
• Multi dimensional arrays
• Strings: Declaring and Initializing
strings
• Operations on strings
• Arrays of strings
• Structures
• Nested structure
• Array of Structure
• Unions
• Typedef
• enum
ARRAY
• In an Array values of same type are stored. An array is a group of memory locations related by the fact that they all
have the same name and same type. To refer to a particular location or element in the array we specify the name to
the array and position number of particular element in the array.
• An array
– a single name for a collection of data values
– all of the same data type
– subscript notation to identify one of the values
• It is a group of variables of similar data types referred to by a single element.
• Its elements are stored in a contiguous memory location.
• The size of the array should be mentioned while declaring it.
• Array elements are always counted from zero (0) onward.
• Array elements can be accessed using the position of the element in the array.
• Accessing each of the values in an array
– Usually a for loop
• A collection of same type data, 1D, 2D
CONTI….
• Consider the following issue:
"We have a list of 1000 students' marks of an integer type. If using the basic data type (int), we will
declare something like the following…“
int studMark0, studMark1, studMark2, ..., studMark999;
Can you imagine how long we have to write the declaration part by using normal variable declaration?
int main(void)
{
int studMark1, studMark2, studMark3, studMark4, …, …, studMark998,
stuMark999, studMark1000;
…
…
return 0;
}
ARRAY DECLARATION
• This will reserve 1000 contiguous memory locations for storing the students’ marks.
• Syntax:
<array_element_datatype> <array_name>[<array_size>]
Ex. int Ar[10];
• Here array_element_data_type define the base type of the array, which is the type of each element
in the array.
• array_name is any valid C / C++ identifier name that obeys the same rule for the identifier naming.
• array_size defines how many elements the array will hold.
SUBSCRIPTING
• Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
• To access an individual element we must apply a subscript to array named Ar.
– A subscript is a bracketed expression.
• The expression in the brackets is known as the index.
– First element of array has index 0.
• Ar[0]
– Second element of array has index 1, and so on.
• Ar[1], Ar[2], Ar[3],…
– Last element has an index one less than the size of the array.
• Ar[9]
• Incorrect indexing is a common error.
ARRAY INITIALIZATION
• We can explicitly initialize arrays at the time of declaration.
• Syntax:
data_type array_name[Size]={value1, value2,……..valueN};
• Value1, value2, valueN are the constant values known as initializers, which are assigned to the
array elements one after another.
• Example:
int marks[5]={10,2,0,23,4};
• The values of the array elements after this initialization are:
marks[0]=10, marks[1]=2, marks[2]=0, marks[3]=23, marks[4]=4
ONE DIMENSIONAL ARRAYS
• Examples of the one-dimensional array declarations,
• The first example declares two arrays named xNum and yNum of type int. Array xNum can store up to 20
integer numbers while yNum can store up to 50 numbers.
• The second line declares the array fPrice of type float. It can store up to 10 floating-point values.
• fYield is basic variable which shows array type can be declared together with basic type provided the type is
similar.
• The third line declares the array chLetter of type char. It can store a string up to 69 characters.
• Why 69 instead of 70? Remember, a string has a null terminating character (\0) at the end, so we must reserve
for it.
ACCESSING ARRAY ELEMENTS
• An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array.
• For example −
double salary = salary[9];
• The above statement will take the 10th element from the array and assign the value to salary
variable.
• The following example Shows how to use all the three above mentioned concepts viz.
declaration, assignment, and accessing arrays
EXAMPLE: DECLARATION, ASSIGNMENT, AND ACCESSING ARRAYS
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j; /* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
} /* output each array element's value */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
Output:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
C PROGRAM TO FIND MAXIMUM ELEMENT IN ARRAY
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
return 0;
}
Enter the number of elements in array
4
Enter 4 integers
11
22
33
44
Maximum element is present at location 4 and it's value is 44.
TWO DIMENSIONAL ARRAYS
• The two-dimensional array can be defined as an array of arrays.
• The 2D array is organized as matrices which can be represented as the collection of rows and
columns.
• It provides ease of holding the bulk of data at once which can be passed to any number of functions
wherever required.
CONTI….
void main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; //traversing 2D array
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
}
• Initialization of 2D Array in C
• In the 1D array, we don't need to specify the size of the array if the declaration and
initialization are being done simultaneously.
• However, this will not work with 2D arrays.
• We will have to define at least the second dimension of the array.
• The two-dimensional array can be declared and defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
#include <stdio.h>
int main ()
{ /* an array
with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
for ( i = 0; i < 5; i++ ) /* output
each array element's value */
{
for ( j = 0; j < 2; j++ )
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}
TWO-DIMENSIONAL ARRAY-INITIALIZATION
INITIALIZATION: PROCESSING:
• 2-D arrays can be initialized in a way similar to • For processing of 2-D arrays we need two nested for
loops. The outer loop indicates the rows and the inner
1-D arrays. loop indicates the columns.
• Example: • Example:
int int a[4][5];
m[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12 a) Reading values in a :
}}; for(i=0;i<4;i++)
for(j=0;j<5;j++)
• The values are assigned as follows: scanf(“%d”,&a[i][j]);
m[0][0]:1 m[0][1]:2 m[0][2]:3
m[1][0]:4 m[1][1]:5 m[1][2]:6 b) Displaying values of a :
for(i=0;i<4;i++)
m[2][0]:7 m[2][1]:8 m[2][2]:9
for(j=0;j<5;j++)
m[3][0]:10 m[3][1]:11 m[3][2]:12
printf(“%d”,a[i][j]);
MULTIDIMENSIONAL ARRAY
• An array of 2-dimensional arrays are treated as multidimensional arrays.
• Example:
int a[2][3][4];
• Here a represents two 2-dimensional arrays and each of these 2-D arrays contains 3 rows and
4 columns.
• The individual elements are:
a[0][0][0], a[0][0][1], a[0][0][2], a[0][1][0]…………a[0][1][2], a[1][0][0],
a[1][0][1], a[1][0][2], a[1][1][0]…………..a[1][3][2]
• The total no. of elements in the above array is 2*3*4=24.
• Initialization of 3D Array:
int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3,
5}, {3, 1, 4, 9}}};
Multidimensional Array
Multidimensional Array