1 / 9

Arrays in Functions

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:

shel
Download Presentation

Arrays in Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays in Functions

  2. 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: inti, 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: • intadjustDays(intoldDays); //returns old days + 5

  3. 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

  4. (continued) • Syntax: • dataTypefunctionName(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!

  5. 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

  6. 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

  7. 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

  8. 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

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

More Related