Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Fundamentals with C++

Similar presentations


Presentation on theme: "Computing Fundamentals with C++"— Presentation transcript:

1 Computing Fundamentals with C++
Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 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.

2 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

3 19.1 Matrix Data Data is sometimes conveniently viewed as tabular--rows and columns Week# Student# An Attendance Sheet Code: 1 represents attendance, 0 indicates absence.

4 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

5 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];

6 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;

7 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

8 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? ______ ?

9 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

10 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?______?

11 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

12 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; };

13 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

14 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

15 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

16 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

17 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

18 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

19 Compare C arrays to matrix

20 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 respectively


Download ppt "Computing Fundamentals with C++"

Similar presentations


Ads by Google