1

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 2
    No way to get your homework done "the easy way" here!! – πάντα ῥεῖ Jan 22 '14 at 00:47
  • I'm not sure a `for` loop is appropriate here. – Beta Jan 22 '14 at 00:49
  • 2
    This is not me asking to do homework, this is me asking for clarification on whether my definition of parallel array is correct or not. Please read the whole question before jumping to conclusions. –  Jan 22 '14 at 00:50
  • 1
    The question seems reasonable to me. He is not asking for code, only an explanation of "parallel array". – Beta Jan 22 '14 at 00:50
  • I'm not sure code is needed here. He's not asking us to check (or write) his code, he's asking what the definition of "parallel array" is. – paxdiablo Jan 22 '14 at 00:57
  • 1
    @UliKöhler Sorry, but I do not feel like my question needed code added in. I'm asking for an explanation of what one may take as a parallel array. Not how to construct a parallel array code or anything of that sort. The last thing I want to do is ask for answers. I am attending a university, not a high school. The questions posed are much more than Q&A, they are to actually create from scratch. –  Jan 22 '14 at 00:58
  • @Brandon Fine. I'm still not sure about it but your explanation seems reasonable to me, so I deleted my previous comments. – Uli Köhler Jan 22 '14 at 01:00
  • 1
    @UliKöhler No problem. The last thing I want is for people to think I am cheating. I will make my post more concise next time. That may resolve any confusions in the future. Have a good day :-) –  Jan 22 '14 at 01:02
  • @Brandon Thanks, you make me happy ;-) It's just your question seems like "here's my [homework] problem, now go deal with it". I assume it would seem less so if you had included code like "This is what I (so far) think a parallel array would look like: int tempVal[100]...". – Uli Köhler Jan 22 '14 at 01:06
  • 1
    @UliKöhler Fair enough. I only brought in my vague assignment spec so that it would assist in where my misunderstanding is. I will rephrase questions better! Thanks, again. –  Jan 22 '14 at 01:10
  • @Brandon Sorry for my (nearby) misconceptions here, it simply sounds like typical homework as you're question is currently layed out. Though, in my opinion you'll need to show some more examples in code, to make clear what you've been trying and where you're failing in particular! – πάντα ῥεῖ Jan 22 '14 at 01:21
  • @πάνταῥεῖ Oh, I understand. There was no failure here. I just wanted a clear definition on parallel arrays. –  Jan 22 '14 at 01:31

3 Answers3

3

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

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
3

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks, paxdiablo. I will think over this and see what is more applicable. –  Jan 22 '14 at 01:05
  • @Brandon, _ask_ your educator. They're not monsters (well, some of them are but they're a minority). Or, if you can't do that, do it with the parallel arrays but put a comment in (the assignment or code) stating that a structure array or `std::pair` would probably be more appropriate. You may get extra marks for that :-) – paxdiablo Jan 22 '14 at 01:15
0

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

hdante
  • 7,685
  • 3
  • 31
  • 36
  • Sorry for my poor word choice. I understand that from Java, but my wording is not acceptable. I will make sure to do a better job next time. –  Jan 22 '14 at 01:04