Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Computer Science 1620 Loops.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 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.
Computer Science 1620 Programming & Problem Solving.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
C++ for Engineers and Scientists Third Edition
CSE202: Lecture 14The Ohio State University1 Arrays.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
Basic Elements of C++ Chapter 2.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2006 Pearson Education, Inc. All rights reserved Arrays.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
February 11, 2005 More Pointers Dynamic Memory Allocation.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
C++ Classes and Data Structures Jeffrey S. Childs
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
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.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Arrays.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
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.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Computing Fundamentals
7 Arrays.
Arrays Arrays A few types Structures of related data items
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.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Computer Science 1620 Arrays

Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the grades 2. calculate the average A 3. add (70 – A) to each grade 4. re-output the new grades to one decimal place

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 1. Read in the grades 2. calculate the average A 3. add (70 – A) to each grade

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 1. Read in the grades 2. calculate the average A 3. add (70 – A) to each grade

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 2. calculate the average A 3. add (70 – A) to each grade

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 2. calculate the average A 3. add (70 – A) to each grade

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 3. add (70 – A) to each grade

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades 3. add (70 – A) to each grade

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } 4. re-output the new grades

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }

Does the previous code work? Yes! Problems: 1) Repeated code! adjusting each mark, and re-outputting each mark is basically the same for each mark

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; }

Does the previous code work? Yes! Problems: 1) Repeated code! 2) Scalability what happens if the number of students increases to X, where X is large?

#include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; double grade6, grade7, grade8, grade9, grade10; double grade11, grade12, grade13, grade14, grade15; double grade16, grade17, grade18, grade19, grade20; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5 >> grade6 >> grade7 >> grade8 >> grade9 >> grade10 >> grade11 >> grade12 >> grade13 >> grade14 >> grade15 >> grade16 >> grade17 >> grade18 >> grade19 >> grade20; double A = (grade1 + grade2 + grade3 + grade4 + grade5 +grade6 + grade7 + grade8 + grade9 + grade10 +grade11 + grade12 + grade13 + grade14 + grade15 +grade16 + grade17 + grade18 + grade19 + grade20)/20; grade1 += (70 – A); grade2 += (70 – A); grade3 += (70 – A); grade4 += (70 – A); grade5 += (70 – A); grade6 += (70 – A); grade7 += (70 – A); grade8 += (70 – A); grade9 += (70 – A); grade10 += (70 – A); grade11 += (70 – A); grade12 += (70 – A); grade13 += (70 – A); grade14 += (70 – A); grade15 += (70 – A); grade16 += (70 – A); grade17 += (70 - A); grade18 += (70 - A); grade19 += (70 - A); grade20 += (70 - A);...

Arrays an aggregate data structure vs. atomic data types (int, float, double, char, …) a group of variables of the same type

Declare 10 variables of type int: int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10; int x[10]; Without Arrays: With Arrays: Array syntax: type name[size]; type can be any of the int or floating point types* name can be any valid variable name size can be any positive integral value, but MUST BE CONSTANT*

How to access the elements in an array use the subscript operator [ ] indices START AT 0, NOT 1 value in the subscripts need not be a constant int x[3]; x[0] = 5; // sets the first element in the array to value 5 x[1] = 6; // sets the second element in the array to value 6 int i = 2; x[i] = 7; // sets the third element in the array to value 7

The elements of an array are used exactly like a single variable can assign a value to them: x[i] = 5; can use them in an expression: int sum = x[0] + x[1] + x[2]; cin >> x[0]; cout << "Second value: " << x[1];

Arrays and Loops the indexing feature of arrays makes it very easy to use them in a loop very handy if you want to perform some operation on each element in the array General template (using a for loop) given an array x of size N begin integer index (call it i) at 0 loop up to (but not including) N increment one at a time perform operation on x[i] inside loop int x[10]; for (int i = 0; i < 10; i++) { // do some operation on x[i] }

Example 1: given an array x of 10 ints, set each element in the array to equal 5 Example 2: given an array x of 10 ints, output the value of each element to standard output int x[10]; for (int i = 0; i < 10; i++) { x[i] = 5; } int x[10]; for (int i = 0; i < 10; i++) { cout << x[i] << endl; }

Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Solution: read in the grades calculate the average A add (70 – A) to each grade re-output the new grades

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Reprogram this example using arrays!

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade1, grade2, grade3, grade4, grade5; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Convert these to an array

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Convert these to an array

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; cin >> grade1 >> grade2 >> grade3 >> grade4 >> grade5; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } cin a value to each element in the array 1. begin integer index (call it i) at 0 2. loop up to (but not including) N 3. increment one at a time 4. perform operation on x[i] inside loop

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = (grade1 + grade2 + grade3 + grade4 + grade5) / 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Add the value of each element in the array to A.

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Add the value of each element in the array to A.

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Divide the total by 5 to obtain the average.

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; grade1 += (70 - A); grade2 += (70 - A); grade3 += (70 - A); grade4 += (70 - A); grade5 += (70 - A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Subtract 70-A from each grade.

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Subtract 70-A from each grade.

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; cout << grade1 << "%" << endl; cout << grade2 << "%" << endl; cout << grade3 << "%" << endl; cout << grade4 << "%" << endl; cout << grade5 << "%" << endl; return 0; } Re-output each grade.

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) cout << grade[i] << "%" << endl; return 0; } Re-output each grade.

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; for (int i = 0; i < 5; i++) grade[i] += (70 – A); cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) cout << grade[i] << "%" << endl; return 0; } We can save some space by combining these two loops!

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } We can save some space by combining these two loops!

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[5]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 5; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 5; i++) A += grade[i]; A /= 5.0; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 5; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } Each of these statements is only used once in the code!

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { double grade[10]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < 10; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < 10; i++) A += grade[i]; A /= 10.0; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < 10; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } Adjusting the code to accommodate 10 students requires changing the array size!

Good programming practice: try to avoid magic numbers in your code numbers whose significance may not be obvious use a const parameter instead this applies to the size of the arrays this is exemplified by the last slide use a const integral value for your array sizes

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { const int NUM_STUDENTS = 10; double grade[NUM_STUDENTS]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < NUM_STUDENTS; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < NUM_STUDENTS; i++) A += grade[i]; A /= NUM_STUDENTS; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < NUM_STUDENTS; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; }

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { const int NUM_STUDENTS = 1000; double grade[NUM_STUDENTS]; cout << "Please enter the grades of the students" << endl; for (int i = 0; i < NUM_STUDENTS; i++) cin >> grade[i]; double A = 0; for (int i = 0; i < NUM_STUDENTS; i++) A += grade[i]; A /= NUM_STUDENTS; cout << fixed << setprecision(1); cout << "New grades:" << endl; for (int i = 0; i < NUM_STUDENTS; i++) { grade[i] += (70 – A); cout << grade[i] << "%" << endl; } return 0; } Adjusting the code to accommodate students requires changing one variable value minimal effect on code length

Arrays: Common Programming Errors off by 1 (run-time error) using indices beginning at 1 int x[10]; for (int i = 1; i <= 10; i++) { cout << x[i]; }

Arrays: Common Programming Errors using a non const value for size (compiler error) int size = 6; int x[size]; for (int i = 0; i < size; i++) { cout << x[i]; }

Arrays: Common Programming Errors going out of bounds (runtime error) C++ does not do bounds checking int x[10]; for (int i = 0; i <= 10; i++) { cout << x[i]; }

Array Initialization when declaring an atomic variable, we had the option to initialize to a value: int x = 10; we can do the same with arrays, using braces int x[5] = {1, 2, 3, 4, 5}; x[0]x[1]x[2]x[3] x[4]

Array Initializers if you do not include enough initializers, fills the rest with 0 int x[5] = {1,2,3}; // x[3] and x[4] get value 0 if you include too many initializers, compiler error int x[5] = {1,2,3,4,5,6}; // won't compile when using initializers, can exclude the size of the array altogether C++ uses size of initializer list int x[ ] = {1,2,3,4,5}; // same as x[5] = {1,2,3,4,5};

Arrays and Operators as mentioned, the elements of an array can be used wherever a variable can int i; int a[10]; cout << i << " " << a[4] << endl; cout << 3 + i << " " << 3 + a[4] << endl;

Arrays and Operators the array however, cannot be used just like a variable some common errors: 1) You cannot output all the elements of an array with one redirect* int a[3]; a[0] = a[1] = a[2] = 4; cout << a << endl;

Arrays and Operators the array however, cannot be used just like a variable some common errors: 2) You cannot input all the elements of an array with one redirect* int a[3]; cin >> a; cout << a[0] << endl;

Arrays and Operators the array however, cannot be used just like a variable some common errors: 3) You cannot assign the elements of one array to another array with one assignment int a[3]; a[0] = a[1] = a[2] = 4; int b[3]; b = a; cout << b[0] << endl;

Arrays and Operators the array however, cannot be used just like a variable some common errors: 4) You cannot compare two arrays with one == operator int a[3] = {4, 4, 4}; int b[3] = {4, 4, 4}; if (a == b) cout << "True" << endl; else cout << "False" << endl;

Arrays and Operators as a rule, you typically have to manually perform operations on each element of an array* 1) To output all values: int a[3]; a[0] = a[1] = a[2] = 4; cout << a[0] << endl; cout << a[1] << endl; cout << a[2] << endl; or int a[3]; a[0] = a[1] = a[2] = 4; for (int i = 0; i < 3; i++) cout << a[i] << endl;

Arrays and Operators as a rule, you typically have to manually perform operations on each element of an array* 2) To input all values: int a[3]; cin >> a[0] >> a[1] >> a[2]; or int a[3]; for (int i = 0; i < 3; i++) cin >> a[i];

Arrays and Sizes once you declare the size of an array, you cannot resize it this can be inconvenient, especially if you don't know how much data you need when the amount of incoming data is not known, an array is typically declared to be larger than necessary this typically requires a second integer, for storing the number of values in the array example: write a program to read in a list of numbers from a user, and print out these numbers in reverse. The user will indicate the end of the numbers by entering a 0. int a[3]; // a has three integers in it // for the rest of its lifetime

1. Read in the grades 2. Calculate the average A 3. Add (70 – A) to each grade 4. Re-output the grades #include using namespace std; int main() { const int MAX_SIZE = 512; int numbers[MAX_SIZE]; int length = 0; int input; cout << "Please enter up to 512 numbers, 0 to stop: "; cin >> input; while ( (length < 512) && (input != 0)) { numbers[length] = input; length++; cin >> input; } for (int i = length-1; i >= 0; i--) cout << numbers[i] << endl; return 0; }

Arrays as Function Arguments Array elements can be passed by value only a copy is sent void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x[3] = {0, 1, 2}; swap(x[0], x[1]); cout << x[0] << ","; cout << x[1] << ","; cout << x[2] << endl; } Output: 0,1,2

Arrays as Function Arguments Array elements can be passed to reference parameters void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main() { int x[3] = {0, 1, 2}; swap(x[0], x[1]); cout << x[0] << ","; cout << x[1] << ","; cout << x[2] << endl; } Output: 1,0,2

Passing Arrays as Parameters an entire array can be passed as a parameter Example: Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers.

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Our summation code goes here: Add each element in the array to result.

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers.

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[5] = {1,2,3,4,5}; cout << "Sum = " << sum(x) << endl; int y[5] = {5, 10, 15, 20, 25}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: When sending the array variable as an argument, no parentheses are needed!

Size element in array parameter the size element in the array parameter has no consequence on the code

#include using namespace std; int sum(int a[5]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[6] = {1,2,3,4,5,6}; cout << "Sum = " << sum(x) << endl; int y[7] = {5, 10, 15, 20, 25, 30, 35}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: The size of these arrays are not 5, but 6 and 7.

Size element in array parameter the size element in the array parameter has no consequence on the code in fact, the size of the array can be omitted completely

#include using namespace std; int sum(int a[]) { int result = 0; for (int i = 0; i < 5; i++) { result += a[i]; } return result; } int main() { int x[6] = {1,2,3,4,5,6}; cout << "Sum = " << sum(x) << endl; int y[7] = {5, 10, 15, 20, 25, 30, 35}; cout << "Sum = " << sum(y) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: Size of array not specified!

The last example works fine, but is slightly limited suppose we want to add the first n numbers in our array, where n != 5 Solution: build more functions? inefficient send a second parameter!

#include using namespace std; int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; } int main() { int x[6] = {1,2,3,4,5,6}; cout << "Sum = " << sum(x, 6) << endl; int y[7] = {5, 10, 15, 20, 25, 30, 35}; cout << "Sum = " << sum(y, 7) << endl; return 0; } Write a program with a function called sum that takes an array of 5 integers, and returns the sum of those integers. Note: Size of array specified as second parameter.

In C/C++, when a function takes an array as input, it very often takes in the size of that array as well exception: strings Hence, the format of your functions with array parameters should typically look like previous example: int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; }

Arrays and Memory arrays are passed BY REFERENCE no copy of the array is made only the address of the array is passed no reference indicator (&) is required on the parameter consequently, all changes to the array made in the function are permanent, not local Example: write a function that adds 5 to each value of an array

#include using namespace std; void add5(int a[], int size) { for (int i = 0; i < size; i++) { a[i] += 5; } int main() { int x[3] = {0, 3, 6}; cout << x[0] << "," << x[1] << "," << x[2] << endl; add5(x, 3); cout << x[0] << "," << x[1] << "," << x[2] << endl; return 0; } Example: write a function that adds 5 to each value of an array

#include using namespace std; void add5(int a[], int size) { for (int i = 0; i < size; i++) { a[i] += 5; } int main() { int x[3] = {0, 3, 6}; cout << x[0] << "," << x[1] << "," << x[2] << endl; add5(x, 3); cout << x[0] << "," << x[1] << "," << x[2] << endl; return 0; } Example: write a function that adds 5 to each value of an array

Const Parameters suppose I have an array of important information e.g. a list of student grades suppose that I want to calculate the sum of all of those grades e.g. for computing an average what if I use the sum function?

#include using namespace std; int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; } int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } Example: write a function that adds 5 to each value of an array

Previous example works fine but what if you couldn't see the code for sum?

#include using namespace std; /* this function computes the sum of the first size elements in the array a */ int sum(int a[], int size); int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } // the code for sum is somewhere down here Example: write a function that adds 5 to each value of an array

Problem with the previous code remember that arrays are passed by reference this means that the function can change the values in the array is this safe? for information that you do not want changed (such as a list of grades), not really how do we safeguard against this? Solution 1: check the source code of the called function

#include using namespace std; /* this function computes the sum of the first size elements in the array a */ int sum(int a[], int size); int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } // the code for sum is somewhere down here int sum(int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; } Example: write a function that adds 5 to each value of an array Programmer has to manually check the sum code to make sure no changes to a occur.

Problems with this approach time-consuming what if the function had 500 lines of code error-prone what if programmer overlooked a line difficult sometimes, code is stored in a totally separate header file

Solution: const parameters prepend an array parameter declaration with the keyword const when an array parameter is declared const, then the function is forbidden from changing the values in that array int sum(const int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]; } return result; }

What happens if the function tries to change a value in a const array? int sum(const int a[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += a[i]++; } return result; }

#include using namespace std; /* this function computes the sum of the first size elements in the array a */ int sum(const int a[], int size); int main() { int marks = {87, 62, 49, 71, 55}; cout << "Average = " << sum(marks, 5) / 5.0 << endl; return 0; } // the code for sum is somewhere down here Example: write a function that adds 5 to each value of an array Programmer can call sum with total confidence that the array will not be altered, without even knowing what the source code of sum looks like.

When should you declare an array parameter const? whenever the function is not going to change any values of the array two purposes: 1) guarantees other programmers using your function that no changes will occur 2) good error check for yourself if you accidentally write code that changes the array, compiler will alert you

Example: write a function that returns the maximum value of a set of integers int sum(int a[], int size) { int biggest = a[0]; for (int i = 1; i < size; i++) if (a[i] > biggest) biggest = a[i]; return biggest; } does this function change any values in a? no therefore, we should declare it const

Example: write a function that returns the maximum value of a set of integers int maximum(const int a[], int size) { int biggest = a[0]; for (int i = 1; i < size; i++) if (a[i] > biggest) biggest = a[i]; return biggest; } does this function change any values in a? no therefore, we should declare it const

Example: write a function that sets all negative values to positive in a function void sum(int a[], int size) { for (int i = 0; i < size; i++) a[i] = abs(a[i]); } does this function change any values in a? yes therefore, we should leave it as non-const

Const Reference Parameters we can also declare const reference parameters this guarantees that we will not make any changes to the parameters in the calling function this is useful when we want to send data to the function, but do not want to make copies e.g. for strings

#include using namespace std; void bio(string &name, string &city, string &province); int main() { string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta"; bio(name, city, province); return 0; } void bio(string &name, string &city, string &province) { cout << name << " currently lives in " << city << ", " << province << endl; } write a function that takes a name, city, and province as inputs, and writes a short bio of the person. No guarantee function will not alter strings, unless function code is checked.

#include using namespace std; void bio(const string &name, const string &city, const string &province); int main() { string name = "Kevin"; string city = "Lethbridge"; string province = "Alberta"; bio(name, city, province); return 0; } void bio(const string &name, const string &city, const string &province) { cout << name << " currently lives in " << city << ", " << province << endl; } write a function that takes a name, city, and province as inputs, and writes a short bio of the person. Function is not allowed to change the const values.

When should you declare reference parameters const? whenever the function is not going to change their values const reference parameters are often used for large data types strings structures/classes (stay tuned!!)

void swap(int &a, int &b) { int temp = b; b = a; a = temp; } does this function change its reference parameters? yes therefore, they cannot be const