Goals: The purpose of this assignment is to test students on basic sorting and s
ID: 3664771 • Letter: G
Question
Goals: The purpose of this assignment is to test students on basic sorting and search algorithms in C++. You can use any sorting and searching algorithm of your choice. You will need knowledge about C++ files and functions.
In this programming assignment, you will design a simple search engine. Realistically, a Search engine allows a user to enter a few keywords, searches all websites in the World Wide Web and presents a list of websites to the user in the order of relevance based on the entered keyword.
In this assignment, you will implement a C++ program that works like a search engine. In this assignment, you will use simple text files as websites. The program should allow the user to enter a keyword to search for. The user can enter ONLY one string as a keyword. Assume that there are exactly 5 files (file1.txt, file2.txt, file3.txt, file4.txt, file5.txt) to search the entered keyword. Also assume that each line in a text file contains EXACTLY one string and each file contains EXACTLY 25 lines. Your search engine should do the following:
1. Search for the entered keyword in all the text files.
2. Find the number of times the entered keyword occurs in each file. Should assign a rank to each textfile that contains the searched keyword.The rank of a textfile is the number of times the entered keyword exists in that file.
3. Finally,display the names of files in descending order of their ranks.Note that you only display the textfiles, which contain the keyword. If multiple files , engine can display those files in any order.
HINT: Here are the key points you need to remember while doing this assignment.
1. Create a function to read contents from a file and stores the contents of the file in a string array.
2. Modify the serach algorithm of the choice so that it returns the number of times the keyword occurs in the string array you are seraching for. Note you are seraching a string and not an integer. You can use this function to get the rank of the file.
3. You can store the ranks of the files in an integer array. Modify sorting algorithm so that it gives you not only the sorted array but also the sorted indices based on the finel sorted array. Use the modified sorted algorithm to obtain the order of the file names in descending oredr of their rank.
Explanation / Answer
//Program
#include <iostream>
#include <fstream>
using namespace std;
/*
Create a function to read contents from a file and stores the contents
of the file in a string array.
*/
string* fileRead(string file)
{
ifstream infile(file.c_str());
if(!infile)
{
cout<<endl<<"Unable to find "<<file;
exit(0);
}
string *content=new string[25];
for(int i=0;i<25;i++)
{
infile>>content[i];
cout<<content[i];
}
return content;
}
/*
function to search keyword in a file and to return rank
*/
int getRank(string content[25],string keyword)
{
int rank=0;
//search the keyword in the content array
for(int j=0;j<25;j++)
{
if(content[j].compare(keyword.c_str())==0)
{
//increment the rank for the each occurence of the keyword
rank++;
}
}
}
int main()
{
/*The program should allow the user to enter a keyword to search for.
The user can enter ONLY one string as a keyword.*/
cout<<"Enter the keyword to search: ";
string keyword;
cin>>keyword;
/*Assume that there are exactly 5 files (file1.txt, file2.txt,
file3.txt, file4.txt, file5.txt) to search the entered keyword.
Also assume that each line in a text file contains EXACTLY
one string and each file contains EXACTLY 25 lines.*/
string filenames[]={"file1.txt","file2.txt","file3.txt","file4.txt","file5.txt"};
int rank[5];
for(int i=0;i<5;i++)
{
//read the file and store its contents in content array
string *content=fileRead(filenames[i]);
//get the rank of each file
rank[i]=getRank(content,keyword);
cout<<rank[i];
}
//sort the filenames in descending order based on the rank
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(rank[i]<rank[j])
{
int temp=rank[i];
rank[i]=rank[j];
rank[j]=temp;
string t=filenames[i];
filenames[i]=filenames[j];
filenames[j]=t;
}
}
}
//Displaying filenames based on their ranks in descending order
cout<<endl<<"List of file names based on their ranks in descending order"<<endl;
for(int i=0;i<5;i++)
{
cout<<rank[i]<<" "<<filenames[i]<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.