Array An “average.cpp” program

Slides:



Advertisements
Similar presentations
Functions Prototypes, parameter passing, return values, activation frams.
Advertisements

PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
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.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 CS161 Introduction to Computer Science Topic #10.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization 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.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
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 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Standard Version of Starting Out with C++, 4th Edition
Arrays Low level collections.
Chapter 7: Arrays.
Chapter 9: Pointers.
Functions and an Introduction to Recursion
A Lecture for the c++ Course
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Arrays Part-1 Armen Keshishian.
New Structure Recall “average.cpp” program
Multi-dimensional Array
Chapter 7: Arrays.
C++ Arrays.
C Passing arrays to a Function
Chapter 9: Pointers.
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Chapter 8 Arrays Objectives
CS-161 Computer Programming Lecture 14: Arrays I
Counting Loops.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CS150 Introduction to Computer Science 1
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions with arrays.
Topics discussed in this section:
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Stack Frames and Functions
Mr. Dave Clausen La Cañada High School
CISC181 Introduction to Computer Science Dr
The Function Prototype
Functions and an Introduction to Recursion
CS150 Introduction to Computer Science 1
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Chapter 8 Arrays Objectives
CS149D Elements of Computer Science
CS150 Introduction to Computer Science 1
C++ winter2008(by:J.Razjouyan)
Arrays Arrays A few types Structures of related data items
CS150 Introduction to Computer Science 1
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CSCE 206 Lab Structured Programming in C
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

Array An “average.cpp” program Read in a list of numbers Count them and sum them up Calculate the average Now - what if we want to know how many of them are above average? Must be able to go through them all again Need to be able to store them in memory

Arrays Collection of individual data values Two characteristics An array is ordered - elements numbered. An array is homogeneous - all values same type Two properties must be defined Element type - type of value for elements Array size - number of elements in array

Array Declaration int scores[NumScores]; scores elementtype arrrayname [ size ]; #define NumScores 10 int scores[NumScores]; scores 0 1 2 3 4 5 6 7 8 9

Array Selection scores[i] = 0; } To refer to element of array, specify array name and position of element (subscript) scores[2] = 25; To set all elements of array for (i=0; i<NumScores; i++) { scores[i] = 0; } To initialize elements of array in declaration int primes[]={2,3,5,7,11,13,17,19};

Changing Numbers Program Add an array for the values (assume 100) #define MaxScores 100 int scores[MaxScores]; Store values in array scores[counter] = value; Check if value > average if (scores[i] > average) ... See aboveavg.cpp

Passing Arrays to Functions Write a program to: Read in list of integers until sentinel 0 is entered Reverse the elements in the list Display list in reverse order int main() { int list[MaxElements]; GetIntArray(list); ReverseIntArray(list); PrintIntArray(list); }

Problems with Method Two issues: Number of elements in array?? MaxElements or how many entered before sentinel GetIntArray and ReverseIntArray must change values of argument arrays in main

Generalize Number of Elements Only wish to reverse and print actual number of elements, array contains more elements in declaration (maximum number) int list[MaxElements]; Call actual number of elements effective size Print and reverse must then be told size PrintIntArray(list, n);

Function Prototypes void PrintIntArray(int array[], int n); Could write as void PrintIntArray(int array[MaxElements], int n); Better to write void PrintIntArray(int array[], int n); Then can pass array of any size Also void ReverseIntArray(int array[], int n);

GetIntArray Prototype GetIntArray has different structure Do not know the effective size before call GetIntArray determines effective size Needs to know actual size to limit it Also give it sentinel value for flexibility int GetIntArray(int array[], int max, int sentinel);

Mechanics of Array Parameters When variable used as parameter, only its value is passed When array name used as parameter, only base address of array sent to function Function can then operate on the array in place without copying its contents Storage for parameter array is shared with actual argument array

Implementing PrintIntegerArray void PrintIntArray(int array[], int n) { int i; for (i = 0; i < n; i++) cout << array[i] << endl; }

int GetIntArray(int array[], int max, int sentinel) { int n = 0, value; cout << ” ? ”; cin >> value; while (value != sentinel) { if (n >= max) cout << ”Too many items”; else array[n] = value; n++; } return n;

ReverseIntArray void ReverseIntArray(int array[], int n) { int i, limit; limit = n / 2; for (i = 0; i < limit; i++) { Swap values in array[i] and array[n-i-1] }

Swap Array Elements Swap(array[i], array[n-i-1]); Try Swap(array[i], array[n-i-1]); No, because passing values in array Must use Swap(array, i, n-i-1);

SwapIntElements void SwapIntElements(int array[], int p1, int p2) { int tmp = array[p1]; array[p1] = array[p2]; array[p2] = tmp; }