This program reads a number flle and display the number occurrence statistics. Enter an existing file name: numbers1.txt Enter the highest number to get occurrence count (must be > 0): 15 Number Occurrences 1 1 2 3 1 1 4 1 5 6 e e e 7 9 10 e 1 11 e e 12 13 14 15 There are 5 numbers in the file. The total of occurrence counts for numbers in the range [1,15] is 5. The number 1 has the highest occurrence. The number 5 has the lowest occurrence. e Process returned e (exe) Press any key to continue. execution time : 14.889 s
This program reads a number flle and display the number occurrence statistics. Enter an existing file name: numbers1.txt Enter the highest number to get occurrence count (must be > 0): 15 Number Occurrences 1 1 2 3 1 1 4 1 5 6 e e e 7 9 10 e 1 11 e e 12 13 14 15 There are 5 numbers in the file. The total of occurrence counts for numbers in the range [1,15] is 5. The number 1 has the highest occurrence. The number 5 has the lowest occurrence. e Process returned e (exe) Press any key to continue. execution time : 14.889 s
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
I have the code but dont have the correct outputs.
numbers1.txt has the following contents
Numbers
10 1 2 3 4
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// function prototypes
int readNumbersAndTallyOccurrences(int[], int, ifstream &);
void printCounts(int[], int);
void findMostAndLeastOccurrence(int[], int, int &, int &);
int findTotalOccurrenceCount(int[], int);
int main()
{
cout<<"This program read a number file and display the number occurrence statistics."<<endl;
// for storing filename
stringfilename;
cout<<"Enter an existing file name: ";
getline(cin,filename);
ifstreaminfile;
infile.open(filename);
if(infile.fail())
{
cout<<"File "<<filename<<" not found"<<endl;
return1;
}
intinputMax;
w
cout<<"Enter the highest number to get occurrence count (must be > 0): ";
cin>>inputMax;
// input validation
while(inputMax<1)
{
cout<<"Number must not be less than 0!"<<endl;
cout<<"Enter the highest number to get occurrence count (must be > 0): ";
cin>>inputMax;
}
intoccurrences[inputMax];
for(inti=0;i<inputMax;i++)
occurrences[i]=0;
intnums=readNumbersAndTallyOccurrences(occurrences,inputMax,infile);
infile.close();
if(nums==0)
{
cout<<"File does not have any numbers."<<endl;
exit(1);
}
printCounts(occurrences,inputMax);
inttotal=findTotalOccurrenceCount(occurrences,inputMax);
// print number total numbers in the file
cout<<"There are "<<nums<<" numbers in the file."<<endl;
// print the total occurrence count
cout<<"The total of occurrence counts for numbers in the range [1,"
<<inputMax<<"] is "<<total<<"."<<endl;
intmost,least;
findMostAndLeastOccurrence(occurrences,inputMax,most,least);
cout<<"The number "<<(most+1)<<" has the highest occurrence."<<endl;
cout<<"The number "<<(least+1)<<" has the lowest occurrence."<<endl;
return0;
}
int readNumbersAndTallyOccurrences(int occurrences[], int size, ifstream &infile)
{
stringfirstLineStr;
// ignore the first line of the file
getline(infile,firstLineStr);
intnumber;
intcountNums=0;
stringline;
while(getline(infile,line))
{
// check if it is valid number
if(atoi(line.c_str()))
{
number=atoi(line.c_str());
}
else
{
continue;
}
if(number<=size)
{
occurrences[number-1]+=1;// number - 1 will give index for this number
}
countNums++;
}
returncountNums;
}
void printCounts(int occurrences[], int size)
{
cout<<right<<setw(8)<<"Number"<<right<<setw(14)<<"Occurrences"<<endl;
for(inti=0;i<size;i++)
{
cout<<right<<setw(5)<<(i+1)<<right<<setw(14)<<occurrences[i]<<endl;
}
}
void findMostAndLeastOccurrence(int occurrences[], int size, int &highestCountIndex, int &lowestCountIndex)
{
highestCountIndex=0;
lowestCountIndex=0;
intmost=occurrences[highestCountIndex];
intleast=occurrences[lowestCountIndex];
for(inti=0;i<size;i++)
{
// check for maximum frequency
if(occurrences[i]>most)
{
highestCountIndex=i;
most=occurrences[highestCountIndex];
}
if(occurrences[i]<least)
{
lowestCountIndex=i;
least=occurrences[lowestCountIndex];
}
}
}
int findTotalOccurrenceCount(int occurrences[], int size)
{
inttotal=0;
for(inti=0;i<size;i++)
{
total+=occurrences[i];
}
returntotal;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education