c Programming-chapter 06
c Programming-chapter 06
PRESENTED BY
SHEYONA G
CHAPTER-06
Arrays
Arrays
Array is a fixed-size sequenced collection of related data items that share a common name.
It is simply a grouping of similar type of data
Array is a derived data type
A list of items can be given one variable name using only one subscript and such a variable is called a
single subscripted variable or a one-dimensional array
Declaration of One-dimensional arrays:
data type variable-name[size];
It declares the name as a character array(string) variable that can hold a maximum of 10 characters.
Initialization of One-dimensional arrays:
An array can be initialized at two stages:
At Compile Time
At Run Time
datatype array-name [size] = { list of values };
The values in the list are separated by commas.
Example 1: int number [3] = { 1,5,6 };
Example 2: float total [5] ={0.0, 15.75, -10}; // the remaining two elements will be considered as zero
Example 3: int counter [ ] = {1, 1, 1, 1};// here the counter array initialized with four elements(1’s)
Example 4: char name [ ] = { ‘J’ , ’O’ , ’H’ , ’N’ , ’\0’ };
Or char name [ ] = “JOHN”;
Example 5: char city [5] = { ‘B’ }; will initialize the first element to ‘B’ and the remaining four to NULL.
Run Time Initialization:
example:
int table[2][3];
Representation of a two-dimensional array in memory:
int table[2][3] = { 0, 0, 0, 1, 1, 1 };
Initialize the elements of the first row to zero and the second row to one.
The initialization is done row by row. That is, int table[2][3] = { { 0, 0, 0 }, { 1, 1, 1 } }; Here the elements of
each row are separating by braces and each pair of braces should be separated by comma.
The initialization is done in the form of a matrix.
int table[2][3] = {
{ 0, 0, 0 },
{ 1, 1, 1 }
};
Multidimensional arrays
Declaration of multi-dimensional arrays:
The general form of a multi-dimensional array declaration is :
In C language, array is a ________ data type. fundamental primitive datatype user defined derived data
datatype datatype type
The_______is the syntax of declaring one dimensional array. datatype arrayname arrayname[si datatype [size];
arrayname[size]; datatype [size]; ze];
int a[10]; array can store maximum _____number of elements. 10 20 9 1
The process of arranging the array elements in ascending or Sorting searching finding deletion
descending order is known as
In C language, an array can be initialized at ________ both compile time compile time only run time only declaration
and run time time only
Which amongst the following process is used for allocating memory static memory dynamic memory stored function
at compile time in C language. allocation allocation procedures
In array we can access individual elements by using ________ scripts elements subscripts both a & c
The process of finding the location of the specified element in a list is declaration compilation initialization searching
called _________
1) What is an array? What are the different types of arrays? Write the general form of declaration of different types
of arrays
2) Write one example for initializing one dimensional integer array at compile time
3) What is the syntax of declaring multi dimensional arrays?
4) Write one example for initializing two dimensional integer arrays at compile time
5) Give the number of elements and size in memory for the following
a)float areas[7][8];
6) Write a C program to create one dimensional integer array and store some elements at run time and display the
elements.
7) Write a C program to create a one dimensional integer array and store some elements and display only the even
numbers.