Engineering Problem Solving with C++, Etter

Slides:



Advertisements
Similar presentations
One Dimensional Arrays
Advertisements

Passing Arrays to Functions Programming. COMP102 Prog. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters l Arrays are.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Passing Arrays to Functions. COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
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.
Programming Functions: Passing Parameters by Reference.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Chapter 8 Arrays and Strings
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
Copyright © 2012 Pearson Education, Inc. Chapter 7 One Dimensional Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
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.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Starting Out with C++, 3 rd Edition 1 Sorting Arrays.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Alternate Version of STARTING OUT WITH C++ 4th Edition
ECE 264 Object-Oriented Software Development
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
C++ Programming Lecture 15 Arrays – Part II
Programming fundamentals 2 Chapter 1:Array
Basic Array Definition
Sorting Algorithms.
Pointers and Pointer-Based Strings
Multi-dimensional Array
CS 1430: Programming in C++.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
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.
C++ Programming Lecture 15 Arrays – Part II
Engineering Problem Solving with C++, Etter/Ingber
Array Data Structure Chapter 6
One Dimensional Arrays
Chapter 18-3 Recursion Dale/Weems.
Arrays Kingdom of Saudi Arabia
Data type List Definition:
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
4.9 Multiple-Subscripted Arrays
Pass by Reference.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Engineering Problem Solving with C++, Etter
Arrays of Two-Dimensions
7 Arrays.
Chapter 9: Data Structures: Arrays
Array Data Structure Chapter 6
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
Pointers & Functions.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
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:

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

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

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

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

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

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

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

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

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

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

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.

Sorting

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

Going on to Classes Read Chapter 9, pp. 390-406

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

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

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] 18 29 45 51 36 18 29 36 51 45 18 29 36 45 51 10/14/11 Engineering Problem Solving with C++, Second Edition, J. Ingber 17

Selection Sort Program selectionSort.cpp

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?