Presentation is loading. Please wait.

Presentation is loading. Please wait.

Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle.

Similar presentations


Presentation on theme: "Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle."— Presentation transcript:

1 Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle

2 §Assignment 2 is up.

3 Programming Tip: §Choosing formal parameter names

4 #include int rand(void)

5 §List of marks §List of names §…

6 Arrays offer a convenient means of grouping together several related variables Arrays

7 Declaring an array §type array_name[size]; l allocates memory for size variables l index of first element is 0 l index of last element is size-1 l size must be a constant

8 Declaring an array- Example §Example: int list[10]; l allocates memory for 10 integer variables l index of first element is 0 l index of last element is 9 l C++ does not perform any bounds checking on arrays list[1] list[0] list[9]

9 Arrays One- dimensional arrays type var_name[size]; int sample[10]; double d[30]; char ch[100];

10 Initializing Arrays §Arrays can be initialized at the time they are declared. Examples: double taxrate[3] ={0.15, 0.25, 0.3}; int num[10] ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

11 Initializing Arrays §What is wrong with the following? int num=10; int iarray[num]; int size; cin>>size; int myArray[size]; double dArray[5]={3.2, 4.5, 6.7, 324.0, 45.8, 23.1, 34.9};

12 Assigning values to an array Example int list[10]; int count, i; cin >> count; for(int i=0; i<count; i++) cin >> list[i]; for loops are often used to assign values to an array

13 Assigning values to an array Example int list[10]; int count, i; cin >> count; for(int i=0; i<count; i++) cin >> list[i]; What if count >9?

14 Assigning values to an array int main() { // this reserves 10 integer elements int sample[10]; int t; // load the array for(t=0; t<10; ++t) sample[t]=t; // display the array for(t=0; t<10; ++t) cout << sample[t] <<“ “; return 0; } //output ?

15 Arrays Output is: 0 1 2 3 4 5 6 7 8 9

16 Summarize Arrays §An array is an indexed data structure l An element of an array is accessed using the array name and an index §An array stores a collection of variables §All variables stored in an array are of the same data type §An individual variable within an array is called an element of the array

17 Summarize Arrays §In C++ all arrays are stored in contiguous memory locations §An index describes the position of an element within an array. §In C++ all arrays have zero as the index of their first element. §The name of the array is the address of the first element. The index is the offset

18 Arrays §Total size of an array in bytes? §Total bytes = number of bytes in type x number of elements §e.g. int sample[10]; §sizeof operator

19 Arrays int a[10]; cout<<sizeof(a)<<endl; cout<<sizeof(a[0])<<endl; cout<<sizeof(int)<<endl; double d[10]; cout<<sizeof(d)<<endl; cout<<sizeof(d[0])<<endl; cout<<sizeof(double)<<endl;

20 Arrays 40 4 80 8

21 Arrays int main() { int i, min_value, max_value; int list[10]; for(i=0; i<10; i++){ list[i] = rand(); cout<<list[i]<<" "; }

22 Arrays // find minimum value min_value = list[0]; for(i=0; i<10; i++) { if(min_value>list[i]){ min_value = list[i]; } cout << "minimum value: " << min_value << “\n”;

23 Arrays // find maximum value max_value = list[0]; for(i=0; i<10; i++){ if(max_value<list[i]){ max_value = list[i]; } cout << "maximum value: " << max_value << '\n'; return 0; }

24 Arrays int a[10], b[10]; //... a = b; // error -- illegal /*So how do we make the contents of one array same as the other?*/

25 Arrays // An incorrect program. int main() { int crash[10], i; for(i=0; i<100; i++) crash[i]=i; return 0; }

26 Arrays - No bounds checking // An incorrect program. int main() { int crash[10], i; for(i=0; i<100; i++) crash[i]=i; return 0; }

27 Sorting an array §Lots of applications require sorting §Algorithm for sorting


Download ppt "Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle."

Similar presentations


Ads by Google