Numeric Arrays
-
Upload
elaine-sutton -
Category
Documents
-
view
44 -
download
0
description
Transcript of Numeric Arrays
Page 1
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Numeric ArraysChapter 4
Page 2
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
What you What you MUSTMUST know before we start: know before we start:
• What integers and real numbers are
• How integers and real numbers are stored in RAM
• The basic concept of what an address is and how it is used
(Remember: The topics in this course build on each other)
• The use of C/C++ to manipulate integers and real numbers
Page 3
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Numeric ArraysNumeric Arrays• ArrayArray: (def) A Regular Order or Arrangement• For our Purposes, we can define an array as For our Purposes, we can define an array as a data
structure containing a fixedfixed number of contiguouscontiguous storage elements all of the same typeall of the same type
Assume we wished to store the squares of the numbers:
Number: 0 1 2 3 4 5 6 7 8 9
Square: 0 1 4 9 16 25 36 49 64 81
We could store the numbers as scalar variables:
intint d0 = 0, d1 = 1, d2 = 4, d3 = 9, d4 = 16, d5 = 25, d6 = 36, d7 = 49, d8 = 64, d9 = 81;
Page 4
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
The problem with this approach is:The problem with this approach is:
• Remembering the variables names can become complex• Manipulating each of the variables is tedious
For Example, suppose we were trying to find the value 64 in our list, but didn’t know where it was stored:
ifif (d0 == 64) printf(“at d0’’); else ifelse if (d1 == 64) printf(“at d1’’); else ifelse if (d2 == 64) printf(“at d2’’); else ifelse if (d3 == 64) printf(“at d3’’); else ifelse if (d4 == 64) printf(“at d4’’); else ifelse if (d5 == 64) printf(“at d5’’); else ifelse if (d6 == 64) printf(“at d6’’); else ifelse if (d7 == 64) printf(“at d7’’); else ifelse if (d8 == 64) printf(“at d8’’); else ifelse if (d9 == 64) printf(“at d9’’); elseelse printf(“Not found”);
Page 5
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
A preferred alternative would be to store the squares of the numbers as an integer arrayinteger array:
intint numvector[10];
What does this declaration do ???What does this declaration do ???
• Using the datatype intint implies that we will require 2-bytes per element
• the variable name numvector will be associated with the base addressbase address of the array
• [10] indicates how many elements will be in our array
• since we are creating an array of type int, we are requesting 2 * 10 = 20 CONTINGUOUSCONTINGUOUS bytes of RAM
Page 6
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
We could also initialize our array when we declare it:intint numvector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};
OR
intint numvector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};
Even though we do not explicitly indicate how many elements are in the array, we are still requesting 20 bytes of CONTIGUOUSCONTIGUOUS storage(We are just asking the compiler to determine the number)
Because we are specifying only one subscriptsubscript or offsetoffset, we are declaring a vector (a one-dimensional array)
Page 7
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
How are arrays stored in RAM ???How are arrays stored in RAM ???• That depends of the type of data we wish to store• In our case, we are storing an integerinteger array containing
10 elements, so we are requesting 2 * 10 = 20 bytes of CONTIGUOUSCONTIGUOUS storage
• If (at run-time) we find there are 20 contiguous bytes of storage available starting at location 9050:
9050 ([0])
000000009051 ([0])
000000009052 ([1])
000000009053 ([1])
000000019054 ([2])
000000009055 ([2])
000001009056 ([3])
000000009057 ([3])
000010019058 ([4])
000000009059 ([4])
00010009060 ([5])
000000009061 ([5])
000110019062 ([6])
000000009063 ([6])
001001009064 ([7])
000000009065 ([7])
001100019066 ([8])
000000009067 ([8])
010000009068 ([9])
000000009069 ([9])
010100019070
011100019071
110010009072
011010019073
00010111
Page 8
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
At a more abstract, and understandable, level, this might appear as:
NoticeNotice:• The firstfirst element on the list (element number 1) has the
subscript (offset) 0 (zero)• The lastlast element on the list (element number 10) has the
subscript (offset) 9 (nine).
0
9050 & 9051 numvector[0]
1
9052 & 9053 numvector[1]
4
9054 & 9055 numvector[2]
0
9050 & 9051 numvector[0]
1
9052 & 9053 numvector[1]
4
9054 & 9055 numvector[2]
0
9050 & 9051 numvector[0]
1
9052 & 9053 numvector[1]
4
9054 & 9055 numvector[2]
0
9050 & 9051 numvector[0]
1
9052 & 9053 numvector[1]
4
9054 & 9055 numvector[2]
Page 9
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Why is the Why is the firstfirst element subscripted as element subscripted as [0][0] ANDAND why why is it also referred to as the is it also referred to as the offsetoffset ??? ???
In fact, the subscript (at least in C) ISIS the offset from the base addressbase address of the array
REMEMBERREMEMBER: When we first initialized our array, we stated:
• The variable name numvector will be associated with the base addressbase address of the array
• Given a base address, we can use the offset (or subscript) to determine the addresses of any individual element in the array
How does this work ???How does this work ???
Page 10
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
RememberRemember: Our basic directive:
“Give me an addressaddress and tell me what type oftype of datadata is stored there, and I will tell you the valuevalue of that data type”
ArraysArrays allow a convenient way of determining an address
Given an integer array of 10 elements, we can calculate individual array addresses using the formula:
Element address = base address of the arraybase address of the array+ (offset numberoffset number * 2)
(Since 2-bytes are needed for an integer)
Page 11
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Given our the Array (as stored in RAM):Given our the Array (as stored in RAM):
We can calculate element addresses as:We can calculate element addresses as:Offset
0Address:9050 + (0 * 2) = 9050
1 9050 + (1 * 2) = 90522 9050 + (2 * 2) = 90543 9050 + (3 * 2) = 90564 9050 + (0 * 2) = 9058
Offset Address:5 9050 + (5 * 2) = 90606 9050 + (6 * 2) = 90627 9050 + (7 * 2) = 90648 9050 + (8 * 2) = 90669 9050 + (9 * 2) = 9068
9
9056 & 9057 numvector[3]
16
9058 & 9059 numvector[4]
25
9060 & 9061 numvector[5]
0
9050 & 9051 numvector[0]
1
9052 & 9053 numvector[1]
4
9054 & 9055 numvector[2]
81
9068 & 9069 numvector[9]
----
9070 & 9071
----
9072 & 9073
49
9064 & 9065 numvector[7]
64
9066 & 9067 numvector[8]
36
9062 & 9063 numvector[6]
Page 12
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Suppose we made the declaration:Suppose we made the declaration: floatfloat fvector[10];And the base addressbase address of the array was 1234812348In RAM this might appear as:
We could calculate element addresses as:
Offset Address:0 12348 + (0 * 4) = 123481 12348 + (1 * 4) = 123522 12348 + (2 * 4) = 123563 12348 + (3 * 4) = 123604 12348 + (4 * 4) = 12364
Offset5
Address:12348 + (5 * 4) = 12368
6 12348 + (6 * 4) = 123727 12348 + (7 * 4) = 123768 12348 + (8 * 4) = 123809 12348 + (9 * 4) = 12384
-----
12348 to 12351 fvector[0]
-----
12352 to 12355 fvector[1]
-----
12356 to 12359 fvector[2]
-----
12360 to 12363 fvector[3]
-----
12364 to 12367 fvector[4]
-----
12368 to 12371 fvector[5]
-----
12372 to 12375 fvector[6]
-----
12376 to 12379 fvector[7]
-----
12380 to 12383 fvector[8]
-----
12372 to 12375 fvector[6]
Page 13
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Consider the following C Code:Consider the following C Code:#include <stdio.h>#include <stdio.h>void main()void main(){ int { int vector[] = {[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 810, 1, 4, 9, 16, 25, 36, 49, 64, 81};}; int int index;; printf(printf("the base address of vector is %lu\n""the base address of vector is %lu\n",&,&vectorvector);); for (for (indexindex = = 00; ; indexindex < < 1010; ; indexindex++) ++) printf(printf("vector["vector[%d%d] = ] = %d%d; stored at address ; stored at address %lu\n%lu\n",", indexindex, , vectorvector[[indexindex], &], &vectorvector[[indexindex]); }]); }
The (slightly modified) output would be:The (slightly modified) output would be: the base address of vector is 41553194 vector[0] = 0; stored at address 41553194 vector[1] = 1; stored at address 41553196 vector[2] = 4; stored at address 41553198 vector[3] = 9; stored at address 41553200 vector[4] = 16; stored at address 41553202 vector[5] = 25; stored at address 41553204 vector[6] = 36; stored at address 41553206 vector[7] = 49; stored at address 41553208 vector[8] = 64; stored at address 41553210 vector[9] = 81; stored at address 41553212
Page 14
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
What if we entered illegal subscripts? For example, for What if we entered illegal subscripts? For example, for the previous code, what if we entered the for loop the previous code, what if we entered the for loop parameters:parameters:
forfor (index = 8; index < 12; index++) printf ("vector["vector[%d%d] = ] = %d%d; stored at address ; stored at address %lu\n%lu\n",", indexindex, , vectorvector[[indexindex], &], &vectorvector[[indexindex]); ]);
(This is illegal because index should not take on any values larger than 9)
The (slightly modified) output might appear as:
vector[8] = 64; stored at address 41553210 vector[9] = 81; stored at address 41553212 vector[10] = -13107; stored at address 41553214 vector[11] = -15863; stored at address 41553216
Page 15
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
How can there be a How can there be a vectorvector[[1010]]? Or ? Or vectorvector[[1111]]????
• vector[10] is nothing more than address 10 * 2 = 20 bytes offset from the base (41553194 + 20 = 41553114)
• vector[11] is nothing more than address 11 * 2 = 22 bytes offset from the base (41553194 + 22 = 41553116)
The command we issued was: index++Which increments the contents of index (at the time, 9) by 1
Therefore, the location: &&vector[[indexindex]]Would yield the addresses given above
But why is the value stored at, for example, But why is the value stored at, for example, vectorvector[[1010]], , -13107-13107 ??? ???
Page 16
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
If we were to go to address:If we were to go to address: &&vector[[1010]]
41553114We might find
4155314
11001100
4155315
11001101 which equates to
Neg. 011001100110010 One’s Compliment+ 1
011001100110011
= -(213 + 212 + 29 + 28 + 25 + 24 + 21 + 20)= -(8192 + 4096 + 512 + 256 + 32 + 16 + 2 + 1)
Two’s Compliment
= -13,107-13,107
Page 17
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Multi-dimensional ArraysMulti-dimensional ArraysSuppose I wished to store the Squares AND the cubes Suppose I wished to store the Squares AND the cubes of the digits from 0 through 9of the digits from 0 through 9
We could store them as two vectors:
intint vect1[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}, vect2[10] = {0, 1, 8, 27, 64, 125, 216, 343, 512, 729};
Assuming that we did, AND we found that the base addresses of the two arrays were:
vect1 = &vect1[0] = 12350AND
vect2 = &vect2[0] = 12424
the relevant section of RAM might appear as:
Page 18
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
12350 & 12351
0
12352 & 12353
1
12354 & 12355
4
12356 & 12357
9
12358 & 12359
1612360 & 12361
25
12362 & 12363
36
12364 & 12365
49
12366 & 12367
64
12368 & 12369
81
. . . . . . . . . . . . . . . .12424 & 12425
0
12426 & 12427
1
12428 & 12429
8
12430 & 12431
27
12432 & 12433
6412434 & 12435
125
12436 & 12437
216
12438 & 12439
343
12440 & 12441
512
12442 & 12443
729
Where: vect1 = &vect1[0] = 12350AND
vect2 = &vect2[0] = 12424
Page 19
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
OR we could store the values as ONE table:
Table sqrscubes
Row\Col
0
0
0
0
0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 217
7 49 343
8 64 512
9 81 729
Where:
The Row and Column numbers are the table
offsetsoffsets
Page 20
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
What difference does it make if we use one or two What difference does it make if we use one or two arrays ???arrays ???
• With two vectors we need 2 blocks of RAM each containing 20 contiguouscontiguous bytes of RAM
• With one array (matrix) we need 1 block of RAM which contains 40 contiguouscontiguous bytes
• With two vectors we need two variables (two base addresses)
• With one matrix we need one variable (one base address)
• The ORDERORDER in which the data is stored in RAM differs
How is RAM storage different ???How is RAM storage different ???
Page 21
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Consider the following C code:Consider the following C code:void mainvoid main(){ intint matrix[10][2], index; forfor (index = 0; index < 10; index++) { matrix[index][0] = index * index; matrix[index][1] = index * index * index; } }
• Using the datatype intint implies that we will require 2-bytes per element
• since we are creating a 2-dimensional array of type int, we are requesting 2 * 2 * 10 = 40 CONTINGUOUSCONTINGUOUS bytes of RAM
• the variable name matrix will be associated with the base base addressaddress of the array
Page 22
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
If the base address for the array matrix was: 6312863128
The relevant portion of RAM might appear as:
63128 & 63129
0
63130 & 63131
0
63132 & 63133
1
63134 & 63135
1
63136 & 63137
463138 & 63139
8
63140 & 63141
9
63142 & 63143
27
63144 & 63145
16
63146 & 63147
6463148 & 63149
25
63150 & 63151
125
63152 & 63153
36
63154 & 63155
217
63156 & 63157
4963158 & 53159
343
63160 & 63161
64
63162 & 63163
512
63164 & 63165
81
63166 & 63167
729
NOTICE that the data is stored BY ROWS ROWS or by rowor by row offsetoffset
Page 23
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Looking at RAM in terms of offsets, we find:63128 & 63129
matrix[0][0]
63130 & 63131
matrix[0][0]
63132 & 63133
matrix[1][0]
63134 & 63135
matrix[1][1]
63136 & 63137
matrix[2][0]
63138 & 63139
matrix[2][1]
63140 & 63141
matrix[3][0]
63142 & 63143
matrix[3][1]
63144 & 63145
matrix[4][0]
63146 & 63147
matrix[4][1]
63148 & 63149
matrix[5][0]
63150 & 63151
matrix[5][1]
63152 & 63153
matrix[6][0]
63154 & 63155
matrix[6][1]
63156 & 63157
matrix[7][0]
63158 & 53159
matrix[7][1]
63160 & 63161
matrix[8][0]
63162 & 63163
matrix[8][1]
63164 & 63165
matrix[9][0]
63166 & 63167
matrix[9][1]
Which corresponds to the manner in which we originally laid out our table.
How do we calculate these addresses ???How do we calculate these addresses ???
Page 24
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Since we know that each row contains 2 elements (4-Since we know that each row contains 2 elements (4-bytes), and each element requires 2-bytes of storage, bytes), and each element requires 2-bytes of storage, we must (slightly) modify our previous formula:we must (slightly) modify our previous formula:
Element address = base address of the arraybase address of the array+ (row offset offset * bytes per row)+ (column offset offset * bytes per element)
Since we are dealing with an integer array which has the base address 63128 the formula is:
Element address = 6312863128+ (row offset offset * 4)+ (column offset offset * 2)
Page 25
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Row/ColRow/Col[0][0]
63128 & 63129matrix[0][0]
63130 & 63131matrix[0][1]
63132 & 63133matrix[1][0]
63134 & 63135matrix[1][1]
63136 & 63137matrix[2][0]
63138 & 63139matrix[2][1]
63140 & 63141matrix[3][0]
63142 & 63143matrix[3][1]
63144 & 63145matrix[4][0]
63146 & 63147matrix[4][1]
63148 & 63149matrix[5][0]
63150 & 63151matrix[5][1]
63152 & 63153matrix[6][0]
63154 & 63155matrix[6][1]
63156 & 63157matrix[7][0]
63158 & 63159matrix[7][1]
63160 & 63161matrix[8][0]
63162 & 63163matrix[8][1]
63164 & 63165matrix[9][0]
63166 & 63167matrix[9][1]
AddressAddress63128 + 0*4 + 0*2 = 6312863128
[0][1] 63128 + 0*4 + 1*2 = 6313063130[1][0] 63128 + 1*4 + 0*2 = 6313263132[1][1] 63128 + 1*4 + 1*2 = 6313463134[2][0] 63128 + 2*4 + 0*2 = 6313663136[2][1] 63128 + 2*4 + 1*2 = 6313863138[3][0] 63128 + 3*4 + 0*2 = 6314063140[3][1] 63128 + 3*4 + 1*2 = 6314263142[4][0] 63128 + 4*4 + 0*2 = 6314463144[4][1] 63128 + 4*4 + 1*2 = 6314663146
Row/ColRow/Col[5][0]
AddressAddress63128 + 5*4 + 0*2 = 6314863148
[5][1] 63128 + 5*4 + 1*2 = 6315063150[6][0] 63128 + 6*4 + 0*2 = 6315263152[6][1] 63128 + 6*4 + 1*2 = 6315463154[7][0] 63128 + 7*4 + 0*2 = 6315663156[7][1] 63128 + 7*4 + 1*2 = 6315863158[8][0] 63128 + 8*4 + 0*2 = 6316063160[8][1] 63128 + 8*4 + 1*2 = 6316263162[9][0] 63128 + 9*4 + 0*2 = 6316463164[9][1] 63128 + 9*4 + 1*2 = 6316663166
Page 26
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
The formula works regardless of how many The formula works regardless of how many dimensions there are.dimensions there are.
Consider the C declaration: floatfloat multiarray[5][4][3][2];
We are requesting a total of: 4 * (5 * 4 * 3 * 2) = 4 * 120 = 480Contiguous bytes of RAM
Notice that:• Each change in the last (4th) offset requires: 4-bytes• Each change in the 3rd offset requires: 8-bytes• Each change in the 2nd offset requires: 24-
bytes• Each change in the 1st offset requires: 96-
bytesWhich makes sense since the first offset can change 5 times and we know we require a total of 480 (= 5 * 96) bytes
Page 27
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
The General formula, therefore, would be:The General formula, therefore, would be:Element address = base address of the arraybase address of the array
+ (first offset offset * 96)+ (second offset offset * 24)+ (third offset offset * 8)+ (fourth offset offset * 4)
If we found that the base address of our variable multiarray was:2157821578
We could calculate the following (sample) addresses as:Array ElementArray Elementmultiarray[0][0][2][1]
AddressAddress21578 + 0*96 + 0*24 + 2*8 + 1*4 = 21598
multiarray[1][2][0][0] 21578 + 1*96 + 2*24 + 0*8 + 0*4 = 21722 multiarray[1][3][2][1] 21578 + 1*96 + 3*24 + 2*8 + 1*4 = 21915 multiarray[2][1][1][0] 21578 + 2*96 + 1*24 + 1*8 + 0*4 = 21802 multiarray[4][3][2][1] 21578 + 4*96 + 3*24 + 2*8 + 1*4 = 22054
Page 28
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Searching an ArraySearching an Array
Assume that we wished to determine if the number 25 was in our original list (of squared values):
#include#include <stdio.h>void mainvoid main(){ intint vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; intint index; for (index = 0; index < 10 && vector[index] != 25; index++);for (index = 0; index < 10 && vector[index] != 25; index++); if if (index > 9) printf(“The value was not found\n”); elseelse printf(“The value %d was found at offset %d\n”, vector[index], index); }
Let’s Consider this program line by line
Page 29
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
intint vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};Reserve (& initialize) 2 * 10 = 20 bytes at base address vectorintint index;Reserve 2 bytes at base address indexforfor (index = 0;
Set (once) the contents of location index to zero (0)
12350 & 12351
0
12352 & 12353
1
12354 & 12355
4
12356 & 12357
9
12358 & 12359
1612360 & 12361
25
12362 & 12363
36
12364 & 12365
49
12366 & 12367
64
12368 & 12369
81
15234 & 15235
0
Page 30
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
intint vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};Reserve (& initialize) 2 * 10 = 20 bytes at base address vectorintint index;Reserve 2 bytes at base address indexforfor (index = 0;
12350 & 12351
0
12352 & 12353
1
12354 & 12355
4
12356 & 12357
9
12358 & 12359
1612360 & 12361
25
12362 & 12363
36
12364 & 12365
49
12366 & 12367
64
12368 & 12369
81
index < 10index < 10
15234 & 15235
0
True
&& vector[index] != 25;&& vector[index] != 25;
True
Therefore, execute the command
Page 31
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
intint vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};Reserve (& initialize) 2 * 10 = 20 bytes at base address vectorintint index;Reserve 2 bytes at base address indexforfor (index = 0; index < 10index < 10 && vector[index] != 25;&& vector[index] != 25;
What Command ???
• There is none needed in this case• We know that the element we are looking for has
not been found (vector[index] != 25)(vector[index] != 25)• We also know that the list hasn’t been exhausted
(index < 10)(index < 10)• We need only move to the next element
index++);index++);
Page 32
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
intint vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};Reserve (& initialize) 2 * 10 = 20 bytes at base address vectorintint index;Reserve 2 bytes at base address indexforfor (index = 0; index < 10index < 10 && vector[index] != 25;&& vector[index] != 25; index++);index++);
Throughout the loop, the logic would be:
Pass No.
1
(contents of) index
0
index <= 10 ?Yes
Vector[index] != 25 ?
2 1 Yes (= 1) Yes3 2 Yes (= 4) Yes4 (= 9) Yes5 4 Yes (= 16) Yes6 5 Yes (= 25) NO
We are out of the loop
3
(= 0) Yes
Yes
Page 33
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
intint vector[10] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};Reserve (& initialize) 2 * 10 = 20 bytes at base address vectorintint index;Reserve 2 bytes at base address indexforfor (index = 0; index < 10index < 10 && vector[index] != 25;&& vector[index] != 25; index++);index++);
Once out of the loop we need to print our findings:
if if (index > 9) False
elseelse printf(“The value %d was found at offset %d\n”, vector[index], index);
º º º º º º º º º ºº º º º º º º º º º
The value 25 was found at offset 5
Page 34
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Pointers and Arrays:Pointers and Arrays:
Assume that we wished to determine if the number 25 was in our original list (of squared values):
#include#include <stdio.h>void mainvoid main(){ intint vector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}; intint * index; index = vector; whilewhile ((index <= &vector[9]) && (* index != 25)) index++; if if (index > &vector[9]) printf(“The value was not found\n”); elseelse printf(“The value %d was found at address %p\n”, *index, index); }
Let’s Consider this program line by line
Page 35
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
intint vector[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};Reserve (& initialize) 2 * 10 = 20 bytes at base address vector
(assume that vector has the base address 8900)
intint *index; Reserve 4-bytes of storage at location indexIf we go to location index, we would find a signed integer value
(assume that index has the base address 8932)
index = vector;Store the address of vector (i.e., 8900) at location index
whilewhile ((index <= &vector[9]) && (*index != 25))Continue processing as long as:• The contents of index (presently 8900) is less than or equal
to &vector (= 8900 + 9*2 = 8918)ANDAND
• The contents of the address stored at location index (at location 8900 we will presently find the integer 0) is not equal to the integer 25
Page 36
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Inside the loop, there is only one statement:
indexindex++;• Increment the contents of location index• since location index contains an address which points to an
integer, incrementing means increasing the value by 2
Throughout the loop, the logic would be:
Pass No.
1
(contents of) index8900
index <= &vector[9] ?
= 8918
Yes*index
0*index != 25 ?
Yes2 8902 Yes 1 Yes3 8904 Yes 4 Yes4 8906 Yes 9 Yes5 8908 Yes 16 Yes6 8910 Yes 25 NO
We are out of the loop
Page 37
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
When we come out of the loop, there are two possibilities:When we come out of the loop, there are two possibilities:
EITHER: EITHER: index > &vector[9]
Meaning that the contents of location index (which contains an an address) are greater than the address of the last legal array address (i.e., 8918)
In which case we print out: The value was not found
OROR
We print out the value:
The value 25 was found at address 8910 (Assuming we were looking for the value 25)
How could the contents of location index ever be How could the contents of location index ever be greater than the array address 8918 ???greater than the array address 8918 ???
Page 38
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
IfIf we were looking for the value 108 (not on the list):
Pass No.1
(contents of) index8900
index <= &vector[9] ?
= 8918
Yes*index
0*index != 108 ?
Yes2 8902 Yes 1 Yes3 8904 Yes 4 Yes4 8906 Yes 9 Yes5 8908 Yes 16 Yes6 8910 Yes 25 Yes7 8912 Yes 36 Yes8 8914 Yes 49 Yes9 8916 Yes 64 Yes
10 8918 Yes 81 Yes11 8920 NONO Unknown Probably
Not
And we are out of the loop
Page 39
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Array DeclarationsArray DeclarationsAutomatic ArraysAutomatic Arrays• Defined INSIDE a function• Exists ONLY for the duration of the function• NOT initialized• When done, memory allocation freed
intint mainmain() { intint intarray[100], index;
External ArraysExternal Arrays• Known to ALL functions• Do NOT Expire when a particular functions ends• INITIALIZED when declared
intint intarray[100]; intint mainmain() { intint index;
Page 40
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays
Static ArraysStatic Arrays
• Like Automatic Arrays, LOCAL to the function• Like External arrays, RETAIN VALUES between calls• INITIALIZED at declaration
intint mainmain() { staticstatic intint intarray[100]; intint index;
Page 41
Data Structures in C for Non-Computer Science Majors
Kirs and Pflughoeft
Numeric Arrays