Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I searched online and i found this problem on thisbook.Engineering Problem Solving with C++But i have noanswers, I miss…

ID: 3615690 • Letter: I

Question

Write a program that reads a data file anddetermines the number of occurrences of each of the characters inthe file. Then, print the characters and the number of times thatthey occurred. If a character does not occur, do not print it(Hint;use array to store the occurrences of the characters based on theirASCII codes) Write a program that reads a data file anddetermines the number of occurrences of each of the characters inthe file. Then, print the characters and the number of times thatthey occurred. If a character does not occur, do not print it(Hint;use array to store the occurrences of the characters based on theirASCII codes)

Explanation / Answer

this program is written in C, which is pretty much the same. takesfile name at command line, maps the file to memory and turns itinto a character array, scans array, then prints output. #include #include #include // open()#include // mmap(), munmap()#include // stat(), open()#include // stat(), open()intmain(int argc, char** argv) { if(argc < 2) exit(EXIT_FAILURE); int mFileDescriptor; char* mFile; register int c; // struct stat is defined in sys/stat.h struct stat* statFile = malloc(sizeof(struct stat)); // call stat() to get file size; stored in statFile->st_size if(stat(argv[1], statFile)) { fprintf(stderr, "Error: stat() "); exit(EXIT_FAILURE); } // call open() to retrieve file descriptor for mmap() if((mFileDescriptor = open(argv[1], O_RDONLY)) == -1) { fprintf(stderr, "Error: open() "); exit(EXIT_FAILURE); } // call mmap() to map file to memory as char array if((mFile = (char*) mmap((char*) 0, statFile->st_size, PROT_READ, MAP_PRIVATE, mFileDescriptor, 0)) == (char*) -1) { fprintf(stderr, "Error: mmap() "); exit(EXIT_FAILURE); } // use calloc() because it automatically initializes values to 0 int* charCounter = (int*) calloc(256, sizeof(int)); // while((c = *mFile++) != '') charCounter[c]++; // deallocate mmap array munmap((char*) mFile, statFile->st_size); // prints data: 1st column is ASCII code, 2nd is character (if not a control char), 3rd is occurrence for(int i = 0; i < 256; ++i) if(charCounter[i]) printf("%3d %c %d ", i, i < 32 ? ' ' : (char) i, charCounter[i]); return 0;}