Docsity

Unit 5 18CSS101J Programming for Problem Solving, Slides of Programming for Engineers

Partial preview of the text

Download Unit 5 18CSS101J Programming for Problem Solving and more Slides Programming for Engineers in PDF only on Docsity!

18 CSS 101 J – Programming for Problem Solving

Unit V

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

COURSE LEARNING RATIONALE (CLR) The purpose of learning this course is to: CLR - 1: Think and evolve a logically to construct an algorithm into a flowchart and a pseudocode that can be programmed CLR - 2: Utilize the logical operators and expressions to solve problems in engineering and real-time CLR - 3: Store^ and^ retrieve^ data^ in^ a^ single^ and^ multidimensional^ array CLR - 4: Utilize custom designed functions that can be used to perform tasks and can be repeatedly used in any application CLR - 5: Create storage constructs using structure and unions. Create and Utilize files to store and retrieve information CLR - 6: Create a logical mindset to solve various engineering applications using programming constructs in C

LEARNING RESOURCES S. No (^) TEXT BOOKS

Zed A Shaw, Learn C the HardWay: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C), AddisonWesley, 2015

W. Kernighan, Dennis M. Ritchie,The C Programming Language, 2nd ed. Prentice Hall, 1996

  1. Bharat Kinariwala,Tep Dobry, Programming in C, eBook
  2. http://www.c4learn.com/learn-c-programming-language/

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.
UNIT V
INTRODUCTION

Initializing Structure, Declaring Structure variable- Structure using typedef, Accessing members – Nested structure Accessing elements in a structure array – Array of structure Accessing elements in a structure array – Passing Array of Structure to function- Array of Pointers to structures- Bit Manipulation of structure and pointer to structure – Union Basic and declaration – Accessing Union Members Pointers to union

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

INTRODUCTION TO STRUCTURE

  • Problem:
    • How to group together a collection of data items

of different types that are logically related to a

particular entity??? ( Array )

Solution: Structure

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

8

STRUCTURE

❑ A Structure is a collection of variables of different data types under a

singlename.

❑ The variables are called members of the structure.

❑ The structure is also called a user-defined data type.

10

  • Example struct student { char name[20]; int roll_no; float marks; char gender; long int phone_no; }; struct student st;
  • Multiple variables of struct student type can be declared as: struct student st1, st2, st3;

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

11

Defining a structure…

• Each variable of structure has its own copy of member

variables.

• The member variables are accessed using the dot (.) operator or

memberoperator.

• For example: st 1 .name is member variable name of st 1

structure variable while st 3 .gender is member variable gender

of st 3 structure variable.

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

13

Structure initialization

• Syntax:

struct structure_name structure_variable={value1, value2, …, valueN};

• Note: C does not allow the initialization of

individual structure members within the

structure definition template.

struct student { char name[20]; int roll_no; float marks; char gender; long int phone_no; }; void main() { struct student st1={“ABC", 4, 79.5, 'M', 5010670}; clrscr(); printf("Name\t\t\tRoll No.\tMarks\t\tGender\tPhone No."); printf("\n.........................................................................\n"); printf("\n %s\t\t %d\t\t %f\t%c\t %ld", st1.name, st1.roll_no, st1.marks, st1.gender, st1.phone_no); getch(); } 14

16 struct student { char name[ 20 ]; int roll; char remarks; float marks; }; void main() { struct student s 1 ={“name", 4 }; clrscr(); printf("Name=%s", s 1 .name); printf("\n Roll=%d", s 1 .roll); printf("\n Remarks=%c", s 1 .remarks); printf("\n Marks=%f", s 1 .marks); getch(); }

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

17 Accessing member of structure/ Processing a structure

  • By using dot (.) operator or period operator or member operator.
  • Syntax: structure_variable.member
  • Here, structure_variable refers to the name of a struct type variable and member refers to the name of a member within thestructure.

19 struct student { char name[20]; int roll; float mark; }; void main() { struct student s; clrscr(); printf("Enter name:\t"); gets(s.name); printf("\n Enter roll:\t"); scanf("%d", &s.roll); printf("\n Enter marks:\t"); scanf("%f", &s.mark); printf("\n Name \t Roll \t Mark\n"); printf("\n...................................\n"); printf("\n%s\t%d\t%f", s.name, s.roll, s.mark); getch(); }

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

20

  • Two variables of the same structure type can be copied in the same way as ordinary

variables.

  • If student1 and student2 belong tothe same structure, then the following statements

are valid:

student 1 =student 2 ; student 2 =student 1 ;

  • However, the statements such as:

student1==student2 student1!=student

are not permitted.

  • If we need to compare the structure variables, we may do so by comparing members

individually.

Copying and Comparing Structure Variables