CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Slides:



Advertisements
Similar presentations
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
Advertisements

1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
CSC Programming for Science Lecture 30: Pointers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Multiple-Subscripted Array
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?
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
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.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
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  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CSC 107 – Programming For Science. The Week’s Goal.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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 :
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.
PHY 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
CSC Programming for Science Lecture 28: Multi-dimensional Arrays.
CSC Programming for Science Lecture 34: Dynamic Pointers.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
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.
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.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CSC Programming for Science Lecture 26: Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
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.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
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.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
CSC Programming for Science Lecture 5: Actual Programming.
Variables A piece of memory set aside to store data
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Data Structures and Algorithms Introduction to Pointers
CS150 Introduction to Computer Science 1
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal

Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to remember  Assignments update memory location with new value  Memory location updated by assignment ONLY  When variable is used in program…  …uses current value at that memory location  Variable can store only one value  Often want multiple values, like adjusting for interest

Getting an “A”

Adjusting For Interest  First call, computed result for every year  Then all but last year computed in second call  Third time we called function, computed all but last 3 … and so on…  Only adjusted for 1 year last time we called function  Interest rate, amount, and results were constant  Unless save in variables, we have to recompute  Using variables required knowing years when coding

Could Have Spent Time

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  Like all variables, must declare before use size  Type, name, & size needed for array declaration  Still variables, so names follow usual rules  Variable is array of the type, so use any legal type  Each of the array's entries hold value of that type  Size must be integer since ½ a value hard to use

Declaring Array Variables  Like all variables, must declare before use size  Type, name, & size needed for array declaration  Still variables, so names follow usual rules  Variable is array of the type, so use any legal type  Each of the array's entries hold value of that type  Size must be integer since ½ a value hard to use

Declaring Array Size

Initializing an Array

Legal Array Entries  Access array's entries indexed from 0 to size-1  0, 1, 2, 3, 4 legal if size of 5 used to declare array  Array created with size of 8: 0, 1, 2, 3, 4, 5, 6, 7 legal  0 only legal index if size declared as 1

Legal Array Entries

Guns (& C++) Don't Kill  C++ make arrays easy, but mistakes easy also  Code can access any index within an array int  No problems compiling, as long as index an int  Includes ridiculous indices like -1 or  To find size, could try using sizeof( array variable )  Entry outside array bounds accessed by program  Program may crash with “Segmentation Fault”  Other variable's value used and updated  Program may be able to complete normally

Using An Array  Within an array, each entry behaves like variable  But need array variable to access the entry  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: long lA[10]; lA[0] = 1; for (long i=0; i < sizeof(lA)/sizeof(long);i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl; }

Using An Array  Within an array, each entry behaves like variable  But need array variable to access the entry  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: long lA[10]; lA[0] = 1; for (long i=0; i < sizeof(lA)/sizeof(long);i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl; }

Using An Array  Within an array, each entry behaves like variable  But need array variable to access the entry  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: long lA[10]; lA[0] = 1; for (int i=0; i < sizeof(lA)/sizeof(long);i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl; }

Using An Array  Within an array, each entry behaves like variable  But need array variable to access the entry  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: const int BORED_NOW = 10; long lA[BORED_NOW]; lA[0] = 1; for (int i=0; i < BORED_NOW;i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl; }

Let's Trace This Code int main() { const int BORED_NOW = 4; long loserArray[BORED_NOW]; loserArray[0] = 1; for (int i=1; i < BORED_NOW; i++){ loserArray[i] = loserArray[i-1] * 2; cout << loserArray[i] << endl; } cout << "Sorry its stupid!" << endl; return 0; }

Your Turn  Get into your groups and try this assignment

For Next Lecture  Read more about arrays in Section 10.5  How can we pass arrays as parameters?  Can values be changed in the array no matter what?  Why couldn't they be consistent about params?  Weekly Assignment #8 out & due Tuesday  Avoid the rush by start working on it now  Programming Assignment #2 now on Angel  Could start working on this now – know all you need