Arrays & Structures II Chapter 9. 2 Organizing Heterogeneous Data  Arrays allow a programmer to organize values of the same type  Homogeneous data 

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Structure.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 9: Arrays and Strings
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
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.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
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.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
Module 1: Array ITEI222 - Advance Programming Language.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Multidimensional Arrays Vectors of Vectors When One Is Not Enough.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
10. Structures Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Organizing Heterogeneous Data.
Test 2 Review Outline.
Computer Programming BCT 1113
CS 1430: Programming in C++.
Array Data Structure Chapter 6
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Review for Final Exam.
Multidimensional Arrays
Multidimensional Arrays
Review for Final Exam.
7 Arrays.
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Presentation transcript:

Arrays & Structures II Chapter 9

2 Organizing Heterogeneous Data  Arrays allow a programmer to organize values of the same type  Homogeneous data  What about logically related data values of differing types?  Heterogeneous data  Parallel arrays  Array of structs Origin DestinationMilesTime Blacksburg, VA Knoxville, TN2443:25 Knoxville, TN Nashville, TN1782:35 Nashville, TN Memphis, TN2103:17 Memphis, TN Little Rock, AR1372:05 Little Rock, AR Texarkana, TX1412:10

3 Organizing Heterogeneous Data  A better design would be to have a new data type, Trip, such that a variable of type Trip would contain all the related values for a single trip.  A struct allows C++ programmers to do just that…  Like enum, struct is used to define a new data type  The statement below does not declare a variable, it defines a type struct Trip { string Origin; // starting point of trip string Destination; // ending point of trip int Miles; // distance traveled int Minutes; // time required, in min double MPH; // average speed };

4 Structures  Structure: a heterogeneous collection of data values called members or fields  each member has a type and a unique name  individual members are accessed by name  Definitions of data types are usually global, since they are required throughout a program.  As long as a type definition is in scope we may declare variables of that type in the usual way: Trip firstLeg; const int MAXLEGS = 100; Trip Itinerary[MAXLEGS]; // array of Trips

5 struct Variables  A variable of a struct type will contain the values for all of the specified members for that type  Those members must be individually initialized  To reference a particular member of a struct variable, state the variable name followed by a period followed by the member name  The period is the member selector operator Trip firstLeg; firstLeg.Origin = "Blacksburg, VA"; firstLeg.Destination = "Knoxville, TN"; firstLeg.Miles = 244; firstLeg.Minutes = 205; firstLeg.MPH = 71.2;

6 struct Variables  Accessing struct array elements requires use of both [] and. Operators  First specify the array index  Then the struct member const int MAXLEGS = 100; Trip Itinerary[MAXLEGS]; // array of Trips Itinerary[0].Origin = "Milwaukee, WI"; Itinerary[0].Destination = "Chicago, IL"; Itinerary[0].Miles = 88; Itinerary[0].Minutes = 88; Itinerary[0].MPH = 60.0;

7 Member Access Example struct Rectangle { int color; int xNW, yNW; int Side[2]; };... Rectangle R; cin >> R.xNW >> R.yNW; cin >> R.Side[0] >> R.Side[1]; R.color = 0; int Area = R.Side[0] * R.Side[1]; The members of a struct variable may be used just as if they were simple variables of the specified type

8 Aggregate Operations  An aggregate operation is an operation that is performed on a data structure, such as an structured variable, as a whole rather than performed on an individual member  Which of the following are supported for structs? Trip X, Y; X = Y; // _____ assigning one struct to another X == Y; // _____ comparing two structs cout << X; // _____ inserting an struct to a stream X = X + Y; // _____ performing arithmetic with structs return X; // _____ using a struct as the return value Foo(X); // _____ passing an entire struct

9 struct Variables as Parameters  Entire struct variables can be passed to functions as parameters.  By default is struct is passed by value  Since struct variables tend to be large, it is generally better to pass them by constant reference void printTrip(Trip& aTrip) { cout << aTrip.Origin << endl << aTrip.Destination << endl << aTrip.Miles << endl << (aTrip.Minutes / MINSPERHOUR) << ':' << (aTrip.Minutes % MINSPERHOUR) << endl << aTrip.MPH << endl; }

10 Problem  In a certain class (<=50 students) 3 programming assignments (worth 40%) and 3 tests (worth 60%) are given  All the programming and test scores must be in the range and should be checked for errors  An erroneous score means that student's data is invalid  Compute the average test and programming scores, and overall grades (A through F, based on 90, 80, 70, 60 scale) for each student, as well as class averages  Assume that the data for each student consists of a SSN followed by 6 integers representing the programming and test scores.

11 Processing Student Grades const int MaxStudents=50; struct StudentType { string ssn; int P1, P2, P3, T1, T2, T3; float Pavg, Tavg, Tot; char grade; bool valid; }; StudentType students[MaxStudents]; SSNP1P2P3PavgT1T2T3TavgTotGradeValid

Multidimensional Arrays

13 Multidimensional Arrays  Multidimensional arrays  Have more than one index  Rows & Columns for 2-D arrays  Syntax for array declaration: [ ][ ] …[ ]; int twoDimArray[4][2]; ? ? ? ? ? ? ? ?

14 Initialization  Multidimensional arrays can be initialized like 1-D arrays  List elements in row major order int twoDimArray[4][2] = {3, 25, 9, 16, -4, 4, 44, 2};

15 for Loops  When accessing or performing operations on multidimensional arrays, it is often necessary to use nested for loops. Consider the problem of adding arrays x and y, and placing the result in z. const int NRow = 4; const int NColumn = 2; int x[NRow][NColumn] ={ 3, 25, 9, 16, -4, 4, 44, 2}; int y[NRow][NColumn] ={ 8, 5, 12, -3, 43, 8, -7, 4}; int z[NRow][NColumn];

16 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? ? ? iRow iColumn xyz

17 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? ? iRow iColumn xyz

18 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? iRow iColumn xyz

19 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? ? ? ? ? iRow iColumn xyz

20 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? 30 ? ? ? iRow iColumn xyz

21 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? 30 ? ? ? iRow iColumn xyz

22 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? ? 30 ? ? ? iRow iColumn xyz

23 for Loops int iRow; int iColumn; for (iRow = 0; iRow < NRow; iRow++) { for (iColumn = 0; iColumn < NColumn; iColumn++) { z[iRow][iColumn] = x[iRow][iColumn] + y[iRow][iColumn]; } ? ? 30 ? ? ? iRow iColumn xyz

24 Multidimensional Arrays & Functions  Recall, a function using a 1-D array uses pass-by reference as follows: a_function(int x[])  With multidimensional arrays, the array is still passed using pass-by reference, but there is less flexibility.  With the 1-D array we do not have to explicitly state the size of the array  With multidimensional arrays, we do not have to explicitly state the size of the first dimensional, but must do so for remaining dimensions a_function(int x[][N])