Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 CSC 107 – Programming For Science

2 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

3 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

4 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

5 Declaring Array Variables

6 Accessing Array Entries

7 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; }

8 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[]

9 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));

10 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));

11 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));

12 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));

13 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));

14 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));

15 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));

16 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));

17 Using Array Parameters

18 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; }

19 Your Turn  Get into your groups and try this assignment

20 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 )


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

Similar presentations


Ads by Google