Đang chuẩn bị liên kết để tải về tài liệu:
Lecture Introduction to computer and programming - Lecture No 24
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
This model is supported by the four programming paradigms-imperative, objectoriented, functional, and logic. In this model, the program controls the sequence of steps that occur at run time, and the ordering of input plays a passive role in regulating how those steps are carried out. Moreover, this model supports algorithms that terminate once the input is exhausted. | CSC103: Introduction to Computer and Programming Lecture No 24 Previous lecture Exercise program Two dimensional array of characters Array of pointer to string Garbage value Dynamic memory allocation Free function Today’s lecture outline malloc function for integer array Structures Declaring a structure Accessing structure elements How structure elements are stored Array of structure malloc function for integer array *p n i Program output Enter no. of subjects : 3 malloc(3*4) = malloc(12) 6550 Marks 1 = 73 6554 6558 6550 3 Enter marks of subject 1 : 73 73 0 1 Enter marks of subject 2 : 85 85 2 Enter marks of subject 3 : 78 78 3 0 1 Marks 2 = 85 2 Marks 3 = 78 3 Structure Variable can store single value of one type Array can store multiple values of similar type Structure can store multiple values of similar or dissimilar type Structures are a collection of variables related in nature, but not necessarily in data type Book Name Price Pages Let us C 550.50 728 Kite runner 500.75 336 Why use structures Suppose we want to store data about a book You might want to store book’s name (a string), price (a float) number of pages (an int). If data about say 3 such books is to be stored, then we can follow two approaches: Construct three arrays, one for storing names, second for storing prices and third for number of pages Use of structure variables Cont. Three separate array approaches Structure variable 0 1 2 price[] 0 1 2 pages[] 0 1 2 name[] 1 2 3 3 3 1 1 2 2 s1 s2 s3 name price pages fflush function fflush(stdin); Standard input stream (stdin) is the default source of data for applications It is usually directed to the input device of the standard console (like a keyboard) Go to program Three separate array approach Go to program Structure approach b1 b2 b3 name price pages Go to program Structure definition Memory will not be allocated when you write function definition This is just to tell the C compiler that what are the elements of a variable of this structure This . | CSC103: Introduction to Computer and Programming Lecture No 24 Previous lecture Exercise program Two dimensional array of characters Array of pointer to string Garbage value Dynamic memory allocation Free function Today’s lecture outline malloc function for integer array Structures Declaring a structure Accessing structure elements How structure elements are stored Array of structure malloc function for integer array *p n i Program output Enter no. of subjects : 3 malloc(3*4) = malloc(12) 6550 Marks 1 = 73 6554 6558 6550 3 Enter marks of subject 1 : 73 73 0 1 Enter marks of subject 2 : 85 85 2 Enter marks of subject 3 : 78 78 3 0 1 Marks 2 = 85 2 Marks 3 = 78 3 Structure Variable can store single value of one type Array can store multiple values of similar type Structure can store multiple values of similar or dissimilar type Structures are a collection of variables related in nature, but not necessarily in data type Book Name Price Pages Let us C 550.50 728 Kite runner 500.75 336 Why