Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Problem Solving with C++, Etter

Similar presentations


Presentation on theme: "Engineering Problem Solving with C++, Etter"— Presentation transcript:

1 Engineering Problem Solving with C++, Etter
Chapter 6 Array Function Arguments 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 1

2 Engineering Problem Solving with C++, Second Edition, J. Ingber
Look at New Assignment P6.12, page 297 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber

3 Engineering Problem Solving with C++, Second Edition, J. Ingber
One Dimensional Arrays Array Function Arguments Sorting with Selection Sort 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 3

4 Engineering Problem Solving with C++, Second Edition, J. Ingber
Function Arguments ARRAYS 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 4

5 Engineering Problem Solving with C++, Second Edition, J. Ingber
Functions and arrays An array identifier, without subscripts, references the starting address(first element) of the array. In C++, arrays are passed by reference. ie the starting address is passed, no size information. Arrays in C++ to not know their size. Generally we specify an additional parameter representing the number of elements in the array. 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 5

6 Example of Array Parameter
insert.cpp A function inserts a number into the array. Puts it at the end of the list.

7 Engineering Problem Solving with C++, Second Edition, J. Ingber
Example 2, Find Minimum 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 7

8 // This function inputs values into an array until cin.fail() or array
#include <iostream> using namespace std; const int MAXSIZE=20; // This function inputs values into an array until cin.fail() or array // limit reached void ReadArr(double a[], int& count) { double temp; count = 0; cin >> temp; while ( (count < MAXSIZE) && !cin.fail() ) a[count] = temp; ++count; } 8

9 Engineering Problem Solving with C++, Second Edition, J. Ingber
//This function returns the index of the smallest //value in an array int FindMin(const double a[], int size) { int indexOfMin = 0; for (int i=1; i<size; ++i) { if (a[i] < a[indexOfMin] ) { indexOfMin = i; }//end if }//end for return indexOfMin; }//end FindMin 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 9

10 Engineering Problem Solving with C++, Second Edition, J. Ingber
int main( ) { double darr[MAXSIZE]; int cnt=0, position=0; ReadArr(darr, cnt); position = FindMin(darr, cnt); cout << "The smallest value in the array is " << darr[position] << endl; } 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 10

11 Your Turn cp ~/cs117/outputFunction.cpp
Add a print() function to the program that will be passed an array of ints and its size. It will output the array's elements.

12 Sorting

13 Constant Parameter Promises not to change the array parameter:
int FindMin(const double a[], int size)

14 Going on to Classes Read Chapter 9, pp

15 Engineering Problem Solving with C++, Second Edition, J. Ingber
Sorting Algorithms Sorting algorithms arrange the data into either ascending or descending order, based on the values in the array. Today we'll look at selection sort. 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 15

16 Engineering Problem Solving with C++, Second Edition, J. Ingber
Selection Sort Algorithm Find minimum value, place it in the first position. Find next minimum value, place it in the second position. Continue doing this until you have placed the second to the largest value in the second to the last position. p.263 of Horstmann 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 16

17 Engineering Problem Solving with C++, Second Edition, J. Ingber
Practice! Fill in the following table to show how the array is sorted into ascending order using the selection sort. arr[0] arr[1] arr[2] arr[3] arr[4] 29 45 18 51 36 swap min and arr[0] 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 17

18 Selection Sort Program
selectionSort.cpp

19 Questions Write a protype for a function that takes an array of ints and its size and returns the sum of the int elements in the array. The selection sort program sorts a list of 5 numbers. How would you modify the program to sort 10?


Download ppt "Engineering Problem Solving with C++, Etter"

Similar presentations


Ads by Google