Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Question. I need help writing the function below which is meant to take a file with characters and update an array of how many
C++ Question. I need help writing the function below which is meant to take a file with characters and update an array of how many times each character is read in the file. All help is appreciated
Write a function named getFredq void getFreq(unsigned long long freq[256], const string &fileName); the array has been initialized to O's by the caller, it must do its own initialization of the array in the file. getFreq will On entry, freq contains ug (unpredictable garbage). In other words, getFreq may not assume that getFreq is supposed to set freq[n] to the number of times it encounters the char with ASCII code n Initialize freq to all O's. (I'm making the type of freq unsigned long long so that your program Try to open the file in binary mode. If getFreq can't open the file, it should complain and die will be able to handle a file with more than 4 billion of a particular char.) in other words, pass an error message to the die function Read each byte of the file, for instance by using ifstream's get method. For example, if the ifstream object is named fin, then fin.get( returns an int that is in the range [-1, 255] If get succeeded in reading a char, it returns a number in the range [0, 255]; when it does not succeed (presumably because there are no more chars in the file), get should return true. If it doesn't, then fin.get() ran into some kind of problem before the end returns -1. When get returns -1 (also known by the name EOF), fin.eof() of the file, in which case the program should c&d (complain and die) Each time fin.get() succeeds in reading a char, it increments the corresponding element in freq. (Of course, when fin.get) finally returns -1, your program will not try to increment freq[-1], which doesn't exist.) So, for example, if we had the following 2- line input file in the Windows environment: Hee Hee Hee Hee (where H is the first char of each line), then freq should end up being freq(10)//because 10 is the ASCII code for line feed, and Windows // has a line feed char for its line terminator freq(13) 2 /because 13 is the ASCIl code for return, and Windows // also has a return char for its line terminator freq(32)2/ because 32 is the ASCII code for space freq(72) 4/because 72 is the ASCIl code for 'H freq(101) 8 /because 101 is the ASCI code for e'. every other element of freq should be 0. Finally, getFreq should close the fileStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started