Download presentation
Presentation is loading. Please wait.
Published byAshlyn Greene Modified over 9 years ago
1
Copyright 1998-2007 Curt Hill Arrays in C/C++ More on usage
2
Copyright 1998-2007 Curt Hill Tracing Somewhat problematic Since one name has many values we have to be careful to keep track of them properly Thus our tracing chart widens
3
Copyright 1998-2007 Curt Hill Voting Machine Again // Properties in the class int counts[20];... // these set to zero for(int i=0;i<20;i++) counts[i] = 0;... void action () { int vote; cin >> vote; if(vote>=0 && vote<=20){ counts[vote]++; } else // invalid vote }
4
Copyright 1998-2007 Curt Hill Technique In the vote example we knew in advance how many candidates before we got started –At compile time What if we do not know? –Two possibilities Make array large and do not fill it C/C++ also has dynamic arrays –Any pointer may refer to a simple item or an array of items
5
Copyright 1998-2007 Curt Hill UnFull Array Technique Consider this declaration: int used = 0, vals[1000]; There are now two integers associated with vals: –used is the number that are used –1000 is the maximum size Each time a value is added: if(used<1000) vals[used++]=a;
6
Copyright 1998-2007 Curt Hill Technique Continued The only time 1000 is used is to see if a new item may be added All other references refer to used, such as a display: for(int i=0;i<used;i++) cout << i << “ “ << vals[i] <<“\n”;
7
Copyright 1998-2007 Curt Hill Discussion In other languages the used variable could either refer to: –The last used value –The first unused value In the C family of languages it is almost always the latter –This makes it similar to array size usage –Thus the for starts at zero and has an end conditional of strictly less than
8
Copyright 1998-2007 Curt Hill More Discussion A function is completely unable to determine the size of the array at run-time This kills array bounds checking –A compiler that can not check something all the time will generally not do it ever It also does not matter whether we pass the function the size of the array or the number of items used
9
Copyright 1998-2007 Curt Hill Example: Statistics Consider that we wish to make a simple statistics program It should allow the entry of values through a window It should also allow display and compute the average As time permits compute median or mode
10
Copyright 1998-2007 Curt Hill Data Structure The central data structure is the array: double data [MAX]; int count = 0; Adding an item to the array: if(count < MAX) data[count++] = val; Now some functions
11
Copyright 1998-2007 Curt Hill Find the average double avg(double d[], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg
12
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d size 0 1 2 3 4 5 6 7 8 9 10 … 4 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
13
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 4 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
14
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 4 i 0 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
15
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 4 i 0 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
16
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4 i 0 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
17
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4 i 0101 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
18
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4 i 0101 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
19
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 4 i 0101 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
20
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 4 i 012012 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
21
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 8.3 4 i 012012 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
22
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 8.3 4 i 01230123 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
23
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 8.3 13.7 4 i 01230123 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
24
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 8.3 13.7 4 i 0123401234 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
25
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 8.3 13.7 4 i 0123401234 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
26
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 8.3 13.7 4 i 0123401234 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 …
27
Copyright 1998-2007 Curt Hill Tracing double avg(double d [], int size){ double sum = 0; for(int i = 0;i 0) // prevent zero division return sum/size; else return 0; } // end of avg d sizesum 0 1 2 3 4 5 6 7 8 9 10 … 0 1.2 4.3 8.3 13.7 4 i 0123401234 1.2 3.1 4.0 5.4 0.1 9.1 3.3 3.1 -3.1 9.1 … result 3.425
28
Copyright 1998-2007 Curt Hill Parameter Passage When a function receives a value parameter as an actual parameter, it receives a copy No matter what the method does the formal parameter is not changed Since array names are pointers a function receives a pointer The pointer cannot be changed but the array to which it refers can be
29
Copyright 1998-2007 Curt Hill Tracing Again Consider the following worthless code: void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x);
30
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 10 8 4
31
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 10 8 4 12 xar
32
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 10 8 4 12 xar 0 i
33
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 684684 xar 0 i
34
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 684684 xar 1 i
35
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 654654 xar 1 i
36
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 654654 xar 2 i
37
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 653653 xar 2 i
38
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 653653 xar 3 i
39
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 653653 xar 3 i
40
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 653653 -100 xar
41
Copyright 1998-2007 Curt Hill Tracing void m1(int ar[], int s, int x){ for(int i = 0;i<s;i++) ar[i] = ar[i]/2 + 1; x = -100; } … int z[]={10,8,4}, x=12; m1(z,3,x); x z 12 653653
42
Copyright 1998-2007 Curt Hill Discussion A value parameter passed to a function or method just passes a copy –The original cannot be changed An array just passes the pointer –The array may changed but not the pointer Arrays may thus have their values changed
43
Copyright 1998-2007 Curt Hill Partial Arrays Beside using the unfull array technique we may also process partial arrays A partial array is a subset of the full array This can be accomplished in two ways –Passing a smaller length, which we have seen –Passing something other than the beginning address
44
Copyright 1998-2007 Curt Hill Lets Not Start at Beginning Since –an array name is just a pointer –the bracket is an address modification operator –we may use both to pass a fragment of an array to a function Suppose: int fun(int d[], int size); int a[1000];... a[0] = fun(&a[10],100);
45
Copyright 1998-2007 Curt Hill Discussion The expression a[10] is just an int The address of operator may then obtain its address This is then passed to the function Of course the function believes that the subscript range starts at zero So d[0] actually corresponds to a[10] Of course, &a[i] works in the same way
46
Copyright 1998-2007 Curt Hill Conclusions Arrays are a valuable means to structure data They require a subscript (or index) to access individual elements Only parameter passage uses an array without an index Methods may change array elements Multiple dimension arrays are also possible but will be discussed later
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.