Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write a C++ program with the following: 1. A class named HexHistogram (files HexHistogram.h and HexHistogram.cpp) Function prototypes and member variables should be declared in

  1. write a C++ program with the following:

    1. A class named HexHistogram (files HexHistogram.h and HexHistogram.cpp)

    • Function prototypes and member variables should be declared in the header (.h) file.

    • All member variables should be private.

    • The constructor and all member functions should be implemented in HexHistogram.cpp.

    • The constructor has one parameter of type std::string, the name of a text file.

o Your class must read the file only once, in the constructor. The constructor must store the needed information (see below) in appropriate variables and data structures. The constructor should close the file.

o Any errors encountered in the constructor (such as input file not found) should throw an exception of type std::runtime. A string describing the error should be supplied as the exception argument. The string should also include the file name.

Your class must implement these public methods: get_value_count: returns the total number of values in the file (type uint32_t).

  1. Write a C++ program with the following:

    1. A class named HexHistogram (files HexHistogram.h and HexHistogram.cpp)

    • Function prototypes and member variables should be declared in the header (.h) file.

    • All member variables should be private.

    • The constructor and all member functions should be implemented in HexHistogram.cpp.

    • The constructor has one parameter of type std::string, the name of a text file.

1

  • get_unique_value_count: returns the number of unique values in the file (type uint32_t).

  • get_line_count: returns the number of lines in the file (type uint32_t)

  • get_smallest_number(uint32_t line_number) returns the smallest value in the line (as

    an uint32_t) o The first line in the file is line 1. o If an invalid line number is given, you should throw a std::logic_error.

  • print: print the histogram contents

o Each unique value should be printed followed by a colon (:), followed by the

number of occurrences of the value. o The values should be printed in increasing order of the unique values. Values

should be printed as hexadecimal numbers. The values should be right aligned in

a field of width 8. o The number of occurrences should be right aligned in a field of width 8. The

number of occurrences should be printed as a decimal number. 2. A main function in main.cpp

The main function accepts a single command line argument of type std::string, the name of a text file. If the number of command line argument is incorrect, you should print the following

message:

 Usage: ./Project1  

Your main program should create a single instance of HexHistogram using the input file specified as the command line argument. The main program should then print the results of functions get_value_count, get_unique_value_count, and get _line_count.

All counts should be output as decimal numbers. All numbers read from the file should be output as hexadecimal numbers. Your output should be formatted so that it is easy to read.

The program should then use a loop to print the line number followed by a colon (:), followed by the smallest value in that line. You should start with the last line in the file and end with the first line in the file. Line numbers should be printed as decimal numbers and should be right aligned in a field of width 4. The values should be right aligned in a field of width 8 and printed as hexadecimal numbers.

After printing the statistics above, call the print method of the HexHistogram class.

2

Implementation Notes for WordCount

Use the C++ type ifstream for the input file. Use the function getline to read each line of the file into a string.

For each line, create a variable of type istringstream to hold the line contents for parsing numbers. Parse the numbers by using the >> operator with the istringstream variable and a string destination. Use the std::hex modifier to read the values as hexadecimal numbers. Both the getline function and the >> operator return a value which can be tested as true or false in an if or while statement. A false value indicates end of file or error. You should use the std::map class to store the frequency count (histogram) data and the smallest number data.

Sample Input/Output

Input:

334 92f 011a 92f ffff0000 1ffff 1234abcd dedc9876 11a dedc9876 92f 01ffff 

Output:

Number of values read: 12 Number of unique values read: 7 Number of lines: 4 
Smallest number on each line 4: 92f 
 3: 11a 2: 1ffff 1: 11a 
Histogram 11a: 2 

334: 1

 92f: 3 1ffff: 2 1234abcd: 1 dedc9876: 2 ffff0000: 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions