Search code examples
c++arraysparallel-arrays

C++ Is parallel array a defined structure


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.


Solution

  • 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.