Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
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
Download now
Download
Save C & DS Unit - II For Later
Download
Save
Save C & DS Unit - II For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
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
Download now
Download
Save C & DS Unit - II For Later
Carousel Previous
Carousel Next
Save
Save C & DS Unit - II For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 87
Search
Fullscreen
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; <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 knowledgeCProgrammingand 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 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 { <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 knowledgeye (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 knowledge2-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 3C 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 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<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 knowledgeC 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 knowledgea 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 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
Slide 01 DS Preliminaries
PDF
No ratings yet
Slide 01 DS Preliminaries
142 pages
C Data Types:: Primary Data Types Derived Data Types User-Defined Data Types
PDF
No ratings yet
C Data Types:: Primary Data Types Derived Data Types User-Defined Data Types
9 pages
Unit4-C Data Types
PDF
No ratings yet
Unit4-C Data Types
10 pages
Structures in C Recap
PDF
No ratings yet
Structures in C Recap
8 pages
Lecture_Week_14.2
PDF
No ratings yet
Lecture_Week_14.2
32 pages
structures
PDF
No ratings yet
structures
8 pages
unit 4
PDF
No ratings yet
unit 4
19 pages
C Structure
PDF
No ratings yet
C Structure
53 pages
Module 4 PCD
PDF
No ratings yet
Module 4 PCD
25 pages
Unit-4 Structure
PDF
No ratings yet
Unit-4 Structure
13 pages
Struct: Structures
PDF
No ratings yet
Struct: Structures
13 pages
Unit IV Structures
PDF
No ratings yet
Unit IV Structures
33 pages
Module 5
PDF
No ratings yet
Module 5
17 pages
AUT - UNIT 4
PDF
No ratings yet
AUT - UNIT 4
28 pages
Structures Topics in Structures
PDF
No ratings yet
Structures Topics in Structures
23 pages
C Programming UNIT 4.3 Structure and Union
PDF
No ratings yet
C Programming UNIT 4.3 Structure and Union
10 pages
Structures Notes
PDF
No ratings yet
Structures Notes
13 pages
CS3353 Unit 2
PDF
No ratings yet
CS3353 Unit 2
26 pages
C Structure
PDF
No ratings yet
C Structure
14 pages
new UNIT-V
PDF
No ratings yet
new UNIT-V
75 pages
C (Chap 10 Structure & Union)
PDF
No ratings yet
C (Chap 10 Structure & Union)
14 pages
Unit 4 PWC
PDF
No ratings yet
Unit 4 PWC
21 pages
PSPC Unit 4 Notes
PDF
No ratings yet
PSPC Unit 4 Notes
25 pages
Structuret
PDF
No ratings yet
Structuret
26 pages
Unit 4 - Structures
PDF
No ratings yet
Unit 4 - Structures
43 pages
C Structures and Unions
PDF
No ratings yet
C Structures and Unions
61 pages
C Unit-1
PDF
No ratings yet
C Unit-1
33 pages
Unit 4-1
PDF
No ratings yet
Unit 4-1
34 pages
Unit 10- Structure and union
PDF
No ratings yet
Unit 10- Structure and union
7 pages
C Data Types:: Primary Data Types Derived Data Types User-Defined Data Types
PDF
No ratings yet
C Data Types:: Primary Data Types Derived Data Types User-Defined Data Types
8 pages
Chapter - 07: "Structures": (C - Programming)
PDF
No ratings yet
Chapter - 07: "Structures": (C - Programming)
12 pages
Module-4 CPS(Structures & Pointers)
PDF
No ratings yet
Module-4 CPS(Structures & Pointers)
18 pages
Module 4 Structures
PDF
No ratings yet
Module 4 Structures
17 pages
Structure, Union, Enumeration and File Concepts
PDF
No ratings yet
Structure, Union, Enumeration and File Concepts
20 pages
Structure
PDF
No ratings yet
Structure
50 pages
Unit 2
PDF
No ratings yet
Unit 2
44 pages
Unit 4 Final Notes
PDF
No ratings yet
Unit 4 Final Notes
21 pages
6 Structures in 'C'
PDF
92% (12)
6 Structures in 'C'
11 pages
Structure 4
PDF
No ratings yet
Structure 4
17 pages
Unit Ii-1
PDF
No ratings yet
Unit Ii-1
37 pages
Programming: Structures
PDF
No ratings yet
Programming: Structures
27 pages
Structure & Unions
PDF
No ratings yet
Structure & Unions
9 pages
Unit 9 Structure & Uniuon
PDF
No ratings yet
Unit 9 Structure & Uniuon
17 pages
Structure
PDF
No ratings yet
Structure
63 pages
Structure Union Enum Typedef
PDF
No ratings yet
Structure Union Enum Typedef
17 pages
08_structure
PDF
No ratings yet
08_structure
6 pages
Structures
PDF
No ratings yet
Structures
64 pages
Unit 2 Structure Notes APC
PDF
No ratings yet
Unit 2 Structure Notes APC
38 pages
"Structure and Union": Presented By: Sunil Subedi Roshan Ray Chaudary Ushan Buddhacharya
PDF
No ratings yet
"Structure and Union": Presented By: Sunil Subedi Roshan Ray Chaudary Ushan Buddhacharya
32 pages
Chapter 9
PDF
No ratings yet
Chapter 9
28 pages
Structure and Union in C
PDF
No ratings yet
Structure and Union in C
11 pages
EXP9 Writeup-1
PDF
No ratings yet
EXP9 Writeup-1
4 pages
DS Unit-1
PDF
No ratings yet
DS Unit-1
13 pages
Structure
PDF
No ratings yet
Structure
41 pages
POP Module 5
PDF
No ratings yet
POP Module 5
35 pages
Structures
PDF
No ratings yet
Structures
17 pages
Structures
PDF
No ratings yet
Structures
28 pages