CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)

Slides:



Advertisements
Similar presentations
C Programming lecture 2 Beautiful programs Greek algorithms
Advertisements

CS 31 Discussion, Week 9 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
 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.
Multiple-Subscripted Array
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Chapter 8 Arrays and Strings
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
CS 31 Discussion, Week 4 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Chapter 7: User-Defined Functions II
Suppose we want to print out the word MISSISSIPPI in big letters.
New Structure Recall “average.cpp” program
Student Book An Introduction
CS 106A, Lecture 7 Parameters and Return
Arrays Kingdom of Saudi Arabia
Arrays Arrays A few types Structures of related data items
Presentation transcript:

CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)

Reflections on Project 3 Get started early Develop incrementally and with tests – write a test case for a function before you’ve implemented it so that you know when it works Functions will save you time and effort – “rule of three”: if you repeat the same large block of code more than twice, it should probably be in a function Passing by reference is useful

Common Errors in Project 3 Misunderstandings about how functions are defined – Functions cannot be nested inside of other functions – Functions that are not main() must be called from main() or they won’t run at all Trouble with loops – For loops are good for stepping over items one at a time, not so great for stepping over an unknown number of items – Use while loops when you don’t know how far you’re going to be stepping forward Strings – Before you attempt to access a character in a string, you must be sure that the character exists if (k < text.size()) /* do something with text[k]… */

How do we store lots of data? So far we’ve stored data in variables, either as input from the user, hardcoded values, etc. – int x = 32; // ‘x’ is hardcoded as 32 One variable, one value Today we’re going to learn how to store lots of data in a structure called an array

The Problem Let’s say you want to store ten values – One variable, one value so far… Could create variables int x1; int x2; int x3; etc. – This is clearly cumbersome, but it’s the best we can do so far – We also can’t iterate over these; we’d have to write an expression to deal with them one at a time You could also stick them in a string – but then they’d be text and you’d have to extract them when you want to use them (also burdensome)

The Solution An array is a collection of variables that all have the same type and are declared at the same time Declaring one looks like this: int vals[10]; You can set and read from the array like any other variable – e.g. vals[0] = 12; We’ll go into more depth on that in a bit…

Declaring Arrays int vals[10]; The above declares an array of type int and of size 10 Arrays can be any type we’ve learned so far – ints, doubles, strings, booleans… – All of the entries in the array are of the same type The number in the brackets is the number of slots and must be a positive integer – That is, > 0 and a whole number

Using Arrays You can put a value into an array (after it’s been declared) like so: vals[3] = 12; The number in the brackets is the slot into which you’re storing – The [3] is called the subscript operator; it’s used to set and retrieve values from an array – Like strings, the slot numbers start at 0 and go up to length-1 In the above, we’re storing to slot #4 out of 10 – It’s illegal to access a slot number past the maximum (your program will crash) You can also get a value out of an array like so: cout << vals[3] << endl;

Types and Declaration Variations Static allocation: int myArray[5] = { 5, 3, 6, 7, 9 }; Length can also be implied: int myArray[] = { 8, 8, 8, 9 }; Arrays of strings: string cities[] = { “Glendale”, “Pasadena”, “West Covina” }; As usual, you can have newlines anywhere you like

Array Pitfalls There is no “size()” function for arrays – You just have to remember how large they are – Or better yet, use a constant that you can refer to later You can’t print an entire array using cout cout << myArray << endl; // this won’t work Again, attempting to access a slot past the end of the array is the most common (and most disastrous) mistake people make with arrays

Arrays and Functions Just like any other variable, you can declare arrays as parameters to functions – Which implies that you can pass them as arguments Unlike other parameters, arrays are always passed by reference – Changes to the array that you pass in the function will be reflected in the caller’s array – If you want to avoid this, use the “const” modifier Let’s look at an example…

Summary Arrays are useful for dealing with large numbers of values Arrays can be created like so; size must be > 0 and an integer: int myArray[200]; Arrays can be set to default values: int myOtherArray[] = { 1, 3, 5, 7, 9, 11 }; Array slots are set and retrieved via the subscript (square brackets) operator: – Set: myArray[3] = 12; – Retrieve: for (int i = 0; i < 200; i++) cout << myArray[i] << endl;

Summary #2 Arrays can be passed to functions – They’re always passed by reference Can prevent modification using “const” modifier – Syntax for taking an array as a parameter: void printAll(const int list[], int len) { for (int i = 0; i < len; i++) { cout << “#” << (i+1); cout << “: “ << list[i] << endl; } Don’t go past the end of an array or before the beginning!