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

Unit 5 18CSS101J Programming for Problem Solving Detailed lecture slides covering all topics as prescribed by the curriculum for the academic year. Important and Necessary Exam Material

Typology: Slides

2017/2018

Uploaded on 02/20/2022

vasundhara-jhobta
vasundhara-jhobta 🇺🇸

4.6

(8)

8 documents

1 / 108

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18CSS101J Programming for Problem Solving
Unit V
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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