Chapter - 1
Introduction to Data Structures
(Sample Programs)
Syllabus
• Pointers
• Structures
• Dynamic Memory Allocation Functions
• Files
• Data Structures: Classifications, Operations
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 1 of 31
Chapter-1 Sample Programs
1 Pointers
1.1 Pointer: Declaration, Initialization, Accessing
1 # include < stdio .h >
2
3 int main ()
4 {
5 int x1 =2 , x2 =3 , y1 =0 , y2 = -23;
6
7 int * p1 , * p2 ;
8
9 int * q1 , q2 ;
10
11 p1 = & x1 ;
12 p2 = & x2 ;
13
14 q1 = & y1 ;
15 q2 = & y2 ;
16
17 printf ( " \ nx1 =% d " ,* p1 ) ;
18 printf ( " \ nx2 =% d " ,* p2 ) ;
19 printf ( " \ ny1 =% d " ,* q1 ) ;
20 printf ( " \ ny2 =% d " ,* q2 ) ;
21
22 return 0;
23 }
1.2 Largest of 3 Numbers using Pointers
1 # include < stdio .h >
2
3 int main ()
4 {
5 int num1 , num2 , num3 ;
6 int * p1 , * p2 , * p3 ;
7
8 printf ( " Enter First Number : " ) ;
9 scanf ( " % d " ,& num1 ) ;
10 printf ( " Enter Second Number : " ) ;
11 scanf ( " % d " ,& num2 ) ;
12 printf ( " Enter Third Number : " ) ;
13 scanf ( " % d " ,& num3 ) ;
14
15 // assigning the address of input numbers to pointers
16 p1 = & num1 ;
17 p2 = & num2 ;
18 p3 = & num3 ;
19
20 if (* p1 > * p2 )
21 {
22 if (* p1 > * p3 )
23 {
24 printf ( " % d is the largest number " , * p1 ) ;
25 }
26 else
27 {
28 printf ( " % d is the largest number " , * p3 ) ;
29 }
30 }
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 2 of 31
Chapter-1 Sample Programs
31 else
32 {
33 if (* p2 > * p3 )
34 {
35 printf ( " % d is the largest number " , * p2 ) ;
36 }
37 else
38 {
39 printf ( " % d is the largest number " , * p3 ) ;
40 }
41 }
42
43 return 0;
44 }
1.3 Using Pointers for Variable Value Arithmetic
1 # include < stdio .h >
2
3 int main ()
4 {
5 int num1 =2 , num2 =3 , sum =0 , mul =0 , div =1;
6 int * ptr1 , * ptr2 ;
7
8 ptr1 = & num1 ;
9 ptr2 = & num2 ;
10
11 sum = * ptr1 + * ptr2 ;
12
13 mul = sum * * ptr1 ;
14
15 * ptr2 +=1;
16
17 div = 9 + * ptr1 / * ptr2 - 30;
18
19 printf ( " \ nsum =% d " , sum ) ;
20 printf ( " \ nproduct =% d " , mul ) ;
21 printf ( " \ ndiv =% d " , div ) ;
22
23 return 0;
24 }
1.4 Area of Circle through Pointers
1 # include < stdio .h >
2
3 void area_of_circle ( float *r , float * a )
4 {
5 * a = 3.14 * (* r ) * (* r ) ;
6 }
7
8 int main ()
9 {
10 float radius , area ;
11
12 printf ( " \ nEnter the radius of Circle >\ t " ) ;
13 scanf ( " % f " , & radius ) ;
14
15 // area = 3.14 * radius * radius ;
16 area (& radius , & area ) ;
17 printf ( " \ nArea of Circleis >\ t % f " , area ) ;
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 3 of 31
Chapter-1 Sample Programs
18
19 return 0;
20 }
1.5 Call–by–Address: Using Pointers
1 # include < stdio .h >
2
3 void swap ( int * , int *) ;
4
5 int main ()
6 {
7 int a ;
8 int b ;
9
10 printf ( " \ nEnter the values of a and b >\ t " ) ;
11 scanf ( " % d % d " , &a , & b ) ;
12
13 printf ( " \ nValue of A and B before calling function is % d and % d " , a , b ) ;
14
15 swap (& a , & b ) ;
16
17 printf ( " \ nValue of A and B after calling function is % d and % d " , a , b ) ;
18
19 return 0;
20 }
21
22 void swap ( int * pa , int * pb )
23 {
24 int temp ;
25
26 temp = * pa ;
27 * pa = * pb ;
28 * pb = temp ;
29 }
1.6 Arrays through Pointers
1 # include < stdio .h >
2
3 int main ()
4 {
5 int a [4]={10 ,20 ,30 ,40};
6 int *p , i ;
7
8 p = a ; // No need for address operator (&) , as array name ( basename ) holds
starting address of the array .
9
10 printf ( " Array elements are :\ n " ) ;
11
12 for ( i =0; i <4; i ++ )
13 {
14 printf ( " % d \ n " ,*( a + i ) ) ;
15 }
16
17 return 0;
18 }
1.7 Addition of 2 Arrays through Pointers
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 4 of 31
Chapter-1 Sample Programs
1 # include < stdio .h >
2
3 int main ()
4 {
5 int a [8] , b [8] , c [8] , n , i ;
6
7 printf ( " \ nEnter the array size > " ) ;
8 scanf ( " % d " ,& n ) ;
9
10 printf ( " \ nEnter the elements for array -1 > " ) ;
11 for ( i =0; i < n ; i ++ )
12 scanf ( " % d " ,( a + i ) ) ;
13
14 printf ( " \ nEnter the elements for array -2\ n " ) ;
15 for ( i =0; i < n ; i ++ )
16 {
17 scanf ( " % d " ,( b + i ) ) ;
18 *( c + i ) = *( a + i ) + *( b + i ) ;
19 }
20
21 printf ( " \ nResultant array is >\ t " ) ;
22 for ( i =0; i < n ; i ++ )
23 {
24 printf ( " % d " ,*( c + i ) ) ;
25 }
26
27 return 0;
28 }
1.8 Array Swapping through Pointers
1 # include < stdio .h >
2
3 int main ()
4 {
5 int a [8] , b [8] , n , i , temp ;
6
7 printf ( " \ nEnter the array size > " ) ;
8 scanf ( " % d " ,& n ) ;
9
10 printf ( " \ nEnter the elements for array a > " ) ;
11 for ( i =0; i < n ; i ++)
12 scanf ( " % d " ,( a + i ) ) ;
13
14 printf ( " \ nEnter the elements for array b \ n " ) ;
15 for ( i =0; i < n ; i ++)
16 scanf ( " % d " ,( b + i ) ) ;
17
18 // swaping
19 for ( i =0; i < n ; i ++)
20 {
21 temp = *( a + i ) ;
22 *( a + i ) = *( b + i ) ;
23 *( b + i ) = temp ;
24 }
25
26 printf ( " \ nAfter swapping " ) ;
27
28 printf ( " \ nElements for array a >\ t " ) ;
29 for ( i =0; i < n ; i ++)
30 printf ( " % d " , *( a + i ) ) ;
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 5 of 31
Chapter-1 Sample Programs
31
32 printf ( " \ nElements for array b >\ t " ) ;
33 for ( i =0; i < n ; i ++)
34 printf ( " % d " , *( b + i ) ) ;
35
36 return 0;
37 }
1.9 Linear Search through Pointers
1 # include < stdio .h >
2
3 int main ()
4 {
5 int a [5] = {10 ,20 ,30 ,40 ,50};
6 int *p , key , i ;
7
8 p = a;
9
10 printf ( " \ nEnter the key element to be searched > " ) ;
11 scanf ( " % d " ,& key ) ;
12
13 for ( i =0; i <5; i ++)
14 {
15 if ( key == *( p + i ) )
16 {
17 printf ( " \ n % d is found at position % d " , key , i +1) ;
18 return 0;
19 }
20 }
21
22 printf ( " \ n % d is Not found " , key ) ;
23
24 return 0;
25 }
2 Structures
2.1 Structures: Declaration, Initialization, Accessing
1 # include < stdio .h >
2 # include < string .h >
3
4 // Structure Definition
5 struct stud
6 {
7 char name [20];
8 int roll ;
9 float marks ;
10 };
11
12 int main ()
13 {
14 // Structure Declaration
15 struct stud s ;
16
17 // Accessing and Initializing Members
18 strcpy ( s . name , " I am Don " ) ;
19 s . roll = 420;
20 s . marks = 0;
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 6 of 31
Chapter-1 Sample Programs
21
22 printf ( " \ nStudent details are > " ) ;
23 printf ( " \ nNAME =% s \ tROLL =% d \ tMARKS =% f " , s . name , s . roll , s . marks ) ;
24
25 return 0;
26 }
2.2 Declaring Multiple Structure Variables
1 # include < stdio .h >
2 # include < string .h >
3
4 struct employee
5 { int id ;
6 char name [50];
7 };
8
9 int main ()
10 {
11 struct employee e1 , e2 ;
12
13 // 1 st variable
14 e1 . id =101;
15 strcpy ( e1 . name , " KLE " ) ;
16
17 // 2 nd variable
18 e2 . id =102;
19 strcpy ( e2 . name , " VTU " ) ;
20
21 printf ( " \ n1st employee id > % d " , e1 . id ) ;
22 printf ( " \ n1st employee name > % s \ n " , e1 . name ) ;
23
24 printf ( " \ n2nd employee id > % d \ n " , e2 . id ) ;
25 printf ( " \ n2nd employee name > % s \ n " , e2 . name ) ;
26
27 return 0;
28 }
2.3 Global Variable and Reading Data of Structure Members
1 # include < stdio .h >
2
3 // Structure Definition and Global Declaration
4 struct stud
5 {
6 char name [10];
7 int roll ;
8 float marks ;
9 }s;
10
11 int main ()
12 {
13
14 printf ( " \ nEnter name , roll and marks of student > " ) ;
15 scanf ( " % s % d % f " , s . name , & s . roll , & s . marks ) ;
16
17 printf ( " \ nStudent Details are " ) ;
18 printf ( " \ nName > % s " , s . name ) ;
19 printf ( " \ nRoll no > % d " , s . roll ) ;
20 printf ( " \ nMarks > % f " , s . marks ) ;
21
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 7 of 31
Chapter-1 Sample Programs
22 return 0;
23 }
2.4 Renaming through TYPEDEF
1 # include < stdio .h >
2
3 int main ()
4 {
5 typedef int I ;
6
7 I a =10 , b =20 , c = a + b ;
8 printf ( " \ nsum = % d " , c ) ;
9
10 return 0;
11 }
2.5 Structure Renaming through TYPEDEF
1 # include < stdio .h >
2
3 typedef struct complex_number
4 {
5 float real ;
6 float img ;
7 } CMP ;
8
9 int main ()
10 {
11 CMP x , y , res ;
12
13 printf ( " \ nEnter real - part of 1 st complex number > " ) ;
14 scanf ( " % f " , & x . real ) ;
15
16 printf ( " \ nEnter imaginary - part of 1 st complex number > " ) ;
17 scanf ( " % f " , & x . img ) ;
18
19 printf ( " \ nEnter real - part of 2 st complex number > " ) ;
20 scanf ( " % f " , & y . real ) ;
21
22 printf ( " \ nEnter imaginary - part of 2 st complex number > " ) ;
23 scanf ( " % f " , & y . img ) ;
24
25 // addition of x and y
26 res . real = x . real + y . real ;
27 res . img = x . img + y . img ;
28
29 printf ( " \ nResult is > %0.2 f + i (%0.2 f ) " , res . real , res . img ) ;
30
31 return 0;
32 }
2.6 Passing Entire Structure Variable as Argument
1 # include < stdio .h >
2 struct stud
3 {
4 char name [20];
5 int roll ;
6 float marks ;
7 };
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 8 of 31
Chapter-1 Sample Programs
8
9 // Function Declaration : structure as parameter
10 void display ( struct stud ) ;
11
12 int main ()
13 {
14 struct stud s ;
15
16 printf ( " \ nEnter name , roll , marks > " ) ;
17 scanf ( " % s % d % f " ,s . name , & s . roll , & s . marks ) ;
18
19 display ( s ) ;
20
21 return 0;
22 }
23
24 void display ( struct stud t )
25 {
26 printf ( " \ nStudent details are > " ) ;
27 printf ( " \ nNAME =% s \ tROLL =% d \ tMARKS =% f " , t . name , t . roll , t . marks ) ;
28
29 return ;
30 }
2.7 Passing Structure Members as Arguments
1 # include < stdio .h >
2
3 typedef struct complex_number
4 {
5 float real ;
6 float img ;
7 } CMP ;
8
9 void square ( float , float ) ;
10
11 int main ()
12 {
13 CMP x ;
14
15 printf ( " \ nEnter real - part of complex number > " ) ;
16 scanf ( " % f " , & x . real ) ;
17
18 printf ( " \ nEnter imaginary - part of complex number > " ) ;
19 scanf ( " % f " , & x . img ) ;
20
21 square ( x . real , x . img ) ;
22
23 return 0;
24 }
25
26 void square ( float r , float i )
27 {
28 CMP res ;
29
30 res . real = r * r ;
31 res . img = i * i ;
32
33 printf ( " \ nResult is > %0.2 f + i (%0.2 f ) " , res . real , res . img ) ;
34
35 return ;
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 9 of 31
Chapter-1 Sample Programs
36 }
2.8 Returning Structure from Function
1 # include < stdio .h >
2
3 struct stud
4 {
5 char name [10];
6 int roll ;
7 float marks ;
8 };
9
10 struct stud read () ;
11
12 int main ()
13 {
14 struct stud x ;
15
16 x = read () ;
17
18 printf ( " \ nName > % s " , x . name ) ;
19 printf ( " \ nRoll no > % d " , x . roll ) ;
20 printf ( " \ nMarks > % f " , x . marks ) ;
21
22 return 0;
23 }
24
25 struct stud read ()
26 {
27 struct stud s ;
28 printf ( " \ nEnter name , roll and marks > " ) ;
29 scanf ( " % s % d % f " , s . name , & s . roll , & s . marks ) ;
30
31 return s ;
32 }
2.9 Returning Structure from Function-2
1 # include < stdio .h >
2
3 struct cricketer
4 {
5 char name [50];
6 int age ;
7 int match ;
8 float avrn ;
9 char temp ;
10 };
11
12 struct cricketer read () ;
13 void display ( struct cricketer ) ;
14
15 int main ()
16 {
17 int n , i ;
18 struct cricketer c1 ;
19
20 printf ( " \ nEnter the number of cricketers > " ) ;
21 scanf ( " % d " , & n ) ;
22
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 10 of 31
Chapter-1 Sample Programs
23 for ( i =0; i < n ; i ++)
24 {
25 printf ( " \ nEnter data of cricketer %d > " , i +1) ;
26 c1 = read () ;
27
28 printf ( " \ nDetails of cricketer % d " ,i +1) ;
29 display ( c1 ) ;
30 }
31
32 return 0;
33 }
34
35 struct cricketer read ()
36 {
37 struct cricketer c ;
38
39 printf ( " \ nName > " ) ;
40 scanf ( " % s " , c . name ) ;
41
42 printf ( " \ nAge > " ) ;
43 scanf ( " % d " , & c . age ) ;
44
45 printf ( " \ nMatches > " ) ;
46 scanf ( " % d " , & c . match ) ;
47
48 printf ( " \ nAverage runs > " ) ;
49 scanf ( " % f " , & c . avrn ) ;
50
51 return c ;
52 }
53
54 void display ( struct cricketer c )
55 {
56 printf ( " \ nName > % s " , c . name ) ;
57 printf ( " \ nAge > % d " , c . age ) ;
58 printf ( " \ nMatches > % d " , c . match ) ;
59 printf ( " \ nAverage runs > % f " , c . avrn ) ;
60 }
2.10 Pointer to Structure
1 # include < stdio .h >
2
3 // Structure Definition
4 struct stud
5 {
6 char name [20];
7 int roll ;
8 float marks ;
9 };
10
11 int main ()
12 {
13 struct stud * ps , s ;
14
15 // Initialize pointer with address of structure .
16 ps = & s ;
17
18 printf ( " \ nEnter name , roll , marks > " ) ;
19 scanf ( " % s % d % f " , ps - > name , & ps - > roll , & ps - > marks ) ;
20
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 11 of 31
Chapter-1 Sample Programs
21 printf ( " \ nStudent details are : " ) ;
22 printf ( " \ nNAME =% s \ tROLL =% d \ tMARKS =% f " , ps - > name , ps - > roll , ps - > marks ) ;
23
24 return 0;
25 }
2.11 Array of Structures
1 # include < stdio .h >
2
3 /* Declaration of structure */
4 struct student
5 {
6 char name [30];
7 int roll ;
8 float marks ;
9 };
10
11 int main ()
12 {
13 struct student s [10];
14 int n , i ;
15
16 printf ( " \ nEnter the number of students > " ) ;
17 scanf ( " % d " , & n ) ;
18
19 for ( i =0; i < n ; i ++)
20 {
21 printf ( " Enter name , roll and marks of % d student > " , i +1) ;
22 scanf ( " % s % d % f " ,s [ i ]. name , & s [ i ]. roll , & s [ i ]. marks ) ;
23 }
24
25 printf ( " \ nStudent details are : " ) ;
26 for ( i =0; i < n ; i ++)
27 {
28 printf ( " \ nName > % s " ,s [ i ]. name ) ;
29 printf ( " \ nRoll > % d " , s [ i ]. roll ) ;
30 printf ( " \ nMarks > %0.2 f " , s [ i ]. marks ) ;
31 }
32
33 return 0;
34 }
2.12 Pointer to Array of Structures
1 # include < stdio .h >
2
3 struct x
4 {
5 int i ;
6 float f ;
7 char c [10];
8 };
9
10 void main ()
11 {
12 struct x x1 [10] , * p ;
13 int n , j ;
14
15 p = x1 ;
16
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 12 of 31
Chapter-1 Sample Programs
17 printf ( " \ nEnter the array size > " ) ;
18 scanf ( " % d " ,& n ) ;
19
20 for ( j =0; j < n ; j ++)
21 {
22 printf ( " \ nEnter int , float and char values > " ) ;
23 scanf ( " % d % f % s " , &( p + j ) ->i , &( p + j ) ->f , ( p + j ) ->c ) ;
24 }
25
26 printf ( " \ nThe int , float and char values assigned are : " ) ;
27 for ( j =0; j < n ; j ++)
28 printf ( " \ n % d \ t %0.2 f \ t % s " , ( p + j ) ->i , ( p + j ) ->f , ( p + j ) ->c ) ;
29 }
2.13 Nested Structures
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 Display the achievements of cricket players .
3 [ Hint > make use of Nested structure ]
4 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
5
6 # include < stdio .h >
7
8 struct stats
9 {
10 int matches ;
11 float average ;
12 };
13
14 struct cricketer
15 {
16 char name [50];
17 int age ;
18 struct stats pstats ;
19 };
20
21 struct cricketer read () ;
22 void display ( struct cricketer ) ;
23
24 void main ()
25 {
26 int i ;
27 struct cricketer c1 ;
28
29 printf ( " \ nEnter data of cricketer > " ) ;
30 c1 = read () ;
31
32 printf ( " \ nDetails of the cricketer are as follows : " ) ;
33 display ( c1 ) ;
34 }
35
36 struct cricketer read ( struct cricketer c )
37 {
38 printf ( " \ nName > " ) ;
39 scanf ( " % s " , c . name ) ;
40
41 printf ( " \ nAge > " ) ;
42 scanf ( " % d " , & c . age ) ;
43
44 printf ( " \ nMatches > " ) ;
45 scanf ( " % d " , & c . pstats . matches ) ;
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 13 of 31
Chapter-1 Sample Programs
46
47 printf ( " \ nAverage runs > " ) ;
48 scanf ( " %0.2 f " , & c . pstats . average ) ;
49
50 return c ;
51 }
52
53 void display ( struct cricketer c )
54 {
55 printf ( " \ nName > % s " , c . name ) ;
56 printf ( " \ nAge > % d " , c . age ) ;
57 printf ( " \ nMatches > % d " , c . pstats . matches ) ;
58 printf ( " \ nAverage runs > % f " , c . pstats . average ) ;
59 }
2.14 Pointer to Nested Structures
1 # include < stdio .h >
2 # include < string .h >
3
4 struct colDetail
5 {
6 int cid ;
7 char cname [50];
8 };
9
10 struct studDetail
11 {
12 int id ;
13 char name [20];
14 float percentage ;
15
16 // structure within structure
17 struct colDetail cData ;
18 };
19
20 int main ()
21 {
22 struct studDetail s = {1 , " Blue - Om " , 90.5 , 02 , " KLE " };
23
24 struct studDetail * sPtr = & s ;
25
26 printf ( " \ nId is > % d " , sPtr - > id ) ;
27 printf ( " \ nName is > % s " , sPtr - > name ) ;
28 printf ( " \ nPercentage is > %0.2 f " , sPtr - > percentage ) ;
29
30 printf ( " \ nCollege Id is > % d " , sPtr - > cData . cid ) ;
31 printf ( " \ nCollege Name is > % s " , sPtr - > cData . cname ) ;
32
33 return 0;
34 }
3 Dynamic Memory Management
3.1 Dynamic String Memory Allocation through – malloc
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main ()
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 14 of 31
Chapter-1 Sample Programs
5 {
6 char * p ;
7 int n ;
8
9 printf ( " \ nWhat is the length of your string > " ) ;
10 scanf ( " % d " , & n ) ;
11
12 // allocate memory dynamically
13 p = ( char *) malloc ( n * sizeof ( char ) ) ;
14
15 if ( p == NULL )
16 {
17 printf ( " \ nError - unable to allocate required memory " ) ;
18 exit (101) ;
19 }
20 else
21 {
22 printf ( " \ nEnter the string > " ) ;
23 scanf ( " % s " , p ) ;
24 }
25
26 printf ( " \ nYour dynamically allocated string is % s " , p ) ;
27
28 // release the allocated memory
29 free ( p ) ;
30
31 return 0;
32 }
3.2 Sum of Dynamic Array Elements through – calloc
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main ()
5 {
6 int n , i , *p , sum =0;
7
8 printf ( " \ nEnter number of elements > " ) ;
9 scanf ( " % d " , & n ) ;
10
11 // memory allocated using calloc
12 p = ( int *) calloc ( n , sizeof ( int ) ) ;
13
14 if ( p == NULL )
15 {
16 printf ( " Error ! unable to allocate memory " ) ;
17 exit (101) ;
18 }
19
20 printf ( " \ nEnter elements of array > " ) ;
21 for ( i =0; i < n ; i ++ )
22 {
23 scanf ( " % d " , p + i ) ;
24 sum += *( p + i ) ;
25 }
26
27 printf ( " Sum of the array elements is % d " , sum ) ;
28
29 free ( p ) ;
30
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 15 of 31
Chapter-1 Sample Programs
31 return 0;
32 }
3.3 Dynamic Structure Memory Management
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 struct course
5 {
6 char name [10];
7 int marks ;
8 };
9
10 int main ()
11 {
12 struct course * p ;
13 int n , i ;
14
15 printf ( " \ nEnter the number of courses > " ) ;
16 scanf ( " % d " , & n ) ;
17
18 // Memory allocation for n structures
19 p = ( struct course *) malloc ( n * sizeof ( struct course ) ) ;
20
21 for ( i = 0; i < n ; i ++)
22 {
23 printf ( " \ nEnter % d course name and marks : " , i +1) ;
24 scanf ( " % s % d " , ( p + i ) -> name , &( p + i ) -> marks ) ;
25 }
26
27 printf ( " \ nDisplaying Information : " ) ;
28 for ( i = 0; i < n ; i ++)
29 {
30 printf ( " \ nName : % s \ tMarks : % d " , ( p + i ) -> name , ( p + i ) -> marks ) ;
31 }
32
33 free ( p ) ;
34
35 return 0;
36 }
3.4 Increasing the Size of Dynamic Array through – realloc
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main ()
5 {
6 int *p , * pNew , n , nNew , i ;
7 char ch ;
8
9 printf ( " \ nEnter the size of the array > " ) ;
10 scanf ( " % d " , & n ) ;
11
12 // allocate memory for n size
13 p = ( int *) malloc ( n * sizeof ( int ) ) ;
14
15 // read the data
16 for ( i =0; i < n ; i ++)
17 {
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 16 of 31
Chapter-1 Sample Programs
18 printf ( " \ nEnter % d value > " , i +1) ;
19 scanf ( " % d " , ( p + i ) ) ;
20 }
21
22 // choose to add new items
23 printf ( " \ nDo you want to add new items [ y | n ] > " ) ;
24 scanf ( " % c " , & ch ) ;
25
26 if ( ch == ’y ’ || ch == ’Y ’ )
27 {
28 printf ( " \ nHow many more items > " ) ;
29 scanf ( " % d " , & nNew ) ;
30
31 // increase the size of data items
32 n += nNew ;
33
34 printf ( " \ nNew size : % d " , n ) ;
35
36 // reallocate the new memory
37 pNew = ( int *) realloc (p , n * sizeof ( int ) ) ;
38
39 // read the new data
40 for ( ; i < n ; i ++)
41 {
42 printf ( " \ nEnter % d value > " , i +1) ;
43 scanf ( " % d " , ( pNew + i ) ) ;
44 }
45 }
46
47 // display the data
48 printf ( " \ nData items are : " ) ;
49 for ( i =0; i < n ; i ++)
50 {
51 printf ( " % d " , *( p + i ) ) ;
52 }
53
54 return 0;
55 }
4 File Management
4.1 File Read through – fgetc
1 # include < stdio .h >
2
3 int main ()
4 {
5 FILE * fp ;
6 char ch ;
7
8 fp = fopen ( " 1. c " ," r " ) ;
9
10 while ( 1 )
11 {
12 ch = fgetc ( fp ) ;
13 if ( ch == EOF )
14 break ;
15 printf ( " % c " , ch ) ;
16 }
17
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 17 of 31
Chapter-1 Sample Programs
18 fclose ( fp ) ;
19
20 return 0;
21 }
4.2 File Read through – fgets
1 # include < stdio .h >
2
3 int main ()
4 {
5 FILE * fp ;
6 char data [50];
7
8 fp = fopen ( " test . txt " ," r " ) ;
9
10 if ( fp == NULL )
11 {
12 printf ( " test . txt file failed to open . " ) ;
13 }
14 else
15 {
16
17 printf ( " The file is now opened .\ n " ) ;
18
19 while ( fgets ( data , 50 , fp ) != NULL )
20 {
21 printf ( " % s " , data ) ;
22 }
23 }
24
25 fclose ( fp ) ;
26
27 printf ( " \ nData successfully read from file test . txt " ) ;
28 printf ( " \ nThe file is now closed . " ) ;
29
30 return 0;
31 }
4.3 File Write through – fputs
1 # include < stdio .h >
2 # include < string .h >
3
4 int main ( )
5 {
6 FILE * fp ;
7
8 char data [50] = " I am Don , my file is dummy - dummie " ;
9
10 fp = fopen ( " test . txt " , " w " ) ;
11
12 if ( fp == NULL )
13 {
14 printf ( " \ ntest . txt file failed to open . " ) ;
15 }
16 else
17 {
18 printf ( " \ nThe file is now opened .\ n " ) ;
19 fputs ( data , fp ) ;
20 }
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 18 of 31
Chapter-1 Sample Programs
21
22 fclose ( fp ) ;
23
24 printf ( " \ nData successfully written in file test . txt " ) ;
25 printf ( " \ nThe file is now closed . " ) ;
26
27 return 0;
28 }
5 Sample PSF and Programs
5.1 Structures
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 19 of 31
Sample PSF and Program for Structures
April 20, 2023
Problem Statement: Varun Finchi is a manger of BCR franchise team, where he has to
select 3 players to make squad full for playing in PPL League. He contacts Lord Hassan, an
analyst of BCR team, where he has all the player details. Varun Finchi selects the players as
(1) who have highest runs in their career, (2) one who has least runs in his career and (3) one
more player who has name “Rajat”. Please help Varun Finchi to select players for BCR team.
1 Understanding the Problem
1.1 Sub-problems
Yes, we have three sub-problem in the question.
• To select the highest and lowest scoring player we have to sort the players.
• To select “Rajat” player we have to search the player from the pool.
• Copy the 3 selected players from pool to selected list.
1.2 Ambiguity
Yes, there are two ambiguity in the question. First one is how many players are there in the
pool?. Second one is which are the other player details to consider, apart from runs?
We remove the ambiguity in assumption section.
1.3 Extra Information
Yes, there are extraneous information in the question. BCR franchise team and PPL League.
These information are not needed to solve the problem.
1.4 Known
• There is pool of players.
• Every player has some details, including runs scored in his career.
• 3 players are to be selected.
• One player named “Rajat” has to be selected.
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 1 of 8
Sample PSF & Program Structures
1.5 Unknowns
• Highest run scorer.
• Lowest run scorer.
1.6 Assumptions
For the given problem as there are two ambiguities, we thus have to make two assumptions.
• For first ambiguity, we assume that user will give the pool size.
• For second ambiguity, we assume that we will take 3 player details: name, jersey number
and runs scored.
1.7 Constraints
Player name Rajat has to be selected. Thus, he has to be there in the player pool. Also, pool
should have a minimum of 3 players.
2 Plan to Solve the Problem
2.1 Problem Representation
The given problem is based on the selecting highest and lowest values. Thus, problem at hand
can be represented numerically.
2.2 Problem Solving Strategy
To find the highest and lowest values, we can sort the player details according to the runs
scored. Thus, we can solve the problem using Mathematical Reasoning strategy.
2.3 Identification of Structure Name and its Members
• Structure Name: players
• Structure Members: These include the player details
– name ← Name of the player, it is string data type.
– jerseyNo ← Jersey number of the player, it is integer data type.
– runs ← Total number of runs scored by the player, it is integer data type.
2.4 Identification of Functions
• readDetails() ← To read the number of players and their details.
• displayDetails() ← To display the players’ details and selected players.
• sortPlayers() ← To sort the players according to their runs scored.
• searchPlayer() ← To search for the rajat player.
• selectPlayers() ← To copy the selected players from pool structure to selected structure.
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 2 of 8
Sample PSF & Program Structures
3 Carrying out the Plan
3.1 Solution Strategy
The problem is solved in three steps.
1. Sort the players in descending order by their runs scored.
2. Select the first and last player from sorted list, to obtain the highest and lowest scoring
player.
3. Search the player pool from rajat and copy his details to the selected list.
Algorithm for the above solution to the problem is as shown in Algorithm 1.
Algorithm 1: Select Players
Data: pool, array of structures, holds the player details.
Result: selected, array of structures, holds the selected players.
1 STEP-0: START
2 STEP-1: // Read the player details into pool structure.
3 n ← Read the number of players.
4 for p ← 0 to n − 1 do
5 Read the player details.
6 STEP-2: Sort the players details in descending order.
// Bubble Sort
7 for i ← 0 to n − 1 do
8 for j ← 0 to (n − i) − 1 do
9 if Is players[j].runs less than players[j+1].runs? then
// then, swap entire players details
10 temp ← players[j]
11 players[j] ← players[j+1]
12 players[j+1] ← temp
13 STEP-3: Copy the highest and lowest player details to selected structure.
14 selected[0] ← players[0]
15 selected[1] ← players[1]
16 STEP-4: Search for player named “Rajat” and copy the details to selected structure.
// Linear Search
17 for i ← 0 to n − 1 do
18 if strcmp(players[j].name , “Rajat”) is ture? then
19 selected[2] ← Rajat player details.
20 STEP-5: Display the selected player details.
21 STEP-6: END
4 Assessing the Results
4.1 Was our understanding of the problem correct?
Yes. We applied the PSF to select the players according to the given rules.
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 3 of 8
Sample PSF & Program Structures
4.2 Did we overlook anything?
No. We solved all the sub-problems player selection. Also, the extra information was eliminated.
4.3 Did we choose the correct strategy?
Yes. As the problem was based on mathematical reasoning, we applied sorting technique to
obtain the highest and lowest run scorer. Further, we also used searching technique to select
the specified player.
4.4 Did we employ that strategy correctly?
Yes. We used bubble sort algorithm to obtain highest and lowest run scorers. Also, linear
search was employed to look for given player.
5 Lessons Learnt
The BCR team manager’s problem is solved using the sorting and searching algorithms.
Through descending order sort, we can obtain the players with highest runs scorers to low-
est runs scorers. Therefore we select find and last index players. We perform linear search
for player name with Rajat. Therefore we select and display the selected 3 players as per the
manager’s needs.
6 Solution Documentation
Enter number of player details to read > 7
Enter Player name, jersey number, runs scored
virat 1 40
dhoni 2 50
rohit 3 29000
ashwin 12 12000
ishant 34 2554
raina 20 12455
Rajat 10 4832
The sorted players from pool are:
NAME JERSEY RUNS
———————————-
rohit 3 29000
raina 20 12455
ashwin 12 12000
ishant 34 2554
dhoni 2 50
The selected players are:
NAME JERSEY RUNS
———————————-
rohit 3 29000
Rajat 10 4832
virat 1 40
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 4 of 8
Sample PSF & Program Structures
7 C Program
7.1 Without Using Functions
1 # include < stdio .h >
2 # include < string .h >
3
4 // Structure Definition
5 struct player
6 {
7 char name [20];
8 int jerseyNo , runs ;
9 };
10
11 int main ()
12 {
13 struct player pool [10] , selected [10] , temp ;
14 int i , n , j ;
15
16 printf ( " \ nEnter no . of players > " ) ;
17 scanf ( " % d " ,& n ) ;
18
19 for ( i = 0; i < n ; i ++ )
20 {
21 printf ( " \ nEnter player % d details ( name , jersey number , runs ) >\ t " ,i
+1) ;
22 scanf ( " % s % d % d " , pool [ i ]. name , & pool [ i ]. jerseyNo , & pool [ i ]. runs ) ;
23 }
24
25 // Bubble Sort
26 for ( i = 0; i < n ; i ++ )
27 {
28 for ( j = 0; j < n -i -1; j ++ )
29 {
30 if ( pool [ j ]. runs < pool [ j +1]. runs )
31 {
32 temp = pool [ j ];
33 pool [ j ] = pool [ j +1];
34 pool [ j +1] = temp ;
35 }
36 }
37 }
38
39 selected [0] = pool [0];
40 selected [1] = pool [n -1];
41
42 // Linear Search
43 for ( i = 0; i < n ; i ++ )
44 {
45 if ( strcmp ( pool [ i ]. name , " rajat " ) == 0 )
46 {
47 selected [2] = pool [ i ];
48 }
49 }
50
51 printf ( " \ nPlayers in the sorted pool are : " ) ;
52 printf ( " \ nNAME \ tJERSEY \ tRUNS \ t \ n " ) ;
53 printf ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\ n " ) ;
54 for ( i = 0; i < n ; i ++ )
55 {
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 5 of 8
Sample PSF & Program Structures
56 printf ( " % s \ t %3 d \ t %3 d \ n " , pool [ i ]. name , pool [ i ]. jerseyNo , pool [ i ].
runs ) ;
57 }
58
59 printf ( " \ nThe selected players are :\ n " ) ;
60 printf ( " \ nNAME \ tJERSEY \ tRUNS \ t \ n " ) ;
61 printf ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\ n " ) ;
62 for ( i = 0; i < 3 ; i ++ )
63 {
64 printf ( " % s \ t %3 d \ t %3 d \ n " , selected [ i ]. name , selected [ i ]. jerseyNo ,
selected [ i ]. runs ) ;
65 }
66
67 }
7.2 Modular Approach
1 # include < stdio .h >
2 # include < string .h >
3
4 // Structure Definition
5 struct player
6 {
7 char name [20];
8 int jerseyNo , runs ;
9 };
10
11 // Function Definition
12 void read ( int , struct player *) ;
13 void display ( int , int , struct player [] , struct player []) ;
14 int search ( int , char [] , struct player []) ;
15 void sort ( int , struct player *) ;
16 int select ( struct player [] , struct player * , int , int ) ;
17
18 int main ()
19 {
20 struct player pool [10];
21 struct player selected [10];
22 struct player * p ;
23 int n ;
24 int pos ;
25 int cnt ;
26
27 printf ( " \ nEnter no . of players > " ) ;
28 scanf ( " % d " ,& n ) ;
29
30 p = pool ;
31
32 read (n , p ) ;
33
34 sort (n , p ) ;
35
36 pos = search (n , " rajat " , p ) ;
37
38 cnt = select ( pool , selected , n , pos ) ;
39
40 display (n , cnt , pool , selected ) ;
41 }
42
43
44 void read ( int l , struct player * ply )
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 6 of 8
Sample PSF & Program Structures
45 {
46 int i ;
47
48 for ( i = 0; i < l ; i ++ )
49 {
50 printf ( " \ nEnter player % d details ( name , jersey number , runs ) >\ t " ,i
+1) ;
51 scanf ( " % s % d % d " , ( ply + i ) -> name , &( ply + i ) -> jerseyNo , &( ply + i ) -> runs ) ;
52 }
53 }
54
55 void sort ( int l , struct player * ply )
56 {
57 int i , j ;
58 struct player temp ;
59
60 // Bubble Sort
61 for ( i = 0; i < l ; i ++ )
62 {
63 for ( j = 0; j < l -i -1; j ++ )
64 {
65 if ( ( ply + j ) -> runs < ( ply + j +1) -> runs )
66 {
67 temp = *( ply + j ) ;
68 *( ply + j ) = *( ply + j +1) ;
69 *( ply + j +1) = temp ;
70 }
71 }
72 }
73 }
74
75
76 int search ( int l , char key [] , struct player pool [])
77 {
78 int i ;
79 // Linear Search
80 for ( i = 0; i < l ; i ++ )
81 {
82 if ( strcmp ( pool [ i ]. name , " rajat " ) == 0 )
83 {
84 return i ;
85 }
86 }
87 return -1;
88 }
89
90 int select ( struct player pool [] , struct player *s , int l , int pos )
91 {
92 int cnt = 0;
93
94 * s = pool [0];
95 *( s +1) = pool [l -1];
96
97 cnt = 2;
98
99 if ( pos != -1)
100 {
101 *( s +2) = pool [ pos ];
102 cnt ++;
103 }
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 7 of 8
Sample PSF & Program Structures
104 else
105 printf ( " \ n Rajat player not found in the pool ! " ) ;
106
107 return cnt ;
108 }
109
110 void display ( int l , int cnt , struct player p [] , struct player s [])
111 {
112 int i ;
113
114 printf ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\ n " ) ;
115 printf ( " \ nThe sorted players from pool are : " ) ;
116 printf ( " \ nNAME \ tJERSEY \ tRUNS \ t \ n " ) ;
117 printf ( " -\n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) ;
118 for ( i = 0; i < l ; i ++ )
119 {
120 printf ( " % s \ t %3 d \ t %3 d \ n " , p [ i ]. name , p [ i ]. jerseyNo , p [ i ]. runs ) ;
121 }
122
123 printf ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) ;
124 printf ( " \ nThe selected players are : " ) ;
125 printf ( " \ nNAME \ tJERSEY \ tRUNS \ t \ n " ) ;
126 printf ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) ;
127 for ( i = 0; i < cnt ; i ++ )
128 {
129 printf ( " % s \ t %3 d \ t %3 d \ n " , s [ i ]. name , s [ i ]. jerseyNo , s [ i ]. runs ) ;
130 }
131
132 }
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 8 of 8
Chapter-1 Sample Programs
5.2 Files
5.2.1 Understanding the Problem
Problem Statement: Sangamesh once a student, now works for Contineo. His boss has
asked him to implement a database for 3rd sem students. Sangamesh had studied about flat
file databases from SPattar sir. In flat file systems, files are used for storing the data. Help
sangamesh to implement the database.
General Overview
• We need to use file management operations to implement the flat file database.
• Following operations on file are to be performed.
– Write operation to store the data.
– Read operation to access the data.
Sub-problems Yes, we have two sub-problem in the question. One is to read the data from
file and other is to write the data to the file.
Ambiguity Yes, there are two ambiguity in the question. First one is how many students’
data are to be stored? Second one is which student details are to be stored.
Extra Information Yes, there are extraneous information in the question. Sangamesh,
Contineo and SPattar are extra information.
Known
• Data is to be stored and accessed from the file (read and write).
• Student data is to be stored (structures).
• There are many students.
Unknowns There are no unknowns in the question.
Assumptions
• We assume that user will give the number of students.
• For simplicity, we will take only two data items of students: name and marks.
Constraints Which type of file to use? As the data to be stored can be large, we can use
binary files. This will reduce the storage space and the read and write operations are also quick.
5.2.2 Plan to Solve the Problem
Problem Representation The given problem is based on storing the data in files. Thus,
problem at hand can be represented in tabular form.
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 28 of 31
Chapter-1 Sample Programs
Problem Solving Strategy To store the data, we need files, and to access the data, we need
the data members of the structure. Thus, we can solve the problem using Natural Language
strategy, i.e., based on Q and A or query technique of database.
Identification of Structure Name and its Members
• Structure Name: student
• Structure Members: These include the player details
– name ← Name of the student, it is string data type.
– marks ← Marks scored by the student, it is integer data type.
Identification of Functions
• writeFile() ← To store the student details in file, it makes use of readStruct().
• readFile() ← To access the students details from file, it makes use of displayStruct().
• readStruct() ← To read the students’ details from user and give it to writeFile().
• displayStruct() ← To display the students’ details received from the readFile().
5.2.3 Carrying out the Plan
Solution Strategy The problem is solved in two steps.
1. Read data from user and store it in file.
2. Read data from file and display it.
5.2.4 Assessing the Results
Was our understanding of the problem correct? Yes. We applied the PSF to store and
access the students’ details.
Did we overlook anything? No. We solved all the sub-problems. Also, the extra informa-
tion was eliminated.
Did we choose the correct strategy? Yes. As the problem was based on natural language,
we identified the students’ details and used correct file operations to store and access the data.
Did we employ that strategy correctly? Yes. We used binary files to store the data.
Also, fread() and fwrite() library functions are used to store and read the data from the binary
file.
5.2.5 Lessons Learnt
We learnt that binary files are efficient to store a large amount of data. Traditionally, flat file
database systems made use of binary files. Also, we learnt about fread() and fwrite() functions
in stdio.h
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 29 of 31
Chapter-1 Sample Programs
5.2.6 Solution Documentation
Enter file name> stud.db
Enter number of students> 3
Enter details for student 1
Enter Student name> sangamesh
Enter Student marks> 23
Enter details for student 2
Enter Student name> Santosh
Enter Student marks> 100
Enter details for student 3
Enter Student name> Don
Enter Student marks> 99
Students details are:
name: sangamesh marks: 23
name: Santosh marks: 100
name: Don marks: 99
5.2.7 C Program
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4
5 struct stud
6 {
7 char name [10];
8 int marks ;
9 };
10
11 void writeFile ( char [] , int ) ;
12 void readFile ( char [] , int ) ;
13
14 struct stud readStruct () ;
15 void displayStruct ( struct stud ) ;
16
17 int main ()
18 {
19 int n ;
20 char fName [10];
21
22 printf ( " \ nEnter file name > " ) ;
23 scanf ( " % s " , fName ) ;
24
25 printf ( " \ nEnter number of students > " ) ;
26 scanf ( " % d " , & n ) ;
27
28 writeFile ( fName , n ) ;
29 readFile ( fName , n ) ;
30
31 return 0;
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 30 of 31
Chapter-1 Sample Programs
32 }
33
34 void writeFile ( char fName [10] , int n )
35 {
36 int i ;
37 struct stud s ;
38 FILE * fptr ;
39
40 fptr = fopen ( fName , " wb " ) ;
41
42 if ( fptr == NULL )
43 {
44 printf ( " Error ! opening file " ) ;
45 exit (101) ;
46 }
47
48 for ( i = 0; i < n ; i ++ )
49 {
50 printf ( " \ nEnter details for student % d " , i +1) ;
51 s = readStruct () ;
52 fwrite (& s , sizeof ( struct stud ) , 1 , fptr ) ;
53 }
54
55 fclose ( fptr ) ;
56 }
57
58 struct stud readStruct ()
59 {
60 struct stud s ;
61
62 printf ( " \ nEnter Student name > " ) ;
63 scanf ( " % s " , s . name ) ;
64
65 printf ( " \ nEnter Student marks > " ) ;
66 scanf ( " % d " , & s . marks ) ;
67
68 return s ;
69 }
70
71 void readFile ( char fName [10] , int n )
72 {
73 struct stud s ;
74 int i ;
75 FILE * fptr ;
76
77 fptr = fopen ( fName , " rb " ) ;
78
79 printf ( " \ nStudents details are : " ) ;
80 for ( i = 0; i < n ; i ++ )
81 {
82 fread (& s , sizeof ( struct stud ) , 1 , fptr ) ;
83 displayStruct ( s ) ;
84 }
85
86 fclose ( fptr ) ;
87 }
88
89 void displayStruct ( struct stud s )
90 {
91 printf ( " \ nname : % s \ tmarks : % d " , s . name , s . marks ) ;
92 }
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 31 of 31
Chapter-1 Sample Programs
Dr. SPattar, KLE Technological University’s Dr. MSSCET, Belagavi Page 32 of 31