Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS150 Introduction to Computer Science 1

Similar presentations


Presentation on theme: "CS150 Introduction to Computer Science 1"— Presentation transcript:

1 CS150 Introduction to Computer Science 1
Review of Structs Used to store data of different data types. To initialize a struct you must: 1) Define the struct variable name and associated members before main and any prototypes (why?). 2) Declare variables to be of your struct variable name datatype. To use the struct you must: 3) Define and access members using the member access operator (a period between struct variable name and member name). Let’s do another example.-> 11/18/2018 CS150 Introduction to Computer Science 1

2 Review of Structs Example: Setup
Suppose we wanted to store the following information for each student in the class: Id number Social Security Number Average How would we define the struct? 11/18/2018 CS150 Introduction to Computer Science 1

3 Review of Structs Example: Struct Definition
1) Define a struct using the name studinfo: struct studinfo { int id; int ssnum; float avg; }; How do we declare variables of this new data type? 11/18/2018 CS150 Introduction to Computer Science 1

4 Review of Structs Example: Variable Declaration
2) Declare variables to be of our new struct datatype: studinfo stud1,stud2,stud3; How do we define and access members of each new variable? 11/18/2018 CS150 Introduction to Computer Science 1

5 Review of Structs Example: Member Access
3) Define members using the member access operator for each variable and each member: stud1.id=1; stud1.ssnum= ; stud1.avg=93.43; stud2.id=2; stud2.ssnum= ; stud2.avg=34.65; stud3.id=3; stud3.ssnum= ; stud3.avg=87.43; Do you see any problems with this method??? 11/18/2018 CS150 Introduction to Computer Science 1

6 CS150 Introduction to Computer Science 1
Solutions??? What would be a better way to deal with many variables of the same struct data type??? How have we dealt with this problem before with other data types??? 11/18/2018 CS150 Introduction to Computer Science 1

7 Arrays of Structs Declaration
Recall that to declare an array of int’s called numbers, we could use the following: int numbers[30]; To declare an array of studinfo’s (our new datatype) called students we do something very similar: studinfo students[30]; Data type Array name Array size 11/18/2018 CS150 Introduction to Computer Science 1

8 Array of Structs Member Access
To access and assign elements of our int array numbers, we would use the following: numbers[0]=1; numbers[1]=353; etc. To access and assign elements of our struct studinfo array students, we would use the following: students[0].id=1; students[0].ssnum= ; students[0].avg=93.43; 11/18/2018 CS150 Introduction to Computer Science 1

9 Array of Structs Program #1
Create a data file “stud.dat” that contains some lines (at most 30) of data in the following form (assume the trailer value is an id of 9999): id ss# avg 9999 Write a C++ program that reads in the information from this file into the struct studinfo array students and prints to the screen the contents of the file. 11/18/2018 CS150 Introduction to Computer Science 1


Download ppt "CS150 Introduction to Computer Science 1"

Similar presentations


Ads by Google