Tffff The program needs to be in C and part 1 of the assignm
Tffff
The program needs to be in C++ and part 1 of the assignment is already done. The only difference is that the appropriate text files that will be used for this program... some of them are large txt files (chapter of a book for example to almost a complete book).
Part 2 is the Question!!!
The code for part one is shown below:
#include <iostream> // required header files
#include <string>
#include <fstream>
using namespace std;
int main() // driver method
{
string myArray[100]; // array to store the data
string filename; //required initialisations
ifstream file;
int count = 0;
cout << \"Please enter the input file name: \"; // prompt for the user to enter the filename
cin >> filename;
filename = filename+\".txt\";
file.open( filename.c_str() ); // opening the filename
if(file.is_open()) // checking for the opening of the file
{
while ( !file.eof() ){
count++; // incrementing the count for the lines
for(int x = 0; x < count; x++){
file >> myArray[x]; // saving the data to the array
cout << \"The Array Data is : \" << myArray[x] << endl; // printing the output to console
}
}
file.close(); // closing the file
} else {
cout << \"Error Opening the File!!\" << endl; // catching the exception
}
cout << \"\ The total Words in the file are : \" << count << endl; // getting the count from the lines
return 0;
}
OUTPUT :
Please enter the input file name: input
The Array Data is : John
The Array Data is : Carter
The Array Data is : Harry
The Array Data is : Mornie
The Array Data is : Michael
The Array Data is : Mary
The Array Data is : Newton
The total Words in the file are : 7
Now from this code, how can we adjust it so part 2 from the assignment fits in.
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main() // driver method
{
string myArray[100]; // array to store the data
string filename, line; //required initialisations
ifstream file;
int count = 0, index = 0, occurrence = 0, foundOcc = 0;
cout << \"Please enter the input file name: \"; // prompt for the user to enter the filename
cin >> filename;
filename = filename+\".txt\";
file.open( filename.c_str() ); // opening the filename
if(file.is_open()) // checking for the opening of the file
{
while ( !file.eof() ){
count++; // incrementing the count for the lines
for(int x = count; x <= count; x++){
index = index + 1;
file >> myArray[x]; // saving the data to the array
cout << \"The Data is : \" << myArray[x] << \" found at index value : \" << index << endl; // printing the output to console
}
}
file.close(); // closing the file
} else {
cout << \"Error Opening the File!!\" << endl; // catching the exception
}
for (int i = 1; i <= count; i++) {
// checking to the data in the array if it was used before
int found = 0;
for (int j = 0; j < i; j++) {
if (myArray[i] == myArray[j]) {
found++;
}
}
// continuing if it\'s the first occurrence
if (found == 0) {
// starting the count with initial value
occurrence = 1;
// checking the data in the array for other occurances
for (int j = i + 1; j < 8; j++) {
if (myArray[i] == myArray[j]) {
occurrence++;
}
}
cout << \"\ Occurrences of the Word \" << myArray[i] << \" is : \"; // printing the number of occurances
cout << myArray[i] << \" : \" << occurrence << endl; // printing the data of the array
float percent = (float)(occurrence*100)/count; // calculating the percent of the occurances
cout << \"Percentage of Occurrence of the Word \" << myArray[i] << \" is : \" << percent << \"%\" << endl; // printing the data and the percent of occurrence
if(occurrence > 1){
foundOcc++; // calculating the total occurrences in the file
}
}
}
cout << \"\ Number of Occurrences Found in the data read are : \" << foundOcc << endl; // printing the results
cout << \"\ The total Words in the file are : \" << count << endl; // getting the count from the lines
return 0;
}
input.txt :
John
Miller
Carter
Penny
Carter
Bill
Harry
Shawn
Perk
Cameroon
Bush
Moore
Mary
Marxter
OUTPUT :
Please enter the input file name: input
The Data is : John found at index value : 1
The Data is : Miller found at index value : 2
The Data is : Carter found at index value : 3
The Data is : Penny found at index value : 4
The Data is : Carter found at index value : 5
The Data is : Bill found at index value : 6
The Data is : Harry found at index value : 7
The Data is : Shawn found at index value : 8
The Data is : Perk found at index value : 9
The Data is : Cameroon found at index value : 10
The Data is : Bush found at index value : 11
The Data is : Moore found at index value : 12
The Data is : Mary found at index value : 13
The Data is : Marxter found at index value : 14
Occurrences of the Word John is : John : 1
Percentage of Occurrence of the Word John is : 7.14286%
Occurrences of the Word Miller is : Miller : 1
Percentage of Occurrence of the Word Miller is : 7.14286%
Occurrences of the Word Carter is : Carter : 2
Percentage of Occurrence of the Word Carter is : 14.2857%
Occurrences of the Word Penny is : Penny : 1
Percentage of Occurrence of the Word Penny is : 7.14286%
Occurrences of the Word Bill is : Bill : 1
Percentage of Occurrence of the Word Bill is : 7.14286%
Occurrences of the Word Harry is : Harry : 1
Percentage of Occurrence of the Word Harry is : 7.14286%
Occurrences of the Word Shawn is : Shawn : 1
Percentage of Occurrence of the Word Shawn is : 7.14286%
Occurrences of the Word Perk is : Perk : 1
Percentage of Occurrence of the Word Perk is : 7.14286%
Occurrences of the Word Cameroon is : Cameroon : 1
Percentage of Occurrence of the Word Cameroon is : 7.14286%
Occurrences of the Word Bush is : Bush : 1
Percentage of Occurrence of the Word Bush is : 7.14286%
Occurrences of the Word Moore is : Moore : 1
Percentage of Occurrence of the Word Moore is : 7.14286%
Occurrences of the Word Mary is : Mary : 1
Percentage of Occurrence of the Word Mary is : 7.14286%
Occurrences of the Word Marxter is : Marxter : 1
Percentage of Occurrence of the Word Marxter is : 7.14286%
Number of Occurrences Found in the data read are : 1
The total Words in the file are : 14
Hope this is helpful.



