0% found this document useful (0 votes)
2 views

Intro to Programming Week 8

This document outlines the curriculum for BCSC 1102: Intro to Programming, focusing on structures and unions in C programming. It covers topics such as declaring and using structures, nested structures, arrays of structures, and provides examples including a simple library system. The course is taught by Dr. Shem Mbandu Angolo at The Cooperative University of Kenya from September to December 2024.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Intro to Programming Week 8

This document outlines the curriculum for BCSC 1102: Intro to Programming, focusing on structures and unions in C programming. It covers topics such as declaring and using structures, nested structures, arrays of structures, and provides examples including a simple library system. The course is taught by Dr. Shem Mbandu Angolo at The Cooperative University of Kenya from September to December 2024.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

BCSC 1102 : Intro To Programming Week 8

Dr. Shem Mbandu Angolo, PhD

The Co-operatetive University of Kenya


September - December 2024

Contents
1 Introduction to Structures 2

2 Declaring and Using Structures 2


2.1 Declaring Structures . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.1.1 Basic Structure Declaration . . . . . . . . . . . . . . . . . 2
2.1.2 Declaring and Initializing Structure Variables . . . . . . . 2
2.1.3 Typedef for Structures . . . . . . . . . . . . . . . . . . . . 2
2.2 Using Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3 Nested Structures and Arrays of Structures 3


3.1 Nested Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Arrays of Structures . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.3 Using Arrays in Structures . . . . . . . . . . . . . . . . . . . . . . 5

4 Introduction to Unions 6
4.1 Declaring Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.2 Using Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

5 Example: Simple Library System 8


5.1 Library System Description . . . . . . . . . . . . . . . . . . . . . 8

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.

2 Declaring and Using Structures


2.1 Declaring Structures
A structure is declared using the struct keyword. There are several ways to
declare a structure in C.

2.1.1 Basic Structure Declaration

1 struct Book {
2 char title [50];
3 char author [50];
4 int pages ;
5 float price ;
6 };

2.1.2 Declaring and Initializing Structure Variables

1 // Declare and initialize in one step


2 struct Book book1 = {" The Great Gatsby " , " F . Scott
Fitzgerald " , 218 , 10.99};
3

4 // Declare and initialize using dot notation


5 struct Book book2 ;
6 strcpy ( book2 . title , "1984") ;
7 strcpy ( book2 . author , " George Orwell ") ;
8 book2 . pages = 328;
9 book2 . price = 14.99;

2.1.3 Typedef for Structures


Using typedef allows the definition of a new type name for the structure, sim-
plifying variable declaration.
1 typedef struct {
2 char title [50];
3 char author [50];
4 int pages ;
5 float price ;

2
6 } Book ;
7

8 Book book3 = {" To Kill a Mockingbird " , " Harper Lee " ,
281 , 7.99};

2.2 Using Structures


To use a structure, we need to create a variable of that structure type.
1 # include < stdio .h >
2 # include < string .h >
3

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

14 // Assigning values to structure members


15 strcpy ( book1 . title , " The Great Gatsby ") ;
16 strcpy ( book1 . author , " F . Scott Fitzgerald ") ;
17 book1 . pages = 218;
18 book1 . price = 10.99;
19

20 // Accessing structure members


21 printf (" Title : % s \ n " , book1 . title ) ;
22 printf (" Author : % s \ n " , book1 . author ) ;
23 printf (" Pages : % d \ n " , book1 . pages ) ;
24 printf (" Price : $ %.2 f \ n " , book1 . price ) ;
25

26 return 0;
27 }

3 Nested Structures and Arrays of Structures


3.1 Nested Structures
Structures can contain other structures as members.
1 # include < stdio .h >
2 # include < string .h >

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

21 // Assigning values to structure members


22 strcpy ( book1 . title , "1984") ;
23 strcpy ( book1 . author , " George Orwell ") ;
24 book1 . pages = 328;
25 book1 . price = 14.99;
26 book1 . published . day = 8;
27 book1 . published . month = 6;
28 book1 . published . year = 1949;
29

30 // Accessing structure members


31 printf (" Title : % s \ n " , book1 . title ) ;
32 printf (" Author : % s \ n " , book1 . author ) ;
33 printf (" Pages : % d \ n " , book1 . pages ) ;
34 printf (" Price : $ %.2 f \ n " , book1 . price ) ;
35 printf (" Published : %02 d -%02 d -% d \ n " , book1 .
published . day , book1 . published . month , book1 .
published . year ) ;
36

37 return 0;
38 }

3.2 Arrays of Structures


Structures can be used in arrays, allowing the storage of multiple records.
1 # include < stdio .h >
2 # include < string .h >
3

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

14 // Assigning values to the first book


15 strcpy ( library [0]. title , " To Kill a Mockingbird ") ;
16 strcpy ( library [0]. author , " Harper Lee ") ;
17 library [0]. pages = 281;
18 library [0]. price = 7.99;
19

20 // Assigning values to the second book


21 strcpy ( library [1]. title , " Pride and Prejudice ") ;
22 strcpy ( library [1]. author , " Jane Austen ") ;
23 library [1]. pages = 279;
24 library [1]. price = 9.99;
25

26 // Accessing array of structures


27 for ( int i = 0; i < 2; i ++) {
28 printf (" Book % d :\ n " , i + 1) ;
29 printf (" Title : % s \ n " , library [ i ]. title ) ;
30 printf (" Author : % s \ n " , library [ i ]. author ) ;
31 printf (" Pages : % d \ n " , library [ i ]. pages ) ;
32 printf (" Price : $ %.2 f \ n \ n " , library [ i ]. price ) ;
33 }
34

35 return 0;
36 }

3.3 Using Arrays in Structures


Structures can also contain arrays as members.
1 # include < stdio .h >
2 # include < string .h >
3

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

13 data . intValue = 10;


14 printf (" data . intValue : % d \ n " , data . intValue ) ;
15

16 data . floatValue = 220.5;


17 printf (" data . floatValue : %.2 f \ n " , data . floatValue )
;

6
18

19 strcpy ( data . strValue , " Hello , World !") ;


20 printf (" data . strValue : % s \ n " , data . strValue ) ;
21

22 return 0;
23 }

4.1 Declaring Unions


Unions are declared using the union keyword.
1 union Data {
2 int intValue ;
3 float floatValue ;
4 char strValue [20];
5 };

4.2 Using Unions


Unions are used similarly to structures.
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

13 // Assigning and accessing union members


14 data . intValue = 10;
15 printf (" data . intValue : % d \ n " , data . intValue ) ;
16

17 data . floatValue = 220.5;


18 printf (" data . floatValue : %.2 f \ n " , data . floatValue )
;
19

20 strcpy ( data . strValue , " Hello , World !") ;


21 printf (" data . strValue : % s \ n " , data . strValue ) ;
22

23 return 0;

7
24 }

5 Example: Simple Library System


5.1 Library System Description
A simple library system that manages book records using structures and a menu
system for user interaction.
1 # include < stdio .h >
2 # include < string .h >
3

4 # define MAX_BOOKS 100


5

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

20 struct Book library [ MAX_BOOKS ];


21 int book_count = 0;
22

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

40 library [ book_count ] = newBook ;


41 book_count ++;
42 printf (" Book added successfully !\ n ") ;
43 } else {
44 printf (" Library is full !\ n ") ;
45 }
46 }
47

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

You might also like