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.

Slides:



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

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Kernighan/Ritchie: Kelley/Pohl:
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
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.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 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.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
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.
Arrays.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
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.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPS120: Introduction to Computer Science Lecture 14 Functions.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Review 1 List Data Structure List operations List Implementation Array Linked List.
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.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
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.
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.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
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.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Array An “average.cpp” program
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 >
New Structure Recall “average.cpp” program
Chapter 7: Arrays.
Functions with arrays.
CISC181 Introduction to Computer Science Dr
The Function Prototype
Presentation transcript:

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 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 elementtype arrrayname [ size ]; #define NumScores 10 int scores[NumScores]; scores

Array Selection 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 up until now, values of arguments cannot be changed outside function

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 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++; cout << ” ? ”; cin >> value; } 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 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; }