Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
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 nowThis is a popular solution!
Step by stepSolved 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.Similar questions
- C++ Code dynamicarray.h and dynamicarray.cpparrow_forward# dates and times with lubridateinstall.packages("nycflights13") library(tidyverse)library(lubridate)library(nycflights13) Qustion: Create a function called date_quarter that accepts any vector of dates as its input and then returns the corresponding quarter for each date Examples: “2019-01-01” should return “Q1” “2011-05-23” should return “Q2” “1978-09-30” should return “Q3” Etc. Use the flight's data set from the nycflights13 package to test your function by creating a new column called quarter using mutate()arrow_forwardCreate a flowchart for this program in c++, #include <iostream>#include <vector> // for vectors#include <algorithm>#include <cmath> // math for function like pow ,sin, log#include <numeric>using std::vector;using namespace std;int main(){ vector <float> x, y;//vector x for x and y for y float x_tmp = -2.5; // initial value of x float my_function(float x); while (x_tmp <= 2.5) // the last value of x { x.push_back(x_tmp); y.push_back(my_function(x_tmp)); // calculate function's value for given x x_tmp += 1;// add step } cout << "my name's khaled , my variant is 21 ," << " my function is y = 0.05 * x^3 + 6sin(3x) + 4 " << endl; cout << "x\t"; for (auto x_tmp1 : x) cout << '\t' << x_tmp1;//printing x values with tops cout << endl; cout << "y\t"; for (auto y_tmp1 : y) cout << '\t' << y_tmp1;//printing y values with tops…arrow_forward
- struct employee{int ID;char name[30];int age;float salary;}; (A) Using the given structure, Help me with a C program that asks for ten employees’ name, ID, age and salary from the user. Then, it writes the data in a file named out.txt (B) For the same structure, read the contents of the file out.txt and print the name of the highest salaried employee and youngest employee names name in the outputscreen.arrow_forwardFunction 3: Positive Unique Numbers _missingSpace(string) Create a JavaScript Arrow function that meets the following requirements: • Authored using arrow expression syntax (constant name _missingSpace) • The function is passed a string argument • The function inserts a white space between every instance of a lowercase character followed immediately by an uppercase character, and returns the modified string, with whitespaces, back to the caller. • Console log output is NOT permitted.arrow_forwardUsing C++ Language Write a function call with arguments tensPlace, onesPlace, and userInt. Be sure to pass the first two arguments as pointers. Sample output for the given program: tensPlace = 4, onesPlace = 1 Code: #include <stdio.h> void SplitIntoTensOnes(int* tensDigit, int* onesDigit, int DecVal){ *tensDigit = (DecVal / 10) % 10; *onesDigit = DecVal % 10;} int main(void) { int tensPlace; int onesPlace; int userInt; scanf("%d", &userInt); /* Your solution goes here */ printf("tensPlace = %d, onesPlace = %d\n", tensPlace, onesPlace); return 0;}arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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