CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
Structure.
Advertisements

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
CS150 Introduction to Computer Science 1
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Computer Programming II
Pointers What is the data type of pointer variables?
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 10-1: Structure.
Visit for more Learning Resources
CS150 Introduction to Computer Science 1
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Structures.
C++ Data Types Simple Structured Address Integral Floating
CS 1430: Programming in C++.
Tejalal Choudhary “C Programming from Scratch” Pointers
Pointers and References
Buy book Online -
Structures Lesson xx In this module, we’ll introduce you to structures.
S. Kiran, PGT (CS) KV, Malleswaram
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
One-Dimensional Array Introduction Lesson xx
Chapter 10: Records (structs)
Chapter 8 Arrays Objectives
Lecture 18 Arrays and Pointer Arithmetic
CS148 Introduction to Programming II
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS150 Introduction to Computer Science 1
Topics discussed in this section:
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Multidimensional array
CISC181 Introduction to Computer Science Dr
CS150 Introduction to Computer Science 1
Structure.
Review for Test1.
Arrays ICS2O.
CS148 Introduction to Programming II
CS150 Introduction to Computer Science 1
INC 161 , CPE 100 Computer Programming
Chapter 8 Arrays Objectives
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
C Programming Lecture-13 Structures
Structures In C Programming By Rajanikanth B.
Data Structures and Algorithms Introduction to Pointers
C Programming Pointers
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
Separating Interface from Implementation
CS148 Introduction to Programming II
EECE.2160 ECE Application Programming
CS148 Introduction to Programming II
Lec 21 More Fun with Arrays: For Loops
Programming Fundamental
Presentation transcript:

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

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

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

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

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=529338383; stud1.avg=93.43; stud2.id=2; stud2.ssnum=353333234; stud2.avg=34.65; stud3.id=3; stud3.ssnum=343433333; stud3.avg=87.43; Do you see any problems with this method??? 11/18/2018 CS150 Introduction to Computer Science 1

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

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

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=529338383; students[0].avg=93.43; 11/18/2018 CS150 Introduction to Computer Science 1

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