Computing Fundamentals with C++

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
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.
Multiple-Subscripted Array
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
13-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
3-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
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.
Arrays Multi-dimensional initialize & display Sorting Part II.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
10-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
 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.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Arrays.
Computer Programming for Engineers
16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Computing Fundamentals with C++
EGR 2261 Unit 10 Two-dimensional Arrays
EGR 2261 Unit 9 One-dimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Two Dimensional Array Mr. Jacobs.
Two-Dimensional Arrays Lesson xx
© 2016 Pearson Education, Ltd. All rights reserved.
C programming---Arrays
Multi-dimensional Array
CS 1430: Programming in C++.
Two Dimensional Arrays
Chapter 8 Repetition Statements
Engineering Problem Solving with C++, Etter/Ingber
Array Data Structure Chapter 6
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Chapter 13 Vector of Vectors (2D Arrays)
Chapter 10 Vectors Computing Fundamentals with C++ 3rd Edition
Arrays Kingdom of Saudi Arabia
4.9 Multiple-Subscripted Arrays
Chapter 12 Pointers and Memory Management
Chapter 4 Implementing Free Functions
Chapter 11 Generic Collections
Chapter 9 File Streams Computing Fundamentals with C++ 3rd Edition
Chapter 7 The Java Array Object © Rick Mercer.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Standard Version of Starting Out with C++, 4th Edition
Review of Everything Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
7 Arrays.
Array Data Structure Chapter 6
Arrays Arrays A few types Structures of related data items
Vectors.
Dr. Khizar Hayat Associate Prof. of Computer Science
4.9 Multiple-Subscripted Arrays
Presentation transcript:

Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8 Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Computing Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer are welcome to use this presentation as long as this copyright notice remains intact.

Chapter 19 Doubly Subscripted Objects Chapter Objectives process tabular data perform row-by-row and column-by-column processing of tabular data declare primitive arrays with two or three subscripts manipulate the data of a visibility research study using a doubly subscripted (one for rows, one for columns) object

19.1 Matrix Data Data is sometimes conveniently viewed as tabular--rows and columns Week# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Student# 0 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 5 1 0 1 0 1 1 1 0 1 1 1 1 1 0 0 6 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 7 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 An Attendance Sheet Code: 1 represents attendance, 0 indicates absence.

19.1 A matrix class a matrix object stores a collection of objects (of the same class) a matrix object is a fixed size (but it can be 'resized') Individual objects are referenced with two subscripts, one for the row and one for the column. Always start at 0 matrix is NOT a standard C++ class It was written by the author

General form of a matrix object declaration Must include matrix: #include "matrix" // for class matrix General form: matrix < class > identifier ( rows , columns ) ; Example: // a matrix object with 10 rows and 15 columns matrix<int> student(10, 15); If you do not want to use matrix, declare like this int student[10][15];

Referencing individual matrix elements A reference to an individual element of matrix requires two subscripts (row and column): matrix-name [ row ] [ column ] Each subscript must be bracketed individually. Examples student[0][0] = 1; student[9][14] = 0;

Errors may occur when referencing individual matrix elements These expressions generate errors: student[1] // require 2 subscripts student[-1][0] // There is no row -1 student[1][15] // There is no column 15 student[2, 3] // An error made by Pascalites If your subscript is out of range, a runtime error occurs. The program is terminated

Nested Looping (no matrix object) Nested for loops are often used to process the data stored in a matrix Consider a loop inside another int outer, inner; for(outer = 0; outer < 4; outer++) { for(inner = 0; inner <= outer; inner++) cout << inner << " "; } cout << endl; Output? ______ ?

Nested for loops to visit every element in a matrix object #include "matrix" // for the matrix<Type> class int main() { matrix <int> t(4, 6); // 4 x 6 ints int row, col; for(row = 0; row < 4; row++) { // Initialize one row for(col = 0; col < 6; col++) { // Reference each column in the row t[row][col] = row * col; } // continued on next slide

Display elements row by row for(row = 0; row < 4; row++) { // Display one row for(col = 0; col < 6; col++) { // Display each column of the row cout << t[row][col] << " "; } cout << endl; return 0; Output?______?

Active Learning Write the code that displays the sum of each row of data. Answer in: sumrows.cpp Your output should look like this: Sum of row #0 = 0 Sum of row #1 = 10 Sum of row #2 = 30 Sum of row #4 = 45

class quizData (with a matrix data member) A quizData object to demonstrate matrix processing (the class definition) class quizData { public: quizData(string filename); // constructor void display() const; // accessor void studentStats() const; // accessor void quizStats() const; // accessor double average() const; // accessor private: int lastStudent, lastQuiz; matrix <double> quiz; };

quizData::quizData quizData::quizData(string fileName) { // Initialize quiz (assuming filename exists) ifstream inFile(filename.c_str() ); int row, col; // Input the size of the data inFile >> lastStudent >> lastQuiz; // Initialize a lastStudent by lastQuiz matrix for(row = 0; row < lastStudent; row++) { for(col = 0; col < lastQuiz; col++) inFile >> quiz[row][col]; } } // end constructor implementation

quizData::display void quizData::display() { int row = 0; int col = 0; cout << "\nQuiz data:" << endl; for(row = 0; row < lastStudent; row++) for(col = 0; col < lastQuiz; col++) cout.width(6); cout << quiz[row][col]; } cout << endl; } // end quizData::display() implementation

quizData::studentStats void quizData::studentStats() { // pre: lastQuiz > 1 // An example of row by row processing double sum, lowest, average; int row, col; cout << endl; cout << " Student Average" << endl; cout << " ======= =======" << endl; cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout.precision(1); // continued on next slide

row by row processing for(row = 0; row < lastStudent; row++) //outer loop { // Assume the first quiz is the lowest lowest = quiz[row][0]; // Assign sum the value of the first quiz sum = lowest; // Process the remaining quizzes (start at 1) for(col = 1; col < lastQuiz; col++) // inner loop sum += quiz[row][col]; if(quiz[row][col] < lowest) lowest = quiz[row][col]; } // end inner loop

Average all but lowest quiz // Drop the lowest quiz sum = sum - lowest; // Divide by 1 less because of dropped quiz average = sum / (lastQuiz - 1); cout.width(10); cout << row; cout.width(15); cout << average << endl; } // end outer loop } // end quizData::studentStats

Primitive C arrays C++ has primitive arrays with one, two, three, four,... subscripts Examples: string myFriends[100]; // store up to 100 strings double x[10][5]; // store up to 150 numbers This gives you row subscripts 0..9 and column subscripts 0..4 You do not get subscript range checking

Compare C arrays to matrix

More than 2 subscripts C++ does not limit the number of subscripts that you can use Arrays with three or more subscripts are sometimes used The declaration follows the pattern of the doubly subscripted matrix double q[4][12][7] This would give 4 x 12 x 7 cube with subscripts 0..3, 0..1, and 0..6 respectively