Arrays in Functions. Indexed Variables as Function Arguments An indexed variable can be used as a function argument just like any other variable Suppose.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
Arrays.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
 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.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter 9: Arrays and Strings
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
C++ for Engineers and Scientists Third Edition
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
21-2 Understand various methods of sorting. Use the qsort function to sort. Binary search Related Chapter: ABC 8.5.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Chapter 7 Arrays. Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Computer Engineering Rabie A. Ramadan Lecture 5.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 7 Arrays.
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.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Copyright Curt Hill Arrays in C/C++ What? Why? How?
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Arrays Version 1.1. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 12 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
1 Understanding Arrays and Pointers COSC1567 C++ Programming Lecture 2.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Computer Programming BCT 1113
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
7 Arrays.
Object Oriented Programming in java
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
7 Arrays.
Arrays Arrays A few types Structures of related data items
Chapter 7 Arrays. Chapter 7 Arrays Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3 Programming with Arrays 7.4 Multidimensional.
Presentation transcript:

Arrays in Functions

Indexed Variables as Function Arguments An indexed variable can be used as a function argument just like any other variable Suppose myFunction() accepts one argument of type int, and the following declarations exist: int i, n, a[10]; Some legal function calls are: – myFunction(n) – myFunction(a[3]) – myFunction(a[i]) See example: add 5 to each employee’s allowed number of vacation days with function: – int adjustDays(int oldDays);//returns old days + 5

Entire Arrays as Function Arguments Array Parameter – a new kind of formal parameter that is neither call by value or call by reference For most practical purposes, it behaves like call by reference An array address has three parts – – The address of the first indexed variable – The base type of the array – The size of the array When an array is used as an array argument to a function, only the first part is given to the function

(continued) Syntax: – dataType functionName(dataType name[], int size) – You may insert a number in the square brackets, but the compiler will ignore it When an array is used as an argument, any action that is performed on the array parameter is performed on the array argument – thus the values in the array can actually be changed You MUST pass the function the size of the array!

What if you want to make sure that array elements are NOT changed? The const parameter modifier Used as a precaution Compiler will check your code for you Should be an all or nothing proposition – if you use it for one array parameter of a particular type, then you should use it for every other array parameter that has that type and that is not changed by the function

Functions that Return an Array A function in C++ can’t return an array in the same way it returns a value of type int or double We will get to this topic when we discuss pointers

Case Study Problem: The Apex Plastic Spoon Company has commissioned us to write a program that will display a bar graph showing the productivity of each of their four manufacturing plants for any given week. Plants keep separate production figures for each department, such as the teaspoon department, soup spoon department, plain cocktail spoon department, colored cocktail spoon department, and so forth. Moreover, each plant has a different number of departments. For example, only one plant manufactures colored cocktail spoons The input is entered plant by plant and consists of a list of numbers giving the production for each department in that plant. The output will consist of a bar graph in the following form: Plant #1 ********** Plant #2 ************* Plant #3 ******************* Plant #4 ***** Each asterisk represents 1,000 units of output

Problem Continued We decide to read in the input separately for each department in a plant. Since departments can’t produce a negative number of spoons, we know that the production figure for each department will be non-negative. Hence, we can use a negative number sentinel value to mark the end of the production numbers for each plant. Since output is in units of 1,000, it must be scaled by dividing it by 1,000. We will round to the nearest 1,000

I/O Input: There are four manufacturing plants numbered 1 through 4. The following input is given for each of the four plants: a list of numbers giving the production for each department in that plant. The list is terminated with a negative number that serves as a sentinel value Output: A bar graph showing the total production for each plant. Each asterisk in the bar graph equals 1,000 units. The production of each plant is rounded to the nearest 1,000 units