Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 1 void GetAllData(float a[][MAX_COLS],

Slides:



Advertisements
Similar presentations
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Functions 1. Example: Power, Square Root and Absolute values of a number #include … float num; float power, squareRoot, absolute; cout
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.
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;
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
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
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.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
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).
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Pointers CSE 5100 Data Structures and Algorithms.
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.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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];
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
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.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
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.
Lecture 14: Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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!
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
1 Program 2 Pseudo Code Read NumOfSongs of 1 st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD.
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.
Multidimensional Arrays As Parameter void fun ( int matrix [] [10] ) {…} void main ( ) { int mat[5][10]; … fun(mat); } void fun (float *mat_ptr, int num_rows,
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++.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
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
Introduction to Programming
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Programming Fundamental
CS 1430: Programming in C++.
Chapter 7: 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++.
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS150 Introduction to Computer Science 1
The Run-Time Stack and Reference Parameters
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Presentation transcript:

Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 1 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 …

Activation Record const float REG_HOURS = 40.0; const float OVER_TIME = 1.5; int main() { float hours, rate, gross; GetInput(rate, hours); gross = GrossPay(rate, hours); // display result return 0; } float GrossPay(float payRate, float hours) { if (hours > REG_HOURS) payRate = (hours - REG_HOURS) * OVER_TIME * payRate + REG_HOURS * payRate; else payRate = hours * payRate; return payRate; } main() GetInput() void GetInput(float& payRate, float& hoursWorked) { cout << "Enter pay rate: "; cin >> payRate; cout << "Enter hours: "; cin >> hoursWorked; return; } GrossPay() 2

Activation Record int main() { float hours, rate, gross; GetInput(rate, hours);... } 3 void GetInput(float& payRate, float& hoursWorked) { cin >> payRate; cin >> hoursWorked; return; } hours: rate: gross: ?????? 45 ?????? &payRate: &hoursWorked: Return Address

Activation Record int main() { float hours, rate, gross; GetInput(rate, hours); gross = GrossPay(rate, hours);... } 4 float GrossPay(float payRate, float hours) { if (hours > REG_HOURS) payRate = (hours - REG_HOURS) * OVER_TIME * payRate + REG_HOURS * payRate; else payRate = hours * payRate; return payRate; } hours: rate: gross: ? payRate: 10 hours: 45 Return Address 475

Activation Record int main() { int num1 = 5, num2 = 9, x = 10; Final(num1, num2, x + num2); return 0; } 5 // Parameters; (,, ) void Final(int& x, int& y, int z) { int temp; temp = z / x; z = 2 * temp - 5; x += z; y = z - x; return; } num1: 5 num2: 9 X : 10 &x : 5 (num1) &y : 9 (num2) z : 19 Temp: ? Return Address ? ? InInOut ? Out

Array and typedef float Scores[9]; // an array variable typedef float arrayType[9]; // a (new) data type // a nick name for array of // 9 float elements 6

2-D Array and typedef typedef float arrayType[9]; arrayType Scores; // Array of float with 9 elements arrayType allScores[10]; // Array of arrays // 2-D array // Same as float allScores[10][9]; 7

2-D Array as Function Parameter float rowAverage(const float nums[][MAX_COLS], int rowIndex, int numCols); // Must specify MAX_COLS! // nums[] is an array // Data Type of array elements: // an array of MAX_COLS floats 8

2-D Array as Function Parameter const int MAX_COLS = 9; typedef float arrayType[MAX_COLS]; float rowAverage(const arrayType nums[], int rowIndex, int numCols); // nums[] is an array // Data Type of array elements: arrayType // an array of MAX_COLS floats float rowAverage(const float nums[][MAX_COLS], int rowIndex, int numCols); // Must specify MAX_COLS! // Must specify data type of array elements! 9

2-D Array and Array of Array typedef float arrayType[9]; arrayType allScores[10]; // To understand float allScores[10][9]; // To code 10

2-D Array and Array of Array float rowAverage(const float nums [], int numCols); float arrayAverage(const float nums [][MAX_COLS], int numRows, int numCols); float nums[MAX_ROWS][MAX_COLS]; float Average; Average = rowAverage(nums[row], cols); // Passing one row as an array Average = arrayAverage(nums, rows, cols); // Passing the entire 2-D array 11

Schedule Quiz9-4: 5 points Due 5 pm, Monday Prog6 Due Wednesday Grace Friday Fix Prog5 errors C-String for C++ String Selection Sort 12

Final Exam 7:00 – 8:52 PM Thursday, May19, 2011 Doudna Points Comprehensive #2 pencil 13