CS 1430: Programming in C++.

Slides:



Advertisements
Similar presentations
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Advertisements

Passing Arrays to Functions Programming. COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters l Arrays are.
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Multiple-Subscripted Array
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
Arrays CS 308 – Data Structures. One-Dimensional Arrays A list of values with the same data type that are stored using a single group name (array name).
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
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.
WEEK 6 Class Activities Lecturer’s slides.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Arrays.
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
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.
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.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
Engineering Problem#1 Engr201 Computer Programming for Engineers Faculty of Engineering Chiang Mai University 1.
CS 1430: Programming in C++.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 1 void GetAllData(float a[][MAX_COLS],
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Introduction to Programming
C++ Review Data Structures.
Two-Dimensional Arrays
I/O Streams File I/O 2-D array review
Two-Dimensional Arrays
CS 1430: Programming in C++ No time to cover HiC.
Introduction to Programming
Two-Dimensional Arrays Lesson xx
Programming Fundamental
Multi-dimensional Array
Chapter 7: Arrays.
C++ Arrays.
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Engineering Problem Solving with C++, Etter/Ingber
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++.
Introduction to Programming
CS 1430: Programming in C++ No time to cover HiC.
CS-161 Computer Programming Lecture 14: Arrays I
Counting Loops.
Engr 0012 (04-1) LecNotes
The Run-Time Stack and Reference Parameters
Multidimensional Arrays
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Multidimensional Arrays
INC 161 , CPE 100 Computer Programming
CS150 Introduction to Computer Science 1
Class StudentList class StudentList { private: int numStudents;
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
CS 1430: Programming in C++.
CS 1430: Programming in C++ No time to cover HiC.
Announcements Exam 2 Next Week!! Final Exam :
Presentation transcript:

CS 1430: Programming in C++

Arrays float Scores[9]; // one dimensional array index: 0 1 2 3 4 5 6 7 8 55.5 59.0 47.0 39.0 50.5 60.0 40.0 ? element

Two Dimensional Arrays float allScores[5][9]; column index 0 1 2 3 4 5 6 7 8 row index 1 2 3 4

Accessing Array Elements float allScores[5][9]; allScores[0][0] allScores[0][8] column index 0 1 2 3 4 5 6 7 8 row index 3.5 3.9 3.2 3.0 1 2 3 4 allScores[2][5] allScores[4][8]

Accessing Array Elements float allScores[5][9]; // Each array element is // the same as a variable cin >> allScores[0][0]; allScores[0][0] += 5.0; cout << allScores[0][0];

For Loop and 2-D Arrays float allScores[5][9]; // Input values to 1st row // Row index of 1st row: 0 // Column index: 0 to 8 (size – 1) for (int col = 0; col < 9; col ++) cin >> allScores[0][col]; // inFile >> allScores[0][col];

For Loop and 2-D Arrays const int MAX_ROWS = 10; const int MAX_COLS = 35; float allScores[MAX_ROWS][MAX_COLS]; // Input values to the last column // Column index of last column: 34 (MAX_COLS – 1) // Row index: from 0 to 9 (MAX_ROWS – 1) for (int row = 0; row < MAX_ROWS; row ++) cin >> allScores[row][MAX_COLS - 1]; // inFile >> allScores[row][MAX_COLS - 1];

For Loop and 2-D Arrays const int MAX_ROWS = 10; const int MAX_COLS = 35; float allScores[MAX_ROWS][MAX_COLS]; // Input values to the entire 2-D array for (int row = 0; row < MAX_ROWS; row ++) for (int col = 0; col < MAX_COLS; col ++) cin >> allScores[row][col]; // inFile >> allScores[row][col];

For Loop and 2-D Arrays const int MAX_ROWS = 10; const int MAX_COLS = 35; float allScores[MAX_ROWS][MAX_COLS]; int numRows, numCols; cout << “Enter No. of rows and No. of cols: ”; cin >> numRows >> numCols; // numRows <= MAX_ROWS // numCols <= MAX_COLS for (int row = 0; row < numRows; row ++) for (int col = 0; col < numCols; col ++) cin >> allScores[row][col];

Two Dimensional Arrays as Function Parameters //---------------------------------------------- // The function inputs values to all array // elements within the first numRows rows and // the first numCols columns. // Parameters: (Out, In, In) void GetData(float a[][MAX_COLS], int numRows, int numCols) { for (int row = 0; row < numRows; row ++) for (int col = 0; col < numCols; col ++) cin >> a[row][col]; return; } // Have to specify MAX_COLS! // float a[][MAX_COLS]

Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function inputs values for numRows and numCols, // then inputs values to all array elements within // the first numRows rows and the first numCols // columns. // Parameters: (Out, Out, Out) void GetAllData(float a[][MAX_COLS], int& numRows,int& numCols) { cin >> numRows >> numCols; for (int row = 0; row < numRows; row ++) for (int col = 0; col < numCols; col ++) cin >> a[row][col]; return; }

Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // colIndex, numRows: int // The function computes and returns the average // value of the column at index colIndex. // Params: ( ? , ? , ? ) float columnAvg( ) { }

Two Dimensional Arrays float columnAvg( ); colIndex numRows - 1

Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // colIndex, numRows: int // The function computes and returns the average // value of the column at index colIndex. // Params: ( In , In , In ) float columnAvg(const float a[][MAX_COLS], int colIndex, int numRows) { float total = 0; for (int row = 0; row < numRows; row ++) total += a[row][colIndex]; return (total / numRows); }

Two Dimensional Arrays as Function Parameters //------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // numCols, rowIndex: int // The function finds and returns the column index // of row at index rowIndex that has the max // value among all elements of row rowIndex. // Params: ( ? , ? , ? ) int IndexOfRowMax( ) { }

Two Dimensional Arrays int IndexOfRowMax(); numCols - 1 rowIndex

Two Dimensional Arrays as Function Parameters //---------------------------------------------------- // The function has 3 parameters: // a[][MAX_COLS]: 2-D array of float // numCols, rowIndex: int // The function finds and returns the column index // of row at index rowIndex that has the max // value among all elements of the row. // Params: ( In , In , In ) int IndexOfRowMax(const float a[][MAX_COLS], int rowIndex, int numCols) { int index = 0; for (int col = 1; col < numCols; col ++) if (a[rowIndex][col] > a[rowIndex][index]) index = col; return index; }

Function Prototypes void GetData(float a[][MAX_COLS], int numRows, int numCols); void GetAllData(float a[][COL_SIZE], int& numRows, int& numCols); float columnAvg(const float a[][MAX_COLS], int colIndex, int numRows); int IndexOfRowMax(const float a[][MAX_COLS], int rowIndex, int numCols); // Must specify column MAX_SIZE!

Two Dimensional Arrays as Function Parameters const int MAX_ROWS = 10; const int MAX_COLS = 35; int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; // Input sizes in main() function. cin >> rows >> cols; // Call function GetData(). GetData(allScores, rows, cols); // No [] for allScores ... return; }

Call Functions int main() { float allScores[MAX_COLS][MAX_ROWS]; int rows, cols, row, col, index; float average; GetAllData(allScores, rows, cols); cout << "Enter column index: "; cin >> col; average = columnAvg(allScores, col, rows); cout << "The average value of column " << col << " is " << average; cout << "Enter row index: "; cin >> row; index = IndexOfRowMax(allScores, row, cols); cout << "The largest value of row " << row << " is " << allScores[row][index]; // << allScores[row][IndexOfColMax(allScores, row, cols)]; return 0; }

Function Prototypes and Function Calls void GetAllData(float a[][COL_SIZE], int& numRows, int& numCols); float columnAvg(const float a[][MAX_COLS], int colIndex, int numRows); int IndexOfRowMax(const float a[][MAX_COLS], int rowIndex, int numCols); // Function calls GetAllData(allScores, rows, cols); average = columnAvg(allScores, col, rows); index = IndexOfRowMax(allScores, row, cols);

Schedule Quiz9-3 (1 bonus point) Name your cpp file as UserName-Quiz9-3.cpp (YangQ-Quiz9-3.cpp) Drop it to K:\Courses\CSSE\yangq\CS1430-Dropbox\Quiz9-3 Lab 11 5 pm Thursday Prog 6 Due Tuesday, Dec 8

Test 3 Wednesday, Dec 9 Quiz3 Now Schedule Test 3 Wednesday, Dec 9 Quiz3 Now