IIT Delhi CS 123 - Programming In C UNIT-IV

Unformatted text preview:

Programming In C ARRAY UNIT IV An array is a collection of similar type of data items and each data item is called an element of the array The data type of the elements may be any valid datatype like char int or float The element of array share the same variable name but each element has different index number known as subscript Ex lets take an array variable age of size 100 and of type int The size of this array variable is 100 so it is capable of storing 100 integer values The individual elements of this array are Age 0 age 1 age 2 age 3 age 4 age 98 age 99 In C the subscript starts from zero so age 0 is the first element age 1 is the second element of array and so on Arrays can be single dimensional or multidimensional The number of subscript determines the dimension of array One Dimensional Array Declaration of one dimensional Array Like other simple variables arrays should also be declared before they are used in the program The syntax for declaration of an array is Data type array name size Here array name denotes the name of the array and it can be any valid C identifier data type is the data type of the elements of array The size of the array specifies the number of elements that can be stored in the array Here are some examples of array declarations int age 100 float salary 15 char grade 20 Here age is an array of type int which can store 100 elements of type int The array salary is a float type array size of 15 can hold values of type Page 1 of 15 BCA I Programming In C float and third one is a character type array of size 20 can hold characters Accessing 1 D Array Elements The elements of an array can be accessed by specifying the array name followed by subscript in brackets In C the array subscript start from 0 Hence if there is an array of size 5 the valid subscript s will be from 0 to 4 The last valid subscript is one less than the size of the array This valid subscript is known as the upper bound of the array and 0 is known as the int arr 5 Size of an array is 5 can hold five integer elements Here 0 is the lower bound and 4 is the upper bound of the array arr lower bound of the array Ex The elements of this array are arr 0 arr 1 arr 2 arr 3 arr 4 Processing 1 D Array 1 Reading values in arr for i 0 i 10 i Scanf d arr i 2 Displaying values of arr for i 0 i 10 i Printf d arr i Initialization of 1 D array Variables can be assigned values during declaration like the following example Int x 7 Arrays can be initialized in the same manner However since an array has multiple elements braces are used to denote the entire array of values and commas are used to separate the individual values assigned to the elements in the array initialization statements as shown a int A 10 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 Values stored in the array elements Page 2 of 15 BCA I Programming In C 0 1 2 3 4 5 6 7 8 9 index values of array elements b double a 5 3 67 1 21 5 87 7 45 9 12 Automatic sizing while initializing the size of a one dimensional array can be omitted as shown int arr 3 1 5 7 9 Here the C compiler will deduce the size of the array from the From above initialization statement the size of the array is initialization statement deducted to be 5 Two Dimensional Array in C 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 However 2D arrays are created to implement a relational database lookalike data structure It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required Declaration of two dimensional Array The syntax to declare the 2D array is given below data type array name rows columns Consider the following example int twodimen 4 3 Here 4 is the number of rows and 3 is the number of columns Processing 2 D Array Ex int a 9 5 1 Reading values in a for i 0 i 9 i for j 0 j 5 j scanf d a i j Page 3 of 15 BCA I Programming 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 Two dimensional array example include stdio h int arr 4 3 1 2 3 2 3 4 3 4 5 4 5 6 for j 0 j 3 j printf arr d d d n i j arr i j 2 Displaying values of a for i 0 i 9 i for j 0 j 5 j printf d a i j Initialization of 2 D Array int main int i 0 j 0 for i 0 i 4 i return 0 Array Of Strings character 0 Strings are defined as an array of characters The difference between a character array and a string is the string is terminated with a special Page 4 of 15 BCA I Programming In C Declaration of strings Declaring a string is as simple as declaring a one dimensional array Below is the basic syntax for declaring a string char str name size In the above syntax str name is any name given to the string variable and size is used to define the length of the string i e the number of characters strings will store Please keep in mind that there is an extra terminating character which is the Null character 0 used to indicate the termination of string which differs strings from normal character arrays Initializing a String A string can be initialized in different ways We will explain this with the help of an example Below is an example to declare a string with name as str and initialize it with RaisoniCollege 1 char str RaisoniCollege 2 char str 50 RaisoniCollege 3 char str R a i s o n i C o l l e g e 0 4 char str 16 R a i s o n i c o l l e g e 0 Pointers The pointer in C language is a variable which stores the address of …


View Full Document

IIT Delhi CS 123 - Programming In C UNIT-IV

Documents in this Course
Load more
Download Programming In C UNIT-IV
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming In C UNIT-IV and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming In C UNIT-IV 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?