CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 

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

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.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
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.
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.
Chapter 8 Arrays and Strings
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. Science Means Solving Problems  Physics -- How does an atom work?
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.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Question of the Day  A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
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.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CSC 107 – Programming For Science. The Week’s Goal.
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.
Generic Types  Recent release of Java added generics  Include type parameters in class definition  Like methods, parameters can change each time 
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
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,
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”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
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.
Problem of the Day How can you make 16 right angles using 4 matchsticks WITHOUT breaking any of them?
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
PHY 107 – Programming For Science. Announcements  No week #5 weekly assignment  Take time to study for Friday’s midterm instead  No lab or office hours.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
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.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
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.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
CSC Programming for Science Lecture 26: Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
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.
EGR 2261 Unit 10 Two-dimensional Arrays
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries  Using values stored in an array  Know connection between arrays and loops

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

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 indices like -1 or that stupid  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  Each array entry behaves like variable  Accessed via array variable is only difference  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: long loserArray[10]; loserArray[0] = 1; for (long i=0; i < sizeof(loserArray);i++){ loserArray[i] = loserArray[i-1] * 2; cout << loserArray[i] << endl; }

Using An Array  Each array entry behaves like variable  Accessed via array variable is only difference  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: long loserArray[10]; loserArray[0] = 1; for (long i=0; i < sizeof(loserArray);i++){ loserArray[i] = loserArray[i-1] * 2; cout << loserArray[i] << endl; }

Using An Array  Each array entry behaves like variable  Accessed via array variable is only difference  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: long loserArray[10]; loserArray[0] = 1; for (int i=0; i < sizeof(loserArray); i++){ loserArray[i] = loserArray[i-1] * 2; cout << loserArray[i] << endl; }

Using An Array  Each array entry behaves like variable  Accessed via array variable is only difference  To use or assign entry, specify index inside brackets  Example code snippet computing powers of 2: const int BORED_NOW = 10; long loserArray[BORED_NOW]; loserArray[0] = 1; for (int i=0; i < BORED_NOW; i++){ loserArray[i] = loserArray[i-1] * 2; cout << loserArray[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 Midterm  You can use on this midterm:  Your textbook & notes IF  Printout of slides IF has notes on that day's slides  At the same time, you may NOT use:  Computer, calculator, cell phone, or similar  Copies of daily activities and/or solutions  Friends, Romans, countrymen or their ears  To be certain rules are followed, when test ends  Hand in all printed material you had with you

How to Prepare for Midterm DODON'T  Make cheat sheets for the test  Converting to & from decimal  Add post-its to important pages  Review what parts of C++ do  Assume notes replace studying  Memorize  Drink case of 40s before test  Use post-its as clothing

For Next Lecture  Read 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?  Midterm on Tuesday  Test will be open-book, open-note, but closed activity  Programming Assignment #2 now on Angel  This is a larger assignment; start it now!