Similar to Array and String in C Arrays - Introduction • Need of Arrays • Characteristics of Arrays • Types of Arrays • Why indexing of arrays start at 0 (zero
Array and String in C Arrays - Introduction • Need of Arrays • Characteristics of Arrays • Types of Arrays • Why indexing of arrays start at 0 (zero
1.
ARRAYS AND STRINGS INC Kiranpal Singh Virk Assistant Professor Guru Nanak Khalsa College Yamuna Nagar kiranpal.virk@yahoo.com
2.
TOPICS • Arrays -Introduction • Need of Arrays • Characteristics of Arrays • Types of Arrays • Why indexing of arrays start at 0 (zero)
3.
NEED OF ARRAYS •An ordinary variable is capable of storing only one value at a time • However, there are situations in which we would be requiring to store more than one value at a time in a single variable
4.
NEED OF ARRAYS •e.g. Store the % marks obtained by 100 students • Two options to store the marks are: – Construct 100 variables to store % marks obtained by 100 different students i.e. Each variable containing one student‟s marks – Construct one variable (called a subscripted variable) capable of storing all the hundred marks
5.
NEED OF ARRAYS #include<stdio.h> voidmain() { int marks1, marks2, marks3, marks4, marks5; scanf(“%d”,&marks1); scanf(“%d %d %d”%d”,&marks2, marks3,marks4, marks5); }
ARRAY • Collection isordered – items added to the collection are maintained in the order it were added to the collection • Elements are homogeneous – elements are of same type, group or class
9.
ARRAYS IN C •Array is a collection of same type elements under the same variable identifier referenced by index number • Arrays – Are a sequence of memory locations referred to by a common name – Can store multiple values of the same data type • The individual data items are called elements of the array
ARRAYS IN C •Array data structure, an arrangement of items at equally spaced addresses in computer memory • Array data type, used in a programming language to specify a variable that can be indexed
13.
CHARACTERISTICS OF ARRAYS INC • Array is a subscripted variable • Random access via indexing operator [] • Indexing –starts at 0 –Ends at N-1 (where N is maximum the array size) • The name of the array refers to the address of the first element in the array
STATIC vs DYNAMIC •Static arrays have their sizes declared from the start and the size cannot be changed after declaration • Dynamic arrays that allow you to dynamically change their size at runtime, but they require more advanced techniques such as pointers and memory allocation.
16.
DECLARING SINGLE DIMENSION ARRAYS •SYNTAX: <data type> <variable name>[<dimension size>] – data type specifies the data type of the values that will be stored in the array – dimension size indicates the maximum number of elements that can be stored in the array • The name of the array refers to the address of the first element in the array
17.
DECLARING SINGLE DIMENSION ARRAYS •Example: <data type> <variable name>[<dimension size>] • This declares an array num of size 5 • Elements of the array are num[0], num[1], num[2], num[3] and num[4] • 0, 1, 2, 3 and 4 are called subscripts or indexes • The array name num refers to the address of num[0] i.e. first element of the array int num[5];
18.
INITIALISING ARRAYS • Initialisationof an array – Refers to assigning values to the elements of the array – Can happen along with declaration or after declaration
MULTI-DIMENSIONAL ARRAYS • Don‟t reallyexist in C • C allows “arrays of arrays” • Use one set of bracket for each dimension • Can be initialized with nested initializer lists
25.
1 2 3 4 56 MULTI-DIMENSIONAL ARRAYS • Consider the following 3 x 2 array: • C stores it linearly: 1 2 3 4 5 6 1 2 3 4 5 6
26.
MULTI-DIMENSIONAL ARRAYS 1 2 34 5 6 •The compiler interpret it as: an array of “3 arrays of 2 integers” 1st Array of 2 integers 2nd Array of 2 integers 3rd Array of 2 integers C allow “arrays of arrays”
27.
DECLARING ARRAYS • Example: <datatype> <variable name> [<dimension 1 size>] [<dimension 2 size>].... [<dimension N size>] • This declares an array num of size 3 x 2 • Elements of the array are num[0][0], num[0][1], num[1][0], num[1][1], num[2][0] and num[2][1] int num[3][2];
DECLARING STRING • Example: <datatype> <variable name>[<dimension size>] • Creates a character array str that can store a maximum of 9 characters • The last element of a character array must always be the null character or string terminator [0] • It indicates the end of the character array • The null character cannot be printed char str[10] ;
39.
INITIALISING ARRAYS char str[6]; str[0]= „I‟; str[1] = „N‟; str[2] = „D‟; str[3] = „I‟; str[4] = „A‟; Str[5] = „0‟; Both statements assign the name INDIA to the array str char str[ ] = “INDIA”; I N D I A „0‟
40.
PRINTING ARRAYS • Allelements of a character arrays can be printed using a single printf() function • The format code for printing a string is %s The above statement prints the value of the character array str The printf() function displays all the characters in the array until the null character „0‟ is reached printf(“%s”, str);
41.
INITIALISING ARRAYS 1000 10011002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 ; ; ; ‟; I N D I A „0‟
42.
EFFECT OF NULL CHARACTER„0‟ON STRING void main(){ char name[5]={„i‟,‟n‟,‟d‟,‟i‟,‟a‟}; char name1[5]=“india”; printf(“NAME = %s”,name); printf(“NAME1= %s”,name); } First Execution Second Execution
43.
STRING FORMAT WITH PRECISION main() { charname[6]={'I','N','D','I','A'}; printf("n%.2s",name); printf("n%.10s",name); printf("n%10.4s",name); printf("n%10s",name); printf("n%-10s",name); }
WHY ARRAY STARTS AT0 Address 100 101 102 103 104 105 str I N D I A „0‟ Index 0 1 2 3 4 5 char str[ ] = “INDIA”; printf(“%c”,str[3]); str[3] *(str+3) *(100+3) *(103) The name of the array refers to the address of the first element in the array
46.
WHAT IF ARRAY STARTSAT 1 Address 100 101 102 103 104 105 str I N D I A „0‟ Index 1 2 3 4 5 6 char str[ ] = “INDIA”; printf(“%c”,str[4]); str[4] *(str+4) *(100+4) *(104)
47.
SUGGESTED READING • BOOKS: –Yashwant Kanetkar, “Let Us C”, 5th Edition, BPB Publications – Chuck Allison, “Thinking in C”, Mindview Inc • ONLINE RESOURCES – Microsoft MSDN library