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

C & DS Unit - II

Anna University Syllabus C Programming and Data Structure Unit 2 Notes.

Uploaded by

hostdkn73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
31 views

C & DS Unit - II

Anna University Syllabus C Programming and Data Structure Unit 2 Notes.

Uploaded by

hostdkn73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 87
C Programming - Advanced Features Syllabus Structures - Union - Enumerated Data Types - Pointers : Pointers to Variables, Arrays and Functions File Handling - Preprocessor Directives. Contents 29 22 23 24 25 26 27 28 29 ‘Structures Union - May-17, Dec.-18,19, me May-19, Difference between Data Type, Structures and Unions Enumerated Data Types Pointers Arrays and Functions File Handling Preprocessor Directives Two Marks Questions with Answers @-1 ](C Programming and Data Structures (Programming - Advanced Feat, geming- Advanced Features SCT 2.1 | Structures COREE Dr 2.1.1 | Defini nm Definition : A structure is a group of items in which each item is defined by some identifier. These items are called members of the structure. Thus structure is a collection of various data items which can be of different data types. The Syntax of declaring structure in C is as follows - struct name { Z member 1; member 2; The stud 1 and stud 2 will look like this roll_no name [10] member n; k Example : struct stud { int roll_no; char name[1 float marks; hk t struct stud studi,stud2; Using typedef An alternative to using a structure ta 8 is to use the structure tag is to use the typedef definition in C. For example typedef struct { int roll_no; char name[10}; float marks; Appear there ever you can assume the complete structure. The stud will act like a data {ype which will represent the above mentioned structure. S0 we ean decline the various structure variables using the tag stud like this. stud studi,stud2; TECHNICAL PUBLICATIONS® - an up-thrust for knowledgeming and Data Structures 2.1.2 | Comparison between Arrays and Structures Array 233 C Programening, - Advanced Featares ‘Structure Array is a collection of similar type of elements. Structure is a collection of variety of elements which can be of different data types. v Array elements can be accessed by the Structure elements can be accessed with index placed within [ ]. the help of . (dot) operator. a z 3. To represent an array, array name is To represent structure a keyboard struct followed by [ ]. has to be used. 4. Example : Example : int a 20}; Struct student { int roll_no; char name (20; a 2.1.3 | Initializing Structure There are three different ways by which structure can be defined. First Way : Writing structure variable after structure template struct student { int roll_no; char name[10]; float marks; Ist; Second Way : Declare structure variable using struct keyword struct student { int roll_no; char name|10}; float marks; i struct student s1; | Third Way : Declaring structure variables using typedef typedef struct student { int roll_no; char name|10]; an up-thrust for knowledgeSS C Programming and Data Structures 2-4 C Programming - Advanced Feature, ——————————S eee 1s S, sl; Ex. 2.1.1 Write a C program to initialize a structure and retrieve the values. Sol. : [EEE E ESE OIRO EEE SEES ISIN B ISIE SENIORS IEE IO DIET TTT ta, ‘This Program is for assigning the values to the structure variable. Also for retrieving the values. #include i #include #include struct student { int roll_no; char name[10]; float marks; jstudl; #include typedef struct Complex { float real; float img; 3; void main() { C xyz; elrscr(); printf(’\n Enter the real part of first complex number’); scanf("%f',8oc.eal); printf("\n Enter the imaginary part of first complex number’); scanf("%f",8x.img); printf("\n Enter the real part of second complex number’); scant ("vf",&y.real); printf("\n Enter the imaginary part of second complex number’); scanf(""éf",&y.img); printf("\n\t The First complex number is %3.2f+ %3.26i' x.real,x.img); printf(\n\t The second complex number is %3.2f + %.28 y.real,y.img); zreal=x.real+ y.real; zim, s+ yarn printf("\n The Addition is'%3.2f + %3.26\n",z.real,z.img); getch(); } 2.1.4 | Array of Structures Definition : Structure is used to store information of one particular object and if one has to store large number of such objects then array of structure is used. TECHNICAL PUBLICATIONS® - an up-thrust for knowledgeata St Programming and Data S For example - If there are 10 students in a class then the record of each Stud, in structure and 10 such records can be stored in array of structure, typedef struct student { roll_no | name | marks | int roll_no; stud[0) char name[20]; stud[1 int marks; stud[2} }stud/10}; #include #include #include typedef struct student { int roll_no; | char name|20}; i int marks; : } stud; stud s[10]; ved maid) “Saya { int ni; clrscr(); Print{("\n Enter the total number of students scanf(“%d" an); Print{("\n Enter Each student's record"); for(i=0;i +) 5 scanf(“%d" ,8s{iJ.roll_no); scanf("%s" ,s|i]. name); canf("%d" ,8s[i] marks); TECHNICAL PUBLICATIONS®Programming end Data Structures | _ a } printf("\n Each student's record is"); printf("\n— "); printf("\n roll_no name marks"); printf("\n- for(i=O;i #include #include TECHNICAL PUBLICATIONS® - an up-thrust for knowledge(C Programming and Data Structures struct student { a int roll_no; i char sname|20); int Total_Marks: }s[100); int 1{100},m[100) . char n[100][20); void main() { int i,jchoice; cuscr(); printi("\n 1. Array \n.2.Array of structure printi("\n Enter your choice: "); scanf("%d" choice); switch(choice) { case 1:fo1 { m{j))/*data with maximum mark is obtained*/ /* mark that record by index j*/ } printf("\n Student with maximum marks i print{("\n Roll.No.\t Name \t Total Marks "); printf(\n %d = %s_ = %d"rljJ.nljJ.mUj); break; case 2:for( ' Planet{i].distance) { min_dist = Planet|i].distance; position = i; } print{('\n The planet having less distance from the Sun is %s", Planet[position].name); Output Enter Each Planet's record Enter Name of the Planet Mercury _ Enter Distance of Planet from sun(in million miles)36 TECHNICAL PUBLICATIONS® - an up-thrust for knowledge_— C Programming and Data Structures Enter Number of Moons 0 Enter Name of the Planet Venus Enter Distance of Planet from sun(in million miles)67 Enter Number of Moons 0 Enter Name of the Planet Earth Enter Distance of Planet from sun(in million miles)93 Enter Number of Moons 1 Enter Name of the Planet Mars Enter Distance of Planet from sun(in million miles) 142 Enter Number of Moons 2 Enter Name of the Planet Jupiter Enter Distance of Planet from sun(in million miles)483 Enter Number of Moons 62 Enter Name of the Planet Satu Enter Distance of Planet from sun(in million miles)888 | Enter Number of Moons 53 Enter Name of the Planet Uranus Enter Distance of Planet from sun(in million miles)1784 Enter Number of Moons 21 TECHNICAL PUBLICATIONS® - an up-hnust for knowledgongand Data Structures 2-13 © Progpamneving- Rvrarend Festeces “i Enter Name of the Planet Neptune C Programm Enter Distance of Planet from sun(in million miles)2794 Enter Number of Moons 13 Enter Name of the Planet Pluto Enter Distance of Planet from sun(in million miles)2647 Enter Number of Moons 3 Each student's record is 0 0 1 2 Jupiter 483 62 Satum 888 53 Uranus 1784 a1 Neptune 2794 13 Pluto 2647 3 ‘The planet having less distance from the Sun is Mercury Ex. 2.1.6 Define a structure to store details of 10 bank customers with customer name,account no, balance, and city. Write a C program to store the details of customers in the bank, access and print the customer details for a specified account no. Sol. : #include #include #include typedef struct Customer { int account_no; char name|20); TECHNICAL PUBLICATIONS® - an upthrust for knowledge>. (C Programming and Data Structures 24 ramming Advanced Fea, bi int balance; char city|20]; Joust; cust [10]; int main() { int ni: Printi("\n Enter the total number of Customers"); Scanf("%d",&n); Printf("\n Enter Each customer's record’ East Acc_no Name Balance City\n") Scanf("%d',&c[i].account_no); scanf("%s' cli] name); scanf("%d" f&cli] balance); scant("%s' clil.city); } i rint{("\n Each record is" printf(‘\n- | Printi("\n Acc_no Name Balance Cio" | printf("\n— for(i=O;i #include #include typedef struct Student { int roll_no; char name|20}; int std; char address[20]; }oust; cust c[10]; int main() { int i; printf("\n Enter Each student's record"); printf("\n Roll no Name Std Address\n"); forli=0;i #include #include struct student { int roll_no; char sname|20}; int Marks|10}; char “grade; }s11001; void main() { int if for(i=0; i #include #include struct bdate { . int day; int month; TECHNICAL PUBLICATIONS® ~ an otras: te oC Programming - Advanced Feat, " eee Feat C Programming and Data Structures int year; Jat; struct address { char street[10]; char city[10}; Jedd; struct student { int roll_no; char name[10]; struct bdate dt; struct address add; }std; void main(void) { ‘elrscx(); Printf(“\n Enter the student's data"); printf(“\n Enter student's rollnumber scanf(“%d",&std.roll_no); printf(“\n Enter student's name"); scanf(“%s",std.name); Printf(“\n Enter student's Birth date(mm-dd-yy))”; scanf(“%d %d %d",&std.dt. month, &std.dt.day,&std.dt.year); printf(“\n Enter student's Address"); scanf(“%s %s",std.add_street,std.add.city); Printf(“\n\n You have entered "); Printf(“\n roll_no name B'date address"); printf("“\a printf(“\n %d%s ".std.roll_no,std.name); printf(" %d-%d-%d ",std.dt.month,std.dt.day,std.dt.year); %s,%s" std.add.street,std.add.city); printf(" getch(); Accessing nested members } Ex, 2.1.9 Write a C program with appropriate structure definition and variad -mployee, using nested structures. Consider th declaration to store information about an e1 following fields like ENAME, EMPID. DOIDate. Mouth. Yoar\ and Calavy (Racin TV—progrannanings andl Data Structures as ec EY Ctra tered Fats | Lt 50 judo jpotudo include gtruct JoiningDate { int Date; int Month; int Years i : struct Amount { m float Basic; float DA; float HRA; : ie Employee { char ENAME[20]; int EMPID; struct JoiningDate DOJ; struct Amount Salary; yEmp; _ void main(void) printf("\n Enter Name of Employee: "); scant("%s"Emp.ENAME); print{("\n Enter Employee ID: "); scanf("%d',&Emp.EMPID); : printf("\n Enter Date of Joining of Employee: printf("\n Enter Date: "); % scanf("%d",&Emp.DOJ.Date); printf(’\n Enter Month: "); scanf("%d', &Emp.DOJ.Month); printf("\n Enter Year; "); scanf("%d",&Emp.DOJ.Year); printf("\n Enter Salary of Employee: printf("\n Enter Basic: "); __ scanf{'%f",&Emp Salary Basic); | a TECHNICAL PUBLICATIONS® - an up-thrust for knowledge

You might also like