Arrays float Scores[9]; 55.559.047.039.050.560.059.040.0? index: 0 1 2 3 4 5 6 7 8 element // one dimensional array 1.

Slides:



Advertisements
Similar presentations
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
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.
 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.
Multiple-Subscripted Array
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
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 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
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:
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
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.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
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.
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.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
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
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
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.
CS 1430: Programming in C++.
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.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
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
Two-Dimensional Arrays
CS 1430: Programming in C++ No time to cover HiC.
Introduction to Programming
Programming Fundamental
CS 1430: Programming in C++.
Multi-dimensional Array
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++.
Engineering Problem Solving with C++, Etter/Ingber
One-Dimensional Array Introduction Lesson xx
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++.
Multidimensional Arrays
Multidimensional Arrays
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
Presentation transcript:

Arrays float Scores[9]; ? index: element // one dimensional array 1

Two Dimensional Arrays float allScores[5][9]; row index column index

Accessing Array Elements float allScores[5][9]; row index column index allScores[0][0] allScores[4][8] allScores[0][8] allScores[2][5] 3

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

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

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

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

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

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] 9

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

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( ) { } 11

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

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); } 13

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( ) { } 14

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

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

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! 17

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

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

Function Prototypes and Function Calls // Function prototypes 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); 20

Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 21 void GetAllData(float a[][MAX_COLS], int& numRows, int& numCols) { cin >> numRows >> numCols; for (...) cin >> a[i][j]; } Rows: cols: allScores[][]: ?????? 24 ?????? &numRows: &numCols: a[][]: Return Address 5 24 … 5 …

Schedule Lab 11 Grace: 5 pm today Prog 6 Due 9:30 pm Today Thursday No Lab We Have Class! In Lab

Programming Grand Rules You must include a comment block before every class declaration. // // The following class represents a day of the year. // class Date {... } 23

Programming Grand Rules Every function and method, except the main function, must have a comment which describes what it does and whether the parameters are input, output, or input and output. Be sure to document the purpose of any parameter which isn't obvious from its name alone. // // This function/method uses hours and rate to compute the gross pay // and the net pay of an employee and pass them back to the calling // function/method. // params: (in, in, out, out) // void ComputePay(int hours, float rate, float& grossPay, float& netPay) {... } 24

Programming Grand Rules Every program must start with a comment block of five sections: Name, Course (section and semester), Purpose, Input, and Output. // // Name: your name // // Course: CS 143, Section X, Spring 2011 // // Purpose: General description of what the program does. This should // be at least several lines long and should discuss the // purpose of the program. This information should be // from a user's perspective (what the program does) as // opposed to the programmer's perspective (how it works). // // Input: The input to the program and the format of the input. // // Output: The output the program produces. //