Using parallel arrays with void functions

Good day

I do not enjoy asking for help in this way but i am really stuck. This question is in an old exam paper i am going through and i have no idea how to go about doing it. My book does not cover anything close to this kind of question and despite trying to research ways i could complete it i am coming up empty. If anyone is willing to help me it would be very much appreciated. i do understand that i have no code of my own but as i said i am completely stumped and am stressing over the question.

Thanks

In this question you have to write a complete function.

MyMedia Publishers uses two parallel arrays to keep track of the number of subscriptions for each of their 50 publications. Array publications holds the names of the magazines and newspapers published and array subscriptions holds the number of subscriptions for each corresponding magazine or newspaper. You have to write a void function, called findMostSubs to determine which publication has the most subscribers. Function findMostSubs has to return the name of the publication as well as the number of subscribers to that publication.

Assume the following:

• a declaration of a global constant: const int NUM_PUBS = 50; //number of publications

• four declaration statements in the main function:

string publications[NUM_PUBS]; //titles of the publications

int subscriptions [NUM_PUBS]; //number of subscriptions for the corresponding publications

int nrMostSubscriptions; //number of subscriptions for publication with most subscriptions

string mostSubscriptions; //title of publication with most subscriptions

• values have been assigned already to all the elements of the arrays

• the function is called in the main program as follows:

findMostSubs (publications, subscriptions, mostSubscriptions, nrMostSubscriptions);

Write down ONLY the complete function findMostSubs.
Last edited on
looks like you just need find the max of subscriptions and return that value, and keep its index, and get the string from publications at that index.

that is, if the 3rd publication is the most, you might see
subscriptions[3] = 10000
and
publications[3] = "some mag"

therefore the index 3 ties the 2 tables together and some mag has 10000 subscribers.
so as you search, you need to track the biggest number AND the index and update both, and once you found the max, use the index to get the correct publication name.

Does that explain it?

this is a poor design. Normally one would put the 2 items together in a struct or class and keep 1 vector or array. It is very rare to need 2 containers that have related info because that design causes problems (forget to update all the containers, etc). There are a few places where this is useful but in general it is not the correct approach.


Topic archived. No new replies allowed.