SlideShare a Scribd company logo
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 // required header files
#include
#include
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 // required header files
#include
#include
#include
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.

More Related Content

PDF
Module 03 File Handling in C
PDF
Write a program that takes any input text and produces both a frequen.pdf
PPT
file_handling_in_c.ppt
PPTX
Data Structure Using C - FILES
PDF
This program here prints the number of words that occurs in the inpu.pdf
DOCX
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
PDF
C Programming Project
PPT
Unit5 C
Module 03 File Handling in C
Write a program that takes any input text and produces both a frequen.pdf
file_handling_in_c.ppt
Data Structure Using C - FILES
This program here prints the number of words that occurs in the inpu.pdf
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
C Programming Project
Unit5 C

Similar to TffffThe program needs to be in C++ and part 1 of the assignment i.pdf (20)

DOCX
UNIT 4-HEADER FILES IN C
PPTX
PPS-II UNIT-5 PPT.pptx
PDF
So I am writing a CS code for a project and I keep getting cannot .pdf
PPTX
file handling final3333.pptx
PPTX
Introduction to files management systems
PPT
Unit5
PDF
IN C++ languageWrite a simple word processor that will accept text.pdf
PPTX
want to learn files,then just use this ppt to learn
PDF
To write a program that implements the following C++ concepts 1. Dat.pdf
DOCX
source code which create file and write into it
PPTX
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
PPTX
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
DOCX
Write a program that asks the user for the name of a file. The progra.docx
PPTX
Cs1123 10 file operations
TXT
Fileinc
PPT
file.ppt
PPTX
File management
DOCX
Data structures
PPT
file_handling_in_c.ppt
DOCX
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY
UNIT 4-HEADER FILES IN C
PPS-II UNIT-5 PPT.pptx
So I am writing a CS code for a project and I keep getting cannot .pdf
file handling final3333.pptx
Introduction to files management systems
Unit5
IN C++ languageWrite a simple word processor that will accept text.pdf
want to learn files,then just use this ppt to learn
To write a program that implements the following C++ concepts 1. Dat.pdf
source code which create file and write into it
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
Write a program that asks the user for the name of a file. The progra.docx
Cs1123 10 file operations
Fileinc
file.ppt
File management
Data structures
file_handling_in_c.ppt
C UNIT-5 PREPARED BY M V BRAHMANANDA REDDY

More from arri2009av (20)

PDF
Identify five muscles of the head area that have a name that is very .pdf
PDF
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
PDF
From a mixed field, what is easier to facilitate through artificial s.pdf
PDF
Explain how you would tell if something that looks like a leaf (flat.pdf
PDF
Explain what a standard deviation value measures in quantitative dat.pdf
PDF
Einstein, in his famous photoelectric effect experiment demonstr.pdf
PDF
Contrast autochthonous and allochthonous food webs. Which type would.pdf
PDF
Based on the below and using the 12 categories of threats identify 3 .pdf
PDF
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
PDF
An attack in which an authentic-looking e-mail or website entices a .pdf
PDF
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
PDF
A vague appointment Four people make an appointment to meet each ot.pdf
PDF
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
PDF
An enzyme aggase requires 16 units of activity for wild type functio.pdf
PDF
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
PDF
1. Match the decription listed with the corresponding structureA. .pdf
PDF
1.) What are some factors that should be taken into account when est.pdf
PDF
Write a program that asks the user for the name of a file. The progr.pdf
PDF
Write a program that obtains the execution time of selection sort, bu.pdf
PDF
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Identify five muscles of the head area that have a name that is very .pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
From a mixed field, what is easier to facilitate through artificial s.pdf
Explain how you would tell if something that looks like a leaf (flat.pdf
Explain what a standard deviation value measures in quantitative dat.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdf
Based on the below and using the 12 categories of threats identify 3 .pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
An attack in which an authentic-looking e-mail or website entices a .pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
A vague appointment Four people make an appointment to meet each ot.pdf
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
1. Match the decription listed with the corresponding structureA. .pdf
1.) What are some factors that should be taken into account when est.pdf
Write a program that asks the user for the name of a file. The progr.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf

Recently uploaded (20)

PDF
Trump Administration's workforce development strategy
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Lesson notes of climatology university.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Trump Administration's workforce development strategy
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Classroom Observation Tools for Teachers
Final Presentation General Medicine 03-08-2024.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
History, Philosophy and sociology of education (1).pptx
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Weekly quiz Compilation Jan -July 25.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
What if we spent less time fighting change, and more time building what’s rig...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Lesson notes of climatology university.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
01-Introduction-to-Information-Management.pdf
Cell Types and Its function , kingdom of life
Updated Idioms and Phrasal Verbs in English subject
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

TffffThe program needs to be in C++ and part 1 of the assignment i.pdf

  • 1. 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 // required header files #include #include 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
  • 2. } 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 // required header files #include #include #include 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";
  • 3. 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
  • 4. 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
  • 5. 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%
  • 6. 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.