Introduction to Abstract Data Type & C++ Revision 1 Data Structure & Algorithm Part II 10/26/2015.
Data Structure and c Part-5
description
Transcript of Data Structure and c Part-5
![Page 1: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/1.jpg)
Data Structure and cPart-5
![Page 2: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/2.jpg)
Mean
An array Symbol [] An array is a group of related data items that stare a common name.
Example:-• Salary [10]:
![Page 3: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/3.jpg)
Salary 1 [10]; Salary 2[15]; Salary 3[20]; Salary 4[25]
![Page 4: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/4.jpg)
Initializing arrayIt have to specify the index of the subscript with the square brackets.Syntax;
Storage class data type array [size] ={value1, value2, value3}
Example:-Int num [5] = {3,4,6,7,10}
![Page 5: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/5.jpg)
Unsized Array
The compiler will count the number of initialize and substitute that number of the size of the array
Example:-Int even [ ] = {2,4,6,8,10}
Int Odd [ ] = {1,3,5,7,9}
![Page 6: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/6.jpg)
Three several types of Arrays
![Page 7: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/7.jpg)
One-Dimensional array 1-D
Example:-One dimensional array
int * [6];Syntax;
data type array name [size];
![Page 8: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/8.jpg)
Two dimensional arrays2-D
• Two dimensional arrays is also called matrix • It has two subscripts
Syntax;Storage class data type array
[s1] [s2];Example:-
Int X [i];Int Y [i];
OR int a [2] [2];
![Page 9: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/9.jpg)
Array of String
It string is defined as a null terminated character array. An array should be defined with an additional byte for holding this null character.Syntax;
Char array name [size];Example:-
Char name [6] = “india” ;
![Page 10: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/10.jpg)
STORAGE CLASSES
• Storage Classes define a variable one needs to mention not only its ‘type’.
• But also its ‘storage class’ Storage class refers to the permanence of a variable.
![Page 11: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/11.jpg)
Types of Storage Class
![Page 12: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/12.jpg)
Automatic storage class
Storage - MemoryDefault initial value - Garbage valueScope - Local to the block in
which the variable is definedLife - Till the control remains
within the block in which it is defined The keyword for this storage class is auto.
![Page 13: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/13.jpg)
Example of Code Program
Example:main ( ){
increment ( );increment ( );increment ( );getch ( );
}Increment ( );
{auto int i = 1;
print (“%d\n”,i);i = i+1;}The output of the program is:111
![Page 14: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/14.jpg)
Register storage class
• Storage - CPU registers.
• Default initial value - Garbage value.
• Scope - Local to the block
in which the variable is defined.
• Life - Till the control remains
within the block in it is defined.
![Page 15: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/15.jpg)
Example of Code Program
Example:main ( ){
register int i;for (i=1;<=10;i++)print (“%d\t”,i);getch ( );
}
The output is1 2 3 4 5 6 7 8 9 10
![Page 16: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/16.jpg)
Static storage class• A storage class with persistent duration is static.
• A static variable declared inside a function retains its value after the function terminates.
Storage -MemoryDefault initial value -ZeroScope -Local to the block in
which the variable is definedLife -Value of the variable
persists between different Function calls.
![Page 17: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/17.jpg)
Example of Code Program
Example:# include <stdio.h>main ( ){
increment ( );increment ( );increment ( );getch ( );
}
increment ( );{static int i = 1;print (“%d\n”,i);i = i+1;}
![Page 18: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/18.jpg)
External storage class
The features of a variable whose storage class has been defined as extern are as follows:Storage -MemoryDefault initial value -ZeroScope -GlobalLife -As long as the program execution doesn’t come to an end
![Page 19: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/19.jpg)
Example of Code ProgramExample:
int main ( ){
extern int z;
int i;………
………
return 0 ;}
![Page 20: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/20.jpg)
FUNCTIONS of TYPES
Functions are the building blocks of the C programming language.
The most important of the C functions is the main( ) function.
Some of the other functions are given below:-
![Page 21: Data Structure and c Part-5](https://reader035.fdocuments.net/reader035/viewer/2022062519/56814f9a550346895dbd5778/html5/thumbnails/21.jpg)
printf( )
scanf( )
getchar( )
getch( )