CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
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.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of.
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What the null character is; why.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
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  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
CSC 107 – Programming For Science. Today’s Goal  Learn what structures are & how they are used  Why we need & why would use a structure  What fields.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
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.
CSC 107 – Programming For Science. Today’s Goal  Know how to write selections besides if-else  When each of the options makes sense  When each selection.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
CSC Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
© 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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
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.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Topics discussed in this section:
C++ Pointers and Strings
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
C++ Pointers and Strings
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function, how can we pass an array  Understand why parameter arrays are "intersesting”  Array’s love life is complex, but can explain this  If you look, almost always see arrays going with loops ♥  Functions ♥ arrays, but invite a third into relationship

Can Make Stronger, Bigger  Arrays  Arrays are variables that can hold many items  Creates range of locations in which to store data  Locations are numbered sequentially from 0 entries  Array entries like variables in their own right  To be able to use them, must declare array (& entries)  Value unknown until assigned in the program  But not a true variable, entries depend on array  Access only via array using the entry's index

Can Make Stronger, Bigger  Arrays  Arrays are variables that can hold many items  Creates range of locations in which to store data  Locations are numbered sequentially from 0 entries  Array entries like variables in their own right  To be able to use them, must declare array (& entries)  Value unknown until assigned in the program  But not a true variable, entries depend on array  Access only via array using the entry's index

Declaring Array Variables

Accessing Array Entries

Trace int main() { const int HELLO_LEN= 5; char word[HELLO_LEN]={'H','e','l','l','o'}; for (int i=0; i < sizeof(word)/sizeof(char); i++){ cout << word[i]; } cout << ", I love you." << endl; cout <<"Won't you tell me your name?"<< endl; return 0; }

Arrays & Entries As Parameters  Entries of an array usable as function argument  Entry like variable & can be used if variable legal  Use in both pass-by-value & pass-by-reference legal cout << pow(dubs[1], 2) << endl; cout << sin(dubs[0]) << endl;  Can also pass entire array as function argument  But only if function’s parameter matches array type  Types should be equal  Types should be equal, promotion not guaranteed  Array’s length will be lost – need another parameter  Write array parameters as: type name[]

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Array Parameters Example double average(int arr[], int n); void printData(double data[], int len); void readData(double data[500]); double bill[100]; int basil[500]; average(basil, sizeof(basil)/sizeof(int)); average(basil, 30); average(bill, 100); readData(basil); readData(bill); printData(basil, sizeof(basil)/sizeof(int)); printData(bill, sizeof(bill)/sizeof(double));

Using Array Parameters

Trace void calcInt(double a[], int n,double cst,double intrst){ a[0] = cst; for (int i = 1; i < n; i++) { cst *= (1 + intrst); a[i] = cst; } } int main() { const int BRIBE_LEN= 5; double amt[BRIBE_LEN]; calcInt(amt, BRIBE_LEN, 10, 0.1); for (int i=0; i < BRIBE_LEN; i++){ cout << amt[i] << endl; } return 0; }

Your Turn  Get into your groups and try this assignment

For Next Lecture  Read about cStrings in Section 10.7 – 10.8  What is this magic power of text in quotes?  How can we read & compare words?  What is this null terminator & why does it matter?  Weekly Assignment #7 available now on Angel  As usual, assignment will be due on Tuesday  Programming Assignment #2 also on Angel  Larger project will be due in 11 days (Friday, Oct. 28 th )