Introduction to Array & Structure & Basic Algorithms.pptx
MrNikhilMohanShinde
15 views
16 slides
May 03, 2024
Slide 1 of 16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
About This Presentation
Introduction to Array & Structure & Basic Algorithms.pptx
Size: 136.38 KB
Language: en
Added: May 03, 2024
Slides: 16 pages
Slide Content
Array , Structure and Basic Algorithms UNIT III
Points to be covered Concept of Array Strings Structures Algorithms Complexity
Introduction to Array Array is a data structure that represents a collection of the same types of data by a common name . Compared to the basic data type ( int , float & char) it is an aggregate or derived data type . All the elements of an array occupy a set of contiguous memory locations. Why need to use array type? 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;
Arrays By using an array, we just declare like this , int studMark [1000]; This will reserve 1000 contiguous memory locations for storing the students’ marks.
Declaring Array When declaring arrays, specify Type of array Name Number of elements arrayType arrayName [ numberOfElements ]; Examples : int c[10]; float myArray [3000]; Declaring multiple arrays of same type int b[100],x[27];
Declaring Array For example, to declare an array of 30 characters, that construct a people name, we could declare, char cName [30 ]; Which can be depicted as follows, In this statement, the array character can store up to 30 characters with the first character occupying location cName [0] and the last character occupying cName [29]. - Note that the index runs from 0 to 29 . In C, an index always starts from 0 and ends with array's (size-1) .
I nitialization An array may be initialized at the time of declaration. Initialization of an array may take the following form, type array_name [size] = { a_list_of_value }; For example: int idNum [7] = {1, 2, 3, 4, 5, 6, 7 }; float fFloatNum [5] = {5.6, 5.7, 5.8, 5.9, 6.1 }; char chVowel [6] = {'a', 'e', ' i ', 'o', 'u', '\0 '}; Note the last character in chVowel is NULL character ('\0 ')
Initialization Example int Ar [10 ] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; Ar [3 ] = -1; 8 7 -1 9 Ar 4 3 2 5 1 4 5 6 3 2 8 9 7 1 8 7 6 9 Ar 4 3 2 5 1 4 5 6 3 2 8 9 7 1
A ssessing elements An element is accessed by indexing the array name. T his is done by placing the index of the element within square brackets after the name of the array. Example:- int main() { int i ; int a[5]={1,2,3,4,5}; for( i =0;i<5;i++) { printf (“%d\t”, a[ i ]) } }
Storage Representation 1-d array are linear array in which elements are stored in the successive memory locations. The element at the very first position in the array is called its base address. Suppose name of linear array is arr of type int and it has 5 elements with base address start from 1024. Then it is represented as : offset :- 0 4 8 12 16 index :- 0 1 2 3 4 value:- Address:- 1024 1028 1032 1036 1040 14 46 4 86 23
M ultidimensional array C programming language allows multidimensional arrays . T he general form of a multidimensional array declaration − type name [size1][size2]… .[ sizeN ]; For example, the following declaration creates a two dimensional integer array int twoDimension [5][4];
Two-dimensional Arrays The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays . Syntax: type arrayName [x][y]; A two-dimensional array a[3][4] , which contains three rows and four columns can be shown as follows-
Initializing Two-Dimensional Arrays Multidimensional arrays may be initialized by specifying bracketed values for each row . Following is an array with 3 rows and each row has 4 columns . The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the above example −
Accessing Two-dimensional array An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array . #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; /* output each array element's value */ for ( i = 0; i < 5; i ++ ) { for ( j = 0; j < 2; j++ ) { printf ("a[%d][%d] = %d\n", i,j , a[ i ][j] ); } } return 0 ; } 1 2 2 4 3 6 4 8
Introduction to String Strings are array of characters i.e. they are characters arranged one after another in memory. Thus, a character array is called string . Each character in a string occupies one location in an array . A string is always terminated by a null character (i.e. slash zero \0 ). VIIT, Pune 15
D eclaration A string variable is declared as an array of characters. Syntax : char string_name [size]; e.g . char s[5]; When the compiler assigns a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string VIIT, Pune 16