Question

In: Computer Science

Write a C++ program that will display the top internet stories from the stories.txt file The...

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

Solutions

Expert Solution

#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


Related Solutions

Need to create a program in C++ that can display/write into a file called marks.txt. I'm...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment. Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start...
Write a C++ program to display toy name
Write a C++ program to display toy name
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT