understanding arrays and how they occupy computer memory array –a sequenced collection of elements...
TRANSCRIPT
![Page 1: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/1.jpg)
Understanding Arrays and How They Occupy Computer Memory
• Array– A sequenced collection of elements of the same
data type is called array– Series or list of variables in computer memory– All variables share the same name but with different
index– Each variable has a different subscript
• Element– Each item in array is called element.
Array 1
![Page 2: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/2.jpg)
How Arrays Occupy Computer Memory
• Subscript (or index)– Position number of an item in an array– Subscripts are always a sequence of integers
• Array elements are contiguous in memory
• Size of the array: number of elements it will hold
Array 2
![Page 3: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/3.jpg)
How Arrays Occupy Computer Memory (continued)
Array 3
Figure 6-1 Appearance of a three-element array in computer memory
![Page 4: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/4.jpg)
How Arrays Occupy Computer Memory (continued)
• All elements have same group name– Individual elements have unique subscript– Subscript indicates distance from first element– Subscripts are a sequence of integers
• Subscripts placed in parentheses or brackets following group name
Array 4
![Page 5: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/5.jpg)
Declaration & Initialization of Array
• Syntax in C to declare an arraydatatype Name[size];
– Exampleint subjectMark[7];
• In C language index start with the value 0
– int count[3];• It will declare 6 variable named with count and can be
accessed by count[0],count[1],count[2]
• Example of declaration with initialization– int count[6]={0};
• It will initialize the each variable of the array with the value 0
Array 5
![Page 6: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/6.jpg)
Declaration & Initialization of Array(continued)
– int count[6]={1,2};• It will initialize the first element with the value 1 which
have the index 0;
– Example: int num[3]; • First value is stored on index 0• scanf(“%d”,&num[0]);
– String is a series of characters treated as a unit.– char name[4]=“FOP”;
• Name is a string having size 4
Array 6
F O P \0
![Page 7: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/7.jpg)
Manipulating an Array to Replace Nested Decisions
• Example: Human Resources Department Dependents report– List employees who have claimed zero through five
dependents• Assume no employee has more than five dependents
• Application produces counts for dependent categories – Uses series of decisions
• Application does not scale up to more dependents
Array 7
![Page 8: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/8.jpg)
Array 8
Figure 6-3 Flowchart and pseudocode of decision-making process using a series of decisions—the hard way
![Page 9: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/9.jpg)
Manipulating an Array to Replace Nested Decisions (continued)
• Array reduces number of statements needed
• Six dependent count accumulators redefined as single array
• Variable as a subscript to the array
• Array subscript variable must be:– Numeric with no decimal places– Initialized to 0– Incremented by 1 each time the logic passes through
the loop
Array 9
![Page 10: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/10.jpg)
Array 10
Figure 6-4 Flowchart and pseudocode of decision-making process—but still the hard way
![Page 11: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/11.jpg)
Array 11
Figure 6-5 Flowchart and pseudocode of decision-making process using an array—but still a hard way
![Page 12: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/12.jpg)
Code Replacing the nested structureint main(){
int dep,i,count[6]={0};
printf(“Give the Number of dependent:”);
scanf(“%d”,&dep);//Prime read
while(dep>=0&&dep<6)
{
count[dep]=count[dep]+1;
printf(“Give the Number of dependent:”);
scanf(“%d”,&dep);
}
for(i=0;i<6;i++)
{
printf(“No of employee having %d child=%d”,i,count[i]);
}return 0;
}Array 12
![Page 13: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/13.jpg)
Variable & Constant Arrays
• When the value of the array element is changed during the execution time is called variable array.
• When the array value is assigned at the coding time which is permanent and final is called constant array.
• Example:
– int FloorRent[4]={350,400,600,100};
Array 13
Floor Rent
0 350
1 400
2 600
3 1000
![Page 14: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/14.jpg)
//code to make a list with rent. Single line comment
int main(){
char name[20];
int floor;
const intFloorRent[4]={350,400,600,100};
printf(“Give the floor number & name”);
scanf(“%d”,&floor);
scanf(“%s”,name);
while(floor>=0 && floor<4)
{
printf(“Name:%s,Rent=%d”,Name,FloorRent[floor]);
printf(“Give the floor number & name”);
scanf(“%d”,&floor);
scanf(“%s”,name);
}
return 0;
}Array 14
![Page 15: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/15.jpg)
Using a Constant as an Array Subscript
• Use a numeric constant as a subscript to an array
• Example– Declare a named constant as num INDIANA = 5– Display value with:
output salesArray[INDIANA]
Array 15
![Page 16: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/16.jpg)
Searching an Array
• Sometimes must search through an array to find a value
• Example:– Class numbers are three-digit, non-consecutive
numbers– Faculty enters class number, check if class number is
valid– Create an array that holds valid class numbers– Search array for exact match
Array 16
![Page 17: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/17.jpg)
int main(){
int class,i=0,flag=0,ClassCode[5]={102,203,101,203,104};printf(“Enter class number:”);scanf(“%d”,&class);while(i<5){
if(ClassCode[i]==class){
flag=1;}i++;
}if(flag==1)
printf(“Correct class”);else
printf(“Incorrect class”);} Array 17
![Page 18: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/18.jpg)
Searching an Array (continued)
• Flag: variable that indicates whether an event occurred
• Technique for searching an array– Set a subscript variable to 0 to start at the first element– Initialize a flag variable to false to indicate the desired
value has not been found– Examine each element in the array– If the value matches, set the flag to True– If the value does not match, increment the subscript
and examine the next array element
Array 18
![Page 19: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/19.jpg)
Using Parallel Arrays
• Example: studentId-mark – Two arrays, each with sixty elements
• Valid student id [1,2,3,…,60]
• Valid marks [ 0-50 ]
– Each price in valid item price array in same position as corresponding item in valid item number array
• Parallel arrays– Each element in one array associated with element in
same relative position in other array
• Look through valid item array for customer item– When match is found, get price from item price array
Array 19
![Page 20: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/20.jpg)
Array 20
Figure 6-9 Parallel arrays
studId 1 2 3 4 5
Marks 20 44 33 22 11
![Page 21: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/21.jpg)
Using Parallel Arrays
• Use parallel arrays– Two or more arrays contain related data– A subscript relates the arrays
• Elements at the same position in each array are logically related
Array 21
![Page 22: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/22.jpg)
/*Program to store student data in parallel array. Here studId[5],marks[5] are parallel array.
Array using for loop.
*/
int main()
{
int studId[5],marks[5],i;
for(i=0;i<5;i++)
{
scanf(“%d”,&studId[i]);
scanf(“%d”,&marks[i]);
}
return 0;
}Array 22
![Page 23: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/23.jpg)
Improving Search Efficiency
• Program should stop searching the array when a match is found
• The larger the array, the better the improvement by doing an early exit
Array 23
![Page 24: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/24.jpg)
int main(){
int class,i=0,flag=0,ClassCode[5]={102,203,101,203,104};printf(“Enter class number:”);scanf(“%d”,&class);while(i<5 && flag==0){
if(ClassCode[i]==class){
flag=1;}i++;
}if(flag==1)
printf(“Correct class”);else
printf(“Incorrect class”);}
Array 24
![Page 25: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/25.jpg)
Searching an Array for a Range Match
• Sometimes programmers want to work with ranges of values in arrays
• Example: mail-order business– Read customer order data; determine discount
based on quantity ordered
• First approach– Array with as many elements as each possible order
quantity– Store appropriate discount for each possible order
quantity
Array 25
![Page 26: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/26.jpg)
Searching an Array for a Range Match (continued)
Array 26
Figure 6-13 Usable—but inefficient—discount array
![Page 27: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/27.jpg)
Searching an Array for a Range Match (continued)
• Drawbacks of first approach– Requires very large array with the use a lot of
memory– Stores same value repeatedly– Difficult to know the no of elements
• Customer can order more
• Better approach– Create four discount array elements for each
discount rate– Parallel array with discount range
Array 27
![Page 28: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/28.jpg)
Searching an Array for a Range Match (continued)
Array 28
Figure 6-14 Parallel arrays to use for determining discount
![Page 29: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/29.jpg)
Psuedo code determines discount ratestart
num quantity,x
num SIZE=4
num Discount[SIZE]=0,0.10,0.15,0.20
num Discount_Range[SIZE]=0,9,13,26
get quantity
x=SIZE-1
while quantity<Discount_Range[x]
x=x-1
endwhile
print “Discount rate:”,Discount[x]
stopArray 29
![Page 30: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/30.jpg)
Remaining within Array Bounds
• Every array has finite size– Number of elements in the array– Number of bytes in the array
• Arrays composed of elements of same data type
• Elements of same data type occupy same number of bytes in memory
• Number of bytes in an array is always a multiple of number of array elements
• Access data using subscript containing a value that accesses memory occupied by the array
Array 30
![Page 31: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/31.jpg)
Array 31
Figure : Determining the month string from user’s numeric entry
![Page 32: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/32.jpg)
Remaining within Array Bounds (continued)
• Program logic assumes every number entered by the user is valid
• When invalid subscript is used:– Some languages stop execution and issue an error– Other languages access a memory location outside
of the array• Invalid array subscript is a logical error• Out of bounds: using a subscript that is not within
the acceptable range for the array• Program should prevent bounds errors
Array 32
![Page 33: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/33.jpg)
startnum monthnum Max_Month=12String Month_Name[Max_Month]=“January”, February”, ”March”, ”April”, ”May”, ”June”, ”July”, ”August”, September”, ”October”, ”November”, ”December”get monthif month>=1 AND month<=Max_Month then
month=month-1print Month_Name[month]
elseprint “Invalid month”
endifstop
Array 33
![Page 34: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/34.jpg)
startnum monthnum Max_Month=12String Month_Name[Max_Month]=“January”, February”, ”March”, ”April”, ”May”, ”June”, ”July”, ”August”, September”, ”October”, ”November”, ”December”get monthwhile month<1 OR month>Max_Month
print “Invalid month”get month
endwhilemonth=month-1print Month_Name[month]
stop
Array 34
![Page 35: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/35.jpg)
Using a for Loop to Process Arrays
• for loop: single statement– Initializes loop control variable– Compares it to a limit– Alters it
• for loop especially convenient when working with arrays– To process every element
• Must stay within array bounds
• Highest usable subscript is one less than array size
Array 35
![Page 36: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/36.jpg)
Summary
• Array: series or list of variables in memory– Same name and type– Different subscript
• Use a variable as a subscript to the array to replace multiple nested decisions
• Some array values determined during program execution– Other arrays have hard-coded values
Array 36
![Page 37: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/37.jpg)
Summary (continued)
• Search an array– Initialize the subscript– Test each array element value in a loop– Set a flag when a match is found
• Parallel arrays: each element in one array is associated with the element in second array– Elements have same relative position
• For range comparisons, store either the low- or high-end value of each range
Array 37
![Page 38: Understanding Arrays and How They Occupy Computer Memory Array –A sequenced collection of elements of the same data type is called array –Series or list](https://reader037.vdocument.in/reader037/viewer/2022103122/56649cdf5503460f949a8ec1/html5/thumbnails/38.jpg)
Summary (continued)
• Access data in an array– Use subscript containing a value that accesses
memory occupied by the array
• Subscript is out of bounds if not within defined range of acceptable subscripts
• for loop is a convenient tool for working with arrays– Process each element of an array from beginning to
end
Array 38