Question

In this question you have to write a complete function. in C++ MyMedia Publishers uses two...

In this question you have to write a complete function. in C++

MyMedia Publishers uses two parallel arrays to keep track of the number of subscriptions for each of their
50 publications.   
Array  publications holds 1)  the names of the  magazines and newspapers  published
and  array  subscriptions 2)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);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//I changed contsnt number NUM_PUBS to 5 and tested with 5 data in the file, You can change this to 50 and test with 50 publications and subscriptions

#include <iostream>
#include<string>
#include<fstream>
using namespace std;

//function declarations
void findMostSubs(string publications[], int subscriptions[],
   string &mostSubscriptions, int &nrMostSubscriptions);
//a declaration of a global constant:
const int NUM_PUBS = 5; //number of publications //change this number to 50 when you have 50 publications and 50 subscription data in file, to test I changed this to 5 as I have 5 data
int main() {

   //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
                           //assign some values to publications, as well subscriptions,read from file
   ifstream in1("publications.txt");
   ifstream in2("subscriptions.txt");
   //check if files can be open
   if (!in1 || !in2)
   {
       cout << "Not able to open either or both of the input files, please check" << endl;
       return -1;
   }
   int count = 0;
   //read info into two paralle arrays
   while (!in1.eof())
   {
       getline(in1, publications[count++]);
   }
   count = 0;
   while (!in2.eof())
   {
       in2 >> subscriptions[count++];
   }
   // 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);
   cout << mostSubscriptions << " publication has " << nrMostSubscriptions << " subscriptions"<<endl;
}

//function definition
void findMostSubs(string publications[], int subscriptions[],
   string &mostSubscriptions, int &nrMostSubscriptions)
{
   nrMostSubscriptions = subscriptions[0];
   for (int i = 0; i<NUM_PUBS; i++)
   {
       if (nrMostSubscriptions < subscriptions[i])
       {
           mostSubscriptions = publications[i];
           nrMostSubscriptions = subscriptions[i];
       }

   }
}

/*Input files with 5 data
publications.txt
Impartial Reporter
Unterrified Democrat
E S Holmans, London
The Arran Banner
Chad Gething, London
scubscriptions.txt
20
10
17
20
28
//tested with 5 data in each file , you can change constant variable declared NUM_PUBS to 50 when testing with large data
Output
Chad Gething, London publication has 28 subscriptions
*/

Add a comment
Know the answer?
Add Answer to:
In this question you have to write a complete function. in C++ MyMedia Publishers uses two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this question you have to write a complete function. MyMedia Publishers uses two parallel arrays...

    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 findMost Subs to determine which publication has the most subscribers. Function findMost Subs has to return the...

  • QUESTION 3 13 marks In this question you have to write a complete function. MyMedia Publishers...

    QUESTION 3 13 marks 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 findMost Subs to determine which publication has the most subscribers. Function findMost Subs...

  • QUESTION 3 13 marks In this question you have to write a complete function. MyMedia Publishers...

    QUESTION 3 13 marks 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 fi ndMost Subs to determine which publication has the most subscribers. Function findMbst...

  • Define an ordinary function called DisplayMin that has three parameters: the first two parameters are parallel...

    Define an ordinary function called DisplayMin that has three parameters: the first two parameters are parallel arrays of different types and the third argument is of type size_t representing the size of both arrays. The function DisplayMin must work when called with various types of actual arguments, for example string DisplayMin(string names[], int calories[], int size) or int DisplayMin(int calories[], float prices[], int size). (Parallel Arrays are two arrays whose data is related by a common index. For example: with...

  • In C++ format: Define a function called DisplayMax. It will have 3 parameters of two different...

    In C++ format: Define a function called DisplayMax. It will have 3 parameters of two different types of known parameter lists which are either void DisplayMax(string idI, double value[], int size) or void DisplayMax(int idn, short value几int size). The first and second parameters are parallel arrays. (Parallel Arrays are two arrays whose data is related by a common index. For example: with the string array and double array, index 5 of the string array would have some name, and index...

  • This project will allow you to practice with one dimensional arrays, function and the same time...

    This project will allow you to practice with one dimensional arrays, function and the same time review the old material. You must submit it on Blackboard and also demonstrate a run to your instructor. General Description: The National Basketball Association (NBA) needs a program to calculate the wins-losses percentages of the teams. Write a complete C++ program including comments to do the following: Create the following: •a string array that holds the names of the teams •an integer array that...

  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT