1 CSC103: Introduction to Computer and Programming Lecture No 24.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to C Programming
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Programming Languages and Paradigms The C Programming Language.
Introduction to Programming Lecture 39. Copy Constructor.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures in C.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Kernighan/Ritchie: Kelley/Pohl:
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
CPS120: Introduction to Computer Science Lecture 15A Structures.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 CSC241: Object Oriented Programming Lecture No 05.
1 CSC103: Introduction to Computer and Programming Lecture No 19.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
11 Introduction to Object Oriented Programming (Continued) Cats.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Local Variables, Global Variables and Variable Scope
C Programming Lecture-8 Pointers and Memory Management
Structures In C Programming By Rajanikanth B.
C Programming Pointers
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 24

2 Previous lecture Exercise program Two dimensional array of characters Array of pointer to string Garbage value Dynamic memory allocation Free function

3 Today’s lecture outline malloc malloc function for integer array Structures Declaring a structure Accessing structure elements How structure elements are stored Array of structure

4 malloc function for integer array *p ni Program output Enter no. of subjects : 3 malloc(3*4) = malloc(12) 6550 Marks 1 = Enter marks of subject 1 : Enter marks of subject 2 : 85 2 Enter marks of subject 3 : Marks 2 = 85 2 Marks 3 = 78 3

5 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 NamePricePages Let us C Kite runner

6 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

7 Cont. Three separate array approaches Structure variable 01 2 price[] 01 2 pages[] 01 2 name[] s1s2s3 name price pages

8 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

9 Three separate array approach Go to program

10 Structure approach b1b2b3 name price pages Go to program

11 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 struct book This statement defines a new data type called struct book

12 Declaring structure variable b1b2 b3struct book This statement declare three variables b1, b2 and b3 of struct book type. Now the memory will be allocated for these three variables These bytes are always in adjacent memory locations b1 b2b3 name price pages

13 Cont. Structure definition and declaration of structure variables can be combined Variable declaration Definition Structure definition Variable declaration

14 Variable initialization Structure variable can be initialize at declare time as well from the input by user

15 Scope of structure Local scope – If the structure is define inside any function, it will only be accessible within that function Global scope – If structure is define outside function, it will be accessible to all the function in that program Go to program

16 Accessing Structure Elements In arrays individual elements are accessed using a subscript A dot (.) operator is used with structure variable name to access individual element of structure For example b1.price = ; b1.pages = 450; strcpy(b1.name, “let us c”);

17 b[5] Array of Structures name[10] pages price Go to program

18 Additional feature The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator

19 Cont. One structure can be nested within another structure Go to program

20