0% found this document useful (0 votes)
27 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)
27 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 knowledge ming 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 knowledge SS 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; <1” Stuoture tag void main(void) { clrscr(); printf(“\n Enter the roll number’ scanf("“%d",&stud1.roll_no); 9 ——————_ Using dot. we are accessing ~ member roll_no printf(‘"\n Enter the name "); scanf("%s" stud1.name); printf(""\n Enter the marks"); scanf("%f" &stud1 marks); printf("'\n The record of the student is’ printf(“\n\n Rollno Name Marks’ printf("\n-———~"); | printi("\n%d %s %.2f ",stud1 roll_no,stud1 name,stud!.marks); getch(); 7 Using dot operator each ‘member is printed Output Enter the roll number 1 Enter the name Shrinath Enter the marks 91.44 ‘The record of the student is TECHNICAL PUBLICATIONS® - an up-thrust for knowledge CProgrammingand DataStructures 25 Progrnneing- Aavancnd Fentres Rollno Name Marks —— 1 Shrinath 91.44 Ex. 2.1.2 Write a C:program to represent a complex number using structure and add two complex numbers. Sol. : #include #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 knowledge ata 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 { <100;i+ +) printf("\n Enter Roll No. scanf("%d" Berli]); printf("\n Enter name of student: scanf("%s"n{i}); printf("\n Enter Marks: "); scanf("%d" &emfil); for(i=0;i<100;i+ +) { if(m|i]>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( '<100;i+ +) TECHNICAL PUBLICATIONS® - an up-thrust for knowledge ye (Programming and Data Structures 2-9 __C Prograrnening,- Advanced Features print{("\nEnter Roll Ni scanf("sd" &s[i].roll_no); printf("\nEnter name of student scanf("%s",s|i].sname); printf("\nEnter marks: "); scant("séd'' &s[i).Total_Marks); printf("\n Student with maximum marks is. printf("\n Roll.No,\t Name \t Total Marks printf(‘\n %d %s. ‘%d"s{j].r0ll_no,s{j].sname,s{j].Total_Marks); break; } } Ex. 21.4 Write down only declaration in ‘C’ for : A database of 100 students each with information as roll no, name, address and marks using : i) Only arrays ii) Array of structures. Sol: i) Only arrays : int roll no {100}; char name [100] [100]; char address [100] [30]; float marks [100]; i | | li) Array of structures : struct student { int roll no; char name [20]; } char address [50]; | 4 TECHNICAL PUBLICATIONS® - an up-thrust for knowledge 2-10 ____C Programming - Advanced naming and Data Strvctures - C Progr float marks; } s{100); Ex, § Develop a structure to represent planet in the solar system. Each planet hag ~ felite for the planet's name, its distance from the sun in miles and the rmuntber of moons it has. Write a program to read the data for each plane, vad store. Also print the name of the planet that has less distance from the sun, Sol. : Following table shows planets in the solar system, their distances from sun in na. and number of moons. Name Distance from sun in miles Moons Mercury 36 million “Venus a million : 93 million [es Mars 142 million Jupiter 483 million Ee Saturn 888 million 53 ‘Uranus 1784 7 2 Neptune 2794 13 Pluto 2647 3 C Programming and Data Structures C Prograrnening,- Advanced Feat n = 9;//number of planets print{("\n Enter Each Planet's record"); for (i = 0; i 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 knowledgo ngand 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<10;i++) {: scanf("%d" &c[i].roll_no); scanf("%s',cli] name); scanf("%d" &cli).std); scanf('%s',c[i].address); } printf("\n Each record is"); printf("\n- "; printf("\n Roll no Name Std Address’ % printf('\n %d\t%s \t%d \t %s'\cfi].roll_no,cfi].name,cfi}.std,cli].address); TECHNICAL PUBLICATIONS® - an up-thrust for knowledge C Programming and Data Structures 2-16 © Programming Advanced fay 7 “ay, } retum 0. } Ex. 2.18: Explain structures and write a C program using structures to store Ta ame, marks itt 10 subjects of 100 students, Calewlate the grade of each student ang the student information. The grades are calculated as follows - Letter Grade Grade Points Marks_Range 10 91-100 | Ax(Execllent) 9 81-90 |_Ateery Good 8 71-80 |_BHGooa 7 61-70 | BiAverage) 50 - 60 i | RA o aeouie ‘AU Sol. : #include #include #include struct student { int roll_no; char sname|20}; int Marks|10}; char “grade; }s11001; void main() { int if for(i=0; i<100;i++) { printf("\nEnter Roll No: "); scanf("%d",&s[i].roll_no); printf("\nEnter name of student: scanf("%s",s[i].sname); for(j=0;}<10j++) Prin, Marks TECHNICAL PUBLICATIONS® - an up-thrust for knowledge a 1 Saag wvwieg,: tow 5 = 0; it oe vorat_marke-+i}- Marko && total_marks <= 100) & total_marks <= 4) 1 ='0" marks? 61 & he <=) Roll.No.\tName\tGrade "); forli=0'< 1001+ +) { printf("\n %d 3 printf(\1 \tms\t%s"s[i).r0ll_no,s[i].sname si ace: Structure within a Structure hen a structure is declared in a structure then it i: #include #include #include struct bdate { . int day; int month; TECHNICAL PUBLICATIONS® ~ an otras: te o C 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