A solution to a list of records

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
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:
Arrays.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
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.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Programmer Defined Structures (Records)
Organizing Heterogeneous Data Arrays allow a programmer to organize lists of values that are all of the same type (homogeneous). But we are often faced.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
ARRAYS.
Passing Objects to Methods
11 Chapter Structured Data
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Chapter 7 Part 1 Edited by JJ Shepherd
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Programming fundamentals 2 Chapter 1:Array
Multi-dimensional Array
Chapter 7: Arrays.
C++ Arrays.
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:
Structures.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
While Loops.
Visual Basic .NET BASICS
Arrays Part II Array Function Arguments
CS 1430: Programming in C++.
One-Dimensional Array Introduction Lesson xx
Previous Lecture Review
Flow of Control October 16, 2017.
Data type List Definition:
Chapter 11: Structured Data.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
CS 1430: Programming in C++.
Input Validation CSCE 121 J. Michael Moore
CS 1430: Programming in C++.
Looping III (do … while statement)
Review for Final Exam.
Arrays Lecture 11.
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Boolean Variables & Values
C++ winter2008(by:J.Razjouyan)
CS250 Introduction to Computer Science II
(Dreaded) Quiz 2 Next Monday.
Standard Version of Starting Out with C++, 4th Edition
Class StudentList class StudentList { private: int numStudents;
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Input Validation CSCE 121 Based on slides created by Carlos Soto.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Presentation transcript:

A solution to a list of records Parallel Arrays A solution to a list of records

Record Definition A collection of two or more related data elements that may be of different types. Each of the data elements is called a Field. Example: a student record consists of fields: - a name (string) - a student id (int) - a gpa (double)

Record List We may also need to have a list of records. But Arrays can only be a list of a single data type. How can we make a "List of Records"?

Parallel Arrays Definition One solution to this problem is Parallel Arrays a set of two or more arrays of equal length where the i'th element of each array is related

Parallel Arrays Example Say we have this list of students on paper: Name ID gpa Alice 12345 2.7 Bob 67890 3.1 Cathy 54321 3.7 David 98765 3.5

Parallel Arrays Declaration Declare an array for each field, all with the same length. const int MAX = 4; string name[MAX]; int id[MAX]; double gpa[MAX];

Parallel Arrays Population with Literals So the i'th element of each array is related to the same student: name[0]="Alice"; id[0]=12345; gpa[0]=2.7; name[1]="Bob"; id[1]=67890; gpa[1]=3.1; name[2]="Cathy"; id[2]=54321; gpa[2]=3.7; name[3]="David"; id[3]=98765; gpa[3]=3.5;

Parallel Arrays Print Now we can print a report of students by looping through the arrays, using the same index for each student: for (int i=0; i<MAX; i++) { cout << name[i] << " " << id[i] << " " << gpa[i] << endl; }

Parallel Arrays Population with user input Or, we can ask the user to populate the arrays for (int i=0; i<MAX; i++) { cout << "Enter name, id and gpa: "; cin >> name[i] >> id[i] >> gpa[i]; }

Parallel Arrays MAX of one field We can find the student with the highest GPA: int ndxMaxGPA = 0; // assume 1st student has max for (int i=1; i<MAX; i++) { if (gpa[i] > gpa[ndxMaxGPA]) ndxMaxGPA = i; // save INDEX of max gpa } cout << "Stu with max GPA: " << name[ndxMaxGPA];

Parallel Arrays SEARCH by one field We can search by ID: int srchID, found = -1; // -1 = not found yet cout << "Enter ID to search for: "; cin >> srchID; for (int i=0; i<MAX; i++) { if (id[i] == srchID) found = i; // found in index i }

Parallel Arrays SEARCH by one field // continued from previous slide if (found == -1) cout << srchID << " not found!"; else cout << "Name = " << name[found] << " GPA = " << gpa[found];

Partial Parallel Arrays Declaration We can apply the idea of Partial Arrays to Parallel: const int MAX = 4; string name[MAX]; int id[MAX]; double gpa[MAX]; int numStudents = 0; // numStudents applies to all 3 arrays

Partial Parallel Arrays Population by user input We can ask the user to populate a Partial Parallel Array: cout << "How many students? "; cin >> numStudents; // should validate 0..MAX for (int i=0; i<numStudents; i++) { cout << "Enter name, id, gpa: "; cin >> name[i] >> id[i] >> gpa[i]; }

In the Future There are better ways to implement a List of Records But Parallel Arrays are a common data structure.

Vocabulary Term Definition Record A collection of related data elements, often of different types. Field One of the data elements of a record. Parallel Arrays set of two or more arrays of the same length, where the i'th element of each is related.