Introduction to Programming

Slides:



Advertisements
Similar presentations
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Advertisements

1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
 Review structures  Program to demonstrate a structure containing a pointer.
Object Oriented Programming (OOP) Lecture No. 11.
Today’s Material Aggregate Data Types: Structures and Unions
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
CPS120: Introduction to Computer Science Lecture 15A Structures.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
Introduction to Programming
Student Book An Introduction
Structures.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
EECE.2160 ECE Application Programming
Data Types – Structures
C++ Data Types Simple Structured Address Integral Floating
DATA HANDLING.
CS 1430: Programming in C++.
Dynamic Memory Allocation Reference Variables
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Data Types – Structures
Structures in C++.
Heterogeneous aggregate datatypes
Structure ការណែនាំអំពី Structure
Introduction to Programming
Constant pointers and pointers to constants
Strings A collection of characters taken as a set:
CS148 Introduction to Programming II
Structure As Function Arguments
Chapter 1: Introduction to Data Structures(8M)
EECE.2160 ECE Application Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Introduction to Programming
EECE.2160 ECE Application Programming
Starting to think about objects...
CHAPTER 4 File Processing.
C Programming Lecture-13 Structures
Introduction to Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
C Programming Lecture-14 Unions
EECE.2160 ECE Application Programming
Structure (i.e. struct) An structure creates a user defined data type
Introduction to Programming
Structures Structured Data types Data abstraction structs ---
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CSCE 206 Lab Structured Programming in C
EECE.2160 ECE Application Programming
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
A little bit about optimization, 2d array, 1d array used as 2d, register volatile union .
Programming Fundamental
Programming Fundamental
Presentation transcript:

Introduction to Programming Lecture 20

In the Last Two Lectures File Handling Sequential Files Random Access Files

Today’s Lecture Structures Unions

Structure ?

} Structure definition struct Student { char name [ 60 ] ; Keyword Structure name struct Student { char name [ 60 ] ; char address [ 100 ] ; Data of the structure double gpa ; } ; }

} Student s1 , s2 , s3 ; Structure Name Variable Names

Structure struct address { char streetAddress [ 100 ] ; char city [ 30 ] ; char country [ 30 ] ; }

Structure struct Student { char name [ 60 ] ; address add ; double gpa ; } s1 , s2 , s3 ;

Example 1 struct Card { char *suit ; char *value ; } ;

Variables of type structure Structures Variables of type structure Pointers to Structure Array of Structure

Structures Student s [ 100 ] ; Student *sPtr ;

Structures Student stdnt1 , stdnt2 ; stdnt1 + stdnt2 ; //Wrong

Structures Student s1 , s2 ; s1 = s2 ;

Example 2 struct Student { char name [ 64 ] ; char course [ 128 ] ; int age ; int year ; } ;

Initializing Structures Student s1 = { “Amara”,“cs201”,“19”, “2002” } ; Name year Course name age

Initializing Structures s1.name ; s1.course ; s1.age ; s1.year ;

Initializing Structures s1.age = 20 ; s1.name = “Abbas Ali ” ; //Wrong strcpy ( s1.name , “Abbas Ali ” ) ;

Accessing structure members cout << s1.name ; cout << s1.course ; cout << s1.age ; cout << s1.year ;

Assignment of structures Student s1 , s2 ; s2 = s1 ;

Passing Structures to Functions

Passing Structures to Functions Call by value Call by Reference X

int Square ( int x ) ;

x = Square ( 10 ) ;

Structures Simple Variable of type Structure Pointer to Structure Arrays of Structures Function that returns a Pass the Structure to functions

Pointers to Structure

Pointers to Structure Student *sPtr , s1 ; sPtr = &s1 ;

Pointers to Structure sPtr.name ; Wrong

Pointers to Structure *sPtr.name ;

Pointers to Structure *( sPtr ).name ;

Pointers to Structure *sPtr ->name ; Same as s1.name

Arrays of Structures

Arrays of Structure Student s [ 100 ] ; s [ 0 ].name ; s [ 1 ].name ;

sizeof ( s1 ) ;

Example 3 struct Student { char firstName [ 30 ] ; char lastName [ 30 ] ; char course [ 15 ] ; char rollNo [ 10 ] ; int age ; float gpa ; } ;

Example 3 Student s [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { cout << “Please enter the student's last name : " ; cin >> s [ i ].lastName ; cout << “Please enter the student's first name : " ; cin >> s [ i ].firstName ; cout << " Please enter the student's course : " ; cin >> s [ i ].course ; cout << " Please enter the student's Roll No. : " ; cin >> s [ i ].rollNo ; cout << " Please enter the student's grade : " ; cin >> s [ i ].grade ; cout << " Please enter the student's age : " ; cin >> s [ i ].age ; cout << " Please enter the student's GPA : " ; cin >> s [ i ].gpa ; }

Example 4 Student getData ( ) ; Return Type Function Name

Example 4 ifstream inFile ( “myText.txt” ) ; void main ( ) { Student s1 ; s1 = getData ( ) ; inFile.close ( ) ; }

Example 4 Student getData ( ) { Student tempStudent ; inFile >> tempStudent.firstName ; inFile >> tempStudent.lastName ; inFile >> tempStudent.course ; inFile >> tempStudent.rollNo ; inFile >> tempStudent.age ; inFile >> tempStudent.gpa ; return tempStudent ; }

Union

Union union intOrChar { int i ; char c ; } ;

Union Memory C Both Variable occupy the same space in memory i

Union union intOrDouble { int ival ; double dval ; } ;

Union Memory iVal Both Variable occupy the same space in memory dVal

cout << u1.dval ; incorrect value Union intOrDouble u1 ; u1.ival = 10 ; cout << u1.ival ; cout << u1.dval ; incorrect value

Union u1.dval = 100.0 ; cout << u1.ival ; incorrect value

Example 5 char c ; int x ; x = ‘a’ ; cout << x ; x = x * 256 :

Example 5 x = x +’b’ ; cout << x ;

Example 5 Union iandc { char c [ 4 ] ; int x ; } ;

Today we studied Structures Unions