Docsity

Using Parallel Arrays to Store and Retrieve Data - Prof. Marjory Baruch, Study notes of Computer Science

Toggle sidebar
bg1
Parallel arrays
int itemNumber[10]
[0]
1234
[1]
6529
[2]
9983
[3]
4567
[4]
2020
[5]
9987
[6]
1212
[7]
3948
[8]
0098
[9]
8876
double price[10]
[0]
386.32
[1]
0.12
[2]
12.34
[3]
99.99
[4]
99.99
[5]
23.99
[6]
0.99
[7]
386.32
[8]
8.45
[9]
0.13
Suppose we want to find the price of item 2020.
Given the function:
int search( int theArray[ ], int arrSize,
int target )
/* search theArray (of size arrSize) for the
first occurrence of target.
Return the index if it is there.
Return -1 if it does not occur in
theArray */
We can use it:
int location;
location = search(itemNumber, 10, 2020);
if (location != -1)
printf ("It costs $%0.2lf.\n",
price[location]);
else
printf("We do not have that item..\n");

Partial preview of the text

Download Using Parallel Arrays to Store and Retrieve Data - Prof. Marjory Baruch and more Study notes Computer Science in PDF only on Docsity!

Parallel arrays int itemNumber[10] [0] 1234 [1] 6529 [2] 9983 [3] 4567 [4] 2020 [5] 9987 [6] 1212 [7] 3948 [8] 0098 [9] 8876 double price[10] [0] 386. [1] 0. [2] 12. [3] 99. [4] 99. [5] 23. [6] 0. [7] 386. [8] 8. [9] 0. Suppose we want to find the price of item 2020. Given the function: int search( int theArray[ ], int arrSize, int target ) /* search theArray (of size arrSize) for the first occurrence of target. Return the index if it is there. Return - 1 if it does not occur in theArray */ We can use it: int location; location = search(itemNumber, 10, 2020); if (location != - 1) printf ("It costs $%0.2lf.\n", price[location]); else printf("We do not have that item..\n");