1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structure.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
A program example is given below to input date and display on the screen by write a class ‘ date ‘ # include Class date { Private: Int y, m, d ; Publice.
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 11 – Structured Data
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
1 C++ Structures Starting to think about objects...
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.
Programmer Defined Structures (Records)
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
+ 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.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Principles of Computer Science I Honors Section Note Set 9 CSE 1341.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
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.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
Struct Data Type in C++ What Are Structures?
Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0;
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
1 C++ Classes and Data Structures Course link…..
Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their own range (or domain) of data and their own set of.
DATA HANDLING.
Structures Lesson xx In this module, we’ll introduce you to structures.
Structures in C++.
One-Dimensional Array Introduction Lesson xx
Starting to think about objects...
Struct Data Type in C++ What Are Structures?
Starting to think about objects...
Programming Fundamental
Presentation transcript:

1 Principles of Programming I Note Set #12

2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters and Strings Structures/Records Binary File I/O

3 Overview Structured Data

4 Concept A structure is a container – It can hold a collection of pre-defined variables Used to organize these things into nice, neat packages

5 What’s in a struct? Each part in a structure is called a member Each member has a name, type and value Names of members follow rules of variable names Types can be any pre-defined types

6 Simple Structure Definition struct studentGrades { int hw1;// homework grades int hw2; int test1;// test grades int final; // final grade float avg;// final average }; structure name structure body

7 Using a struct By declaring a struct, you are in essence creating a new data type Once you have the struct defined, you can now create variables of the new type studentGrades stu1; studentGrades stu2;

8 Accessing Members You can treat the members of a struct just like variables Access the members of a struct using ‘.’ notation (dot-notation) cout << stu1.hw1; cout << stu2.hw1; 2 different variables

9 Accessing Members Still use ‘.’ notation to store data in struct members. stu1.test1 = 89; stu2.test2 = 99; Struct variable name Member of struct

10 Pointers to Strctures Because our structure is a type, we can declare a pointer to it. We can dynamically allocate a structure studentGrades *sgPtr; sgPtr = new studentGrades;

11 Pointers to Strctures To access a member with a pointer, we use the indirection operator ( -> ) studentGrades *sgPtr; sgPtr = new studentGrades; sgPtr->test1 = 101; cout test1; cin >> sgPtr->test2;

12 Arrays of Structures Since your struct is a type, you can have arrays of the structure. studentGrades cse1341[15]; cse1341[1].test1 = 93; cse1341[2].hw2 = 15; cout << “Test 1: “ << cse1341[1].test1;

13 Passing Structs Can pass to functions by Value, Reference, or Pointer Remember: Passing by value won’t change the original parameter from the calling function.

14 Passing By Value //in main cout << stu1.avg; updateAvg(stu1); cout << stu1.avg; void updateAvg(studentGrades sg) { float sum = sg.hw1 + sg.hw2 +sg.test1 + sg.final; sg.avg = sum/220; } Won’t Work!!!

15 Passing By Reference //in main cout << stu1.avg; updateAvg(stu1); cout << stu1.avg; void updateAvg(studentGrades &sg) { float sum = sg.hw1 + sg.hw2 +sg.test1 + sg.final; sg.avg = sum / 220; } Pass by Reference

16 Passing by Pointer //in main cout << stu1.avg; updateAvg(&stu1); cout << stu1.avg; void updateAvg(studentGrades * sg) { float sum = sg->hw1 + sg->hw2 +sg->test1 + sg->final; sg->avg = sum/220; }

17 DON’T DO THIS! Can’t initialize members in struct definition! struct geoInfo { char cityName[25]=“Dallas”; char state[15]; unsigned int population= ; };

18 Where to put struct definitions Can define anywhere before they are used. Typically (and required for 1341) they are placed in a separate file called a header file myStructFile.h. Use #include “myStructFile.h” quotes for your own header files

19 Structure Exercise Write a program that uses the geoInfo struct. We need to be able to keep track of up to 10 cities and need functionality that will allow us to find the average population of all the cities.

20 Arrays of Structures struct payInfo { int hours; float payRate; }; An Array of 5 payInfo Structures would be: payInfo employees[5];

21 Arrays of Structures cout << “Enter the hours worked by 5 “ << “employees and their hourly” << “ rates.” << endl; for (int i = 0; i < 5; i++) { cout << “Emp “ << i+1 << “:”; cout << “ Hours: “; cin >> employees[i].hours; cout << “ Pay Rate: “; cin >> employees[i].payRate; }

22 Arrays of Structures Write a function that will calculate and return the total amount the company will have to pay out for the 5 employees for the pay period.

23 Defining and Initializing payInfo worker1 = {40, 5.75}; payInfo emps[3] = { {40, 5.75}, {32, 9.00}, {41, 15.00} }; payInfo Worker[50] = { {0} }; Initializes the 1 st member of the 1 st element to this, and the rest to 0, just like before…

24 Defining and Initializing Declare an array of 5 payInfo structs and initialize the hours member to 40. payInfo abc[5] = { {40}, {40}, {40}, {40}, {40} };

25 Nested Structures A structure can be a member of another structure A structure can not have a member of its own data type. However, it can have a pointer to its own datatype

26 Nested Structures struct date { int mon; int day; int yr; }; struct place { char street[20]; char city[10]; char state[2]; char zip[7]; }; struct empInfo { char name[20]; int payRate; date birthDate; place homeAddy; };

27 Using Nested Structrues empInfo emp1; strcpy(emp1.name, “John Doe”); emp1.payRate = 5.75; emp1.birthDate.mon = 8; emp1.birthDate.day = 18; emp1.birthDate.yr = 1965; strcpy(emp1.homeAddy.street, “Airline”); strcpy(emp1.homeAddy.city, “Dallas”); strcpy(emp1.homeAddy.state, “TX”); strcpy(emp1.homeAddy.zip, “75275”);

28 Nested Structures struct empInfo { char Name[20]; int payRate; date birthDate; place homeAddy; } Notice that we are moving towards grouping variables together that represent one thing or one concept.

29 Nested Structures struct locat { float x; locat y; locat z; }; Structure can’t have a member whose data type is that structure

30 Nested Structures struct node { int pieceOfData; node * next; }; Used in a very powerful data structure called the linked list.

31 Structure Storage struct myLilStruct { int myInt; float myFloat; char myChar[5]; };

32 More with Structures and Functions

33 Structures and Functions struct rectangle { float length; float width; };

34 Structures and Functions void display(rectangle r) { cout << r.length << endl; cout << r.width << endl; } int main() { rectangle r = {5, 10}; display(r); return 0; }

35 Structures and Functions rectangle getInput() { rectangle rec; cout << “Enter length: “; cin >> rec.length; cout << “Enter width: “; cin >> rec.width; return rec; } int main() { rectangle r; r = getInput(); return 0; }

36 Questions?? ?