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

This document demonstrates the use of parallel arrays to store and retrieve data using an example of an array of item numbers and their corresponding prices. The document also includes a function to search for the index of a target item number and then uses that index to retrieve the price. This is a useful technique for organizing and accessing data in a program.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-o2t
koofers-user-o2t 🇺🇸

5

(1)

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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");