In: Computer Science
Write a C++ program that will display the top internet stories from the stories.txt file
The program must display the stories that have a score which the statistical "mode". The "mode" of a set of values is the value that occurs most often (with the greatest frequency)
The data about the stories is in a file that contains the following:
storyTitle (a sequence of numbers and/or letters, may contain spaces in it)
storyURL (a regular URL, like http://www.twitter.com/story1,without spaces)
score (an integer number, greater than zero)
If the data has no mode (i.e none of the 'score' values occur more than once), then the program must display the first five stories read from the file (or all of them if there are less than five). Is there is a mode, notice that you won't necessarily know how many stories have the mode.
The output should look like:
<“Mode: ”, mode | “No mode was found.”>
Story 1 title, Story 1 URL
Story 2 title, Story 2 URL
etc.
The program should first read the data from a text file named “stories.txt” into an array of structures. This file contains first the number of stories contained in the file, and then the data items for each story, in the order listed above, with each data item on a separate line. See the sample input file in the assignment announcement.
The program should allocate memory for the array to store the numberof stories in the input file. You must use dynamic memory allocationto achieve this feature. Demonstrate your pointer prowess by using pointer notationinsteadof array notation in your program.
To find the mode in the data, write a function that accepts as arguments:
·the array of structures,
·the size of the array.
This function should find the mode of the data (using each story’s score) and return it. The modeis the value the function should return. If the array has no mode, the function should return -1.
this is the text file
8 The first cannabis cafe in the United States opens :cnn.com/travel/article/cannabis-cafe-lowell-farms-trnd/index.html 55 Jimmy Carter, the oldest living former U.S. president, is 95 today ://www./2019/10/01/us/jimmy-carter-95-birthday-trnd/index.html 13 British couple spends $11,800 on Airbnb rental in Ibiza that doesn't exist ://www./travel/article/airbnb-ibiza-spain-penthouse-scam-trnd/index.html 95 Jurors can consider the so-called castle doctrine in the murder trial of ex-police officer Amber Guyger :/.com/2019/10/01/us/amber-guyger-trial-castle-doctrine/index.html 55 Houston Astros shortstop Carlos Correa donates $10,000 to the family of slain Houston Sikh deputy ://.com/2019/10/01/sport/carlos-correa-visits-family-of-slain-texas-sikh-deputy/index.html 55 R. Kelly's lawyer is complaining that the singer can visit with only one of his girlfriends at a time :/.cnn./2019/10/01/us/r-kelly-girlfriends-jail-visits/index.html 34 NFL suspends Vontaze Burfict for the rest of the season ://cnn./2019/09/30/sport/nfl-suspends-vontaze-burfict-for-rest-of-the-season-spt-intl/index.html 95 Ford is handing control of its India business to Mahindra :cnn2019/10/01/business/ford-india-mahindra-joint-venture/index.html 55
#include<bits/stdc++.h>
using namespace std;
class Story {
private:
string storyTitle;
string storyURL;
int score;
public:
Story(){
}
Story* setTitle(char *sTitle)
{
storyTitle =
sTitle;
return
this;
}
Story* setURL(char *sURL)
{
storyURL =
sURL;
return
this;
}
Story* setScore(int sc)
{
score =
sc;
return
this;
}
void print()
{
cout<<storyTitle<<",
"<<storyURL<<endl;
}
int getScore()
{
return
score;
}
};
int findMode( Story *A, int n)
{
map<int,int> m;
for(int i=0; i<n; i++)
{
if(m.find(A[i].getScore())!=m.end())
return
A[i].getScore();
m[A[i].getScore()] = 1;
}
return -1;
}
int main()
{
FILE* file = fopen ("./story.txt", "r"); // file
int n = 0;
fscanf (file, "%d", &n); // scan size of
array;
Story *A = new Story[n];
char sTitle[5000], sURL[5000];
int sc;
for(int i = 0; i < 2*n; i++)
{
fscanf (file, "%[^\n]%*c", sTitle);
// scan title
fscanf (file, "%[^\n]%*c", sURL);
// scan url
fscanf (file, "%d", &sc); //
scan score
if(i%2!=0)
{
A[i/2].setTitle(sTitle)->setURL(sURL)->setScore(sc);
}
}
int tempScore = findMode(A, n);
if(tempScore != -1 )
{
for(int i=0; i<n; i++)
{
if(A[i].getScore() == tempScore)
A[i].print();
}
}
else
{
int tempSize = 5;
if(tempSize > n)
tempSize =
n;
for(int i=0; i<tempSize;
i++)
A[i].print();
}
}
// use filename as story.txt and code file and txt file should be in one folder or use correct path of the file