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
icon
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;
}
The program reads data from one of the files given here. The first line of each file can contain any string (may
have multiple spaces) that should be read but not used. Following the string are numbers whose values are
equalled to or greater than 1.
There should be no assumption of how many numbers are in the file. There should be no assumption of the
largest number in the file.
While reading each number from the input file, the program should keep track of (tally) the number of occurrences
(frequency counts) of each number whose value is 1 up to certain value inputMax (entered by user) and program
stores the frequency counts in an array of size inputMax. The program reads all numbers in a file but it does not
tally the occurrence of numbers that are larger than inputMax. The frequency (occurrence count) of a number is
the count of how many times the number occurs. For example, if use input inputMax is 20, the program keeps
track of the occurrence counts for the number 1, 2, 3, 4,.20, and it ignores the numbers that are larger than 20.
The program should NOT store the numbers it reads from the file because there is no need to do it and there are
too many numbers (millions of numbers). All it needs to store are the occurrence counts of numbers from 1 to
inputMax. For example, if user input inputMax is 20, then the occurrence count array size is 20.
int readNumbersAndTallyOccurrences(occurrences, size, infile);
Parameters:
occurrences: array of integers
size: array size
infile: input file (ifstream object), passed by reference.
The input file is opened in main function, then passed to this function.
Function to read all the numbers in the input file using a loop
It keeps track of the occurrence of each number if it is < or equal to size in the array parameter
Code reads but do no process numbers that are greater than the size. For example, if user enters 20 as
the maxInput, the code reads but does not use numbers that are > 20.
Function does not print anything (main function and printCounts function does the printing)
Function returns the count of all numbers in the file.
void printCounts(occurrences, size);
Parameters:
occurrences: array of integers
size: array size
Print the values of the occurrence counts in the array using a loop
void findMostAndLeastOccurrence(occurrences, size, highestCountindex, lowestCountindex);
Parameters:
occurrences: array of integers
size: array size
highestCountindex: subscript of array element with the highest occurrence count, passed by reference
lowestCountindex: subscript of array element with the lowest occurrence count, passed by reference
This function find the number with the most and the number with the least occurrence in the array
It does not print anything (no "cout" in here, main function and printCounts function does the printing)
Transcribed Image Text:The program reads data from one of the files given here. The first line of each file can contain any string (may have multiple spaces) that should be read but not used. Following the string are numbers whose values are equalled to or greater than 1. There should be no assumption of how many numbers are in the file. There should be no assumption of the largest number in the file. While reading each number from the input file, the program should keep track of (tally) the number of occurrences (frequency counts) of each number whose value is 1 up to certain value inputMax (entered by user) and program stores the frequency counts in an array of size inputMax. The program reads all numbers in a file but it does not tally the occurrence of numbers that are larger than inputMax. The frequency (occurrence count) of a number is the count of how many times the number occurs. For example, if use input inputMax is 20, the program keeps track of the occurrence counts for the number 1, 2, 3, 4,.20, and it ignores the numbers that are larger than 20. The program should NOT store the numbers it reads from the file because there is no need to do it and there are too many numbers (millions of numbers). All it needs to store are the occurrence counts of numbers from 1 to inputMax. For example, if user input inputMax is 20, then the occurrence count array size is 20. int readNumbersAndTallyOccurrences(occurrences, size, infile); Parameters: occurrences: array of integers size: array size infile: input file (ifstream object), passed by reference. The input file is opened in main function, then passed to this function. Function to read all the numbers in the input file using a loop It keeps track of the occurrence of each number if it is < or equal to size in the array parameter Code reads but do no process numbers that are greater than the size. For example, if user enters 20 as the maxInput, the code reads but does not use numbers that are > 20. Function does not print anything (main function and printCounts function does the printing) Function returns the count of all numbers in the file. void printCounts(occurrences, size); Parameters: occurrences: array of integers size: array size Print the values of the occurrence counts in the array using a loop void findMostAndLeastOccurrence(occurrences, size, highestCountindex, lowestCountindex); Parameters: occurrences: array of integers size: array size highestCountindex: subscript of array element with the highest occurrence count, passed by reference lowestCountindex: subscript of array element with the lowest occurrence count, passed by reference This function find the number with the most and the number with the least occurrence in the array It does not print anything (no "cout" in here, main function and printCounts function does the printing)
For numbers1.txt input file with maxlnput 15
This program reads a number file 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
1
3
1
1
7
8
9
10
1
11
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.
Process returned e (exe)
Press any key to continue.
execution time : 14.889 s
Transcribed Image Text:For numbers1.txt input file with maxlnput 15 This program reads a number file 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 1 3 1 1 7 8 9 10 1 11 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. Process returned e (exe) Press any key to continue. execution time : 14.889 s
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
File Input and Output Operations
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.
Similar questions
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education