CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
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.
Introduction to Programming Lecture 39. Copy Constructor.
Structures Spring 2013Programming and Data Structure1.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 CS161 Introduction to Computer Science Topic #10.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
1 CS161 Introduction to Computer Science Topic #15.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
User-Written Functions
Chapter 10-1: Structure.
OOP-4-Templates, ListType
Object Oriented Programming
Student Book An Introduction
Visit for more Learning Resources
CS 1430: Programming in C++.
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
CS150 Introduction to Computer Science 1
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
Introduction to Programming
Classes and Objects.
CS150 Introduction to Computer Science 1
Topics discussed in this section:
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
CS150 Introduction to Computer Science 1
CS111 Computer Programming
CISC181 Introduction to Computer Science Dr
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Structures In C Programming By Rajanikanth B.
CS150 Introduction to Computer Science 1
Data Structures and Algorithms Introduction to Pointers
Fundamental Programming
Submitted By : Veenu Saini Lecturer (IT)
CS250 Introduction to Computer Science II
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 9: Pointers and String
CS150 Introduction to Computer Science 1
Structure (i.e. struct) An structure creates a user defined data type
Structures in c By Anand George.
Structures Structured Data types Data abstraction structs ---
Standard C++ Library Part II.
CS148 Introduction to Programming II
Introduction to data structures
CS150 Introduction to Computer Science 1
Structures, Unions, and Enumerations
Presentation transcript:

CS150 Introduction to Computer Science 1 Structs 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Last Time Today we will Look at a new way of storing data called structs (short for structures) 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Arrays and Data Types Useful for storing a collection of data elements of the same data type (float, int, string). char myName[5]; //All elements chars float salaries[NUM_EMP]; //All elements floats char vowels[]={‘A’,’E’,’I’,’O’,’U’}; What about storing a collection of data elements of different data types? 11/23/04 CS150 Introduction to Computer Science 1

Data with Different Data Types For example, what if we wanted to keep the following information on a particular employee: employee id SS# number of children salary citizen The elements have different data types, so we can’t conveniently use array. Instead we will use a struct ( short for structure). 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Structure Definition To store this information: employee id SS# number of children salary citizen We would begin by defining a structure : struct employ { int id int ssnum; int numchild; float salary; bool citizen; }; 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Struct Terminology For this struct: struct employ { int id int ssnum; int numchild; float salary; bool citizen; }; employ is the identifier name and a NEW data type. The individual components id, ssnum, etc. are called members. 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Struct Declaration As with all data types, in order to use our new data type employ we must allocate storage space by declaring variables of this data type: employ engineer, tech; This will allocate space for two variables called engineer and tech with the previously described members id, ssnum, etc. 11/23/04 CS150 Introduction to Computer Science 1

Member Access Operator To access a struct member, we use the member access operator (period between struct variable name and member name). In the variable engineer of data type employ we can make the assignments: engineer.id = 12345; engineer.ssnum = 534334343; engineer.numchild = 2; engineer.salary = 45443.34; engineer.citizen = true; How do we access the data in arrays? 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example One Write a C++ struct data type realnum that will have members number, realpart, and intpart. Declare a variable numinfo of that type. Place the value 3.14159 in the field number. 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example One Solution struct realnum { float number; int realpart; int intpart; }; realnum numinfo; numinfo.number=3.14159; Define struct realnum Declare variable numinfo Assign member number 11/23/04 CS150 Introduction to Computer Science 1

Structs as function arguments Structs can be passed to functions by reference or value in the same manner that other data types have been passed. Generally, passing structs by reference is preferred since passing by value requires a local copy of the struct be created within the function’s variables. 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Two Write a C++ function split that accepts a variable of type realnum. Assign the integer part of the number to the member variable intpart and the real part of the number to the member variable realpart. 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Two Solution Function prototype: void split(realnum &); Function call: split (numinfo); Function definition: void split(realnum & numberinfo) { // Use numberinfo.number,numberinfo.intpart, // and numberinfo.realpart. } 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Three Consider the following struct data type: struct info { int num; int divisors[10]; int howmany; }; Write a C++ function compute that accepts a variable of type info and returns all the divisors greater than 1 of the variable num in the array divisors and the number of divisors in the variable howmany. 11/23/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we covered structures 11/23/04 CS150 Introduction to Computer Science 1