C++ Is parallel array a defined structure

758 Views Asked by At

Simple question:

For my assignment I am asked to count the words in a file and keep track of their frequency. I am to create a parallel int array for the frequency.

Is a parallel array a special data structure, or does it simply mean I am creating 2 arrays, where one is dependent on the other. For example, I create 2 dynamic arrays and update both inside the loop with respect to my i variable from the for loop.

3

There are 3 best solutions below

2
On BEST ANSWER

A parallel array is basically what you posit in your question. It's two distinct arrays connected by the index.

For example, a parallel array counting frequencies of temperatures may be:

int    tempVal  [100];
size_t tempCount[100];

and the temperature value at index 42 has a frequency given by tempCount[42].

Purists will argue (and they do have a point) that it's better to provide a single array of a structure such as:

typedef struct {
    int    val;
    size_t count;
} tFreq;
tFreq tempFreq[100];

and C++ has collections that will do this for you, such as std::pair. But, if your assignment specifically calls for parallel arrays, I suspect std::pair would not be considered thus.

1
On

There isn't a parallel array data structure as such.

You can create two arrays and address them in parallel.

There are some alternatives, such as creating an array of std::pair, or (probably the "right" one for the task at hand) an std::unordered_map (or possibly an std::map instead).

1
On

No structure is special, it's always composed of primitives.