Intro to Programming Week 8
Intro to Programming Week 8
Contents
1 Introduction to Structures 2
4 Introduction to Unions 6
4.1 Declaring Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2 Using Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1
1 Introduction to Structures
Structures in C allow the grouping of variables of different data types under a
single name. This is particularly useful for representing complex data.
1 struct Book {
2 char title [50];
3 char author [50];
4 int pages ;
5 float price ;
6 };
2
6 } Book ;
7
8 Book book3 = {" To Kill a Mockingbird " , " Harper Lee " ,
281 , 7.99};
4 struct Book {
5 char title [50];
6 char author [50];
7 int pages ;
8 float price ;
9 };
10
11 int main () {
12 struct Book book1 ;
13
26 return 0;
27 }
3
3
4 struct Date {
5 int day ;
6 int month ;
7 int year ;
8 };
9
10 struct Book {
11 char title [50];
12 char author [50];
13 int pages ;
14 float price ;
15 struct Date published ;
16 };
17
18 int main () {
19 struct Book book1 ;
20
37 return 0;
38 }
4
4 struct Book {
5 char title [50];
6 char author [50];
7 int pages ;
8 float price ;
9 };
10
11 int main () {
12 struct Book library [2];
13
35 return 0;
36 }
4 struct Student {
5 char name [50];
6 int scores [5]; // Array to hold scores in 5
subjects
7 };
5
8
9 int main () {
10 struct Student student1 ;
11
12 // Assigning values
13 strcpy ( student1 . name , " Alice ") ;
14 int tempScores [5] = {85 , 90 , 78 , 92 , 88};
15 for ( int i = 0; i < 5; i ++) {
16 student1 . scores [ i ] = tempScores [ i ];
17 }
18
19 // Printing values
20 printf (" Name : % s \ n " , student1 . name ) ;
21 printf (" Scores : ") ;
22 for ( int i = 0; i < 5; i ++) {
23 printf ("% d " , student1 . scores [ i ]) ;
24 }
25 printf ("\ n ") ;
26
27 return 0;
28 }
4 Introduction to Unions
Unions are similar to structures but use the same memory location for all their
members. This means only one member can hold a value at a time.
1 # include < stdio .h >
2 # include < string .h >
3
4 union Data {
5 int intValue ;
6 float floatValue ;
7 char strValue [20];
8 };
9
10 int main () {
11 union Data data ;
12
6
18
22 return 0;
23 }
4 union Data {
5 int intValue ;
6 float floatValue ;
7 char strValue [20];
8 };
9
10 int main () {
11 union Data data ;
12
23 return 0;
7
24 }
6 struct Date {
7 int day ;
8 int month ;
9 int year ;
10 };
11
12 struct Book {
13 char title [50];
14 char author [50];
15 int pages ;
16 float price ;
17 struct Date published ;
18 };
19
23 void addBook () {
24 if ( book_count < MAX_BOOKS ) {
25 struct Book newBook ;
26 printf (" Enter title : ") ;
27 getchar () ; // To consume newline character
left by previous input
28 fgets ( newBook . title , sizeof ( newBook . title ) ,
stdin ) ;
29 newBook . title [ strcspn ( newBook . title , "\ n ") ] =
’\0 ’; // Remove newline character
30 printf (" Enter author : ") ;
31 fgets ( newBook . author , sizeof ( newBook . author ) ,
stdin ) ;
8
32 newBook . author [ strcspn ( newBook . author , "\ n ") ]
= ’\0 ’; // Remove newline character
33 printf (" Enter pages : ") ;
34 scanf ("% d " , & newBook . pages ) ;
35 printf (" Enter price : ") ;
36 scanf ("% f " , & newBook . price ) ;
37 printf (" Enter published date ( dd mm yyyy ) : ") ;
38 scanf ("% d % d % d " , & newBook . published . day , &
newBook . published . month , & newBook . published
. year ) ;
39
48 void listBooks () {
49 if ( book_count == 0) {
50 printf (" No books in the library .\ n ") ;
51 } else {
52 for ( int i = 0; i < book_count ; i ++) {
53 printf (" Book % d :\ n " , i + 1) ;
54 printf (" Title : % s \ n " , library [ i ]. title ) ;
55 printf (" Author : % s \ n " , library [ i ]. author ) ;
56 printf (" Pages : % d \ n " , library [ i ]. pages ) ;
57 printf (" Price : $ %.2 f \ n " , library [ i ]. price )
;
58 printf (" Published : %02 d -%02 d -% d \ n \ n " ,
library [ i ]. published . day , library [ i ].
published . month , library [ i ]. published .
year ) ;
59 }
60 }
61 }
62
63 void deleteBook () {
64 if ( book_count == 0) {
65 printf (" No books to delete .\ n ") ;
66 } else {
67 int index ;
68 printf (" Enter the book number to delete : ") ;
69 scanf ("% d " , & index ) ;
70 if ( index < 1 || index > book_count ) {
9
71 printf (" Invalid book number .\ n ") ;
72 } else {
73 for ( int i = index - 1; i < book_count -
1; i ++) {
74 library [ i ] = library [ i + 1];
75 }
76 book_count - -;
77 printf (" Book deleted successfully !\ n ") ;
78 }
79 }
80 }
81
82 int main () {
83 int choice ;
84 while (1) {
85 printf ("\ nLibrary Menu :\ n ") ;
86 printf ("1. Add a title \ n ") ;
87 printf ("2. List all titles \ n ") ;
88 printf ("3. Delete a title \ n ") ;
89 printf ("4. Exit \ n ") ;
90 printf (" Enter your choice : ") ;
91 scanf ("% d " , & choice ) ;
92
93 switch ( choice ) {
94 case 1:
95 addBook () ;
96 break ;
97 case 2:
98 listBooks () ;
99 break ;
100 case 3:
101 deleteBook () ;
102 break ;
103 case 4:
104 return 0;
105 default :
106 printf (" Invalid choice . Please try
again .\ n ") ;
107 }
108 }
109 return 0;
110 }
10