Question 3 (40 Marks)
This question is based on Unit 3 and Unit 4. It tests your ability to read from a file on a disk, to collate data and to represent it graphically.
In this question you have to display data which is submitted by the user and saved on a file on disk. The data consists of a listing of grades scored by AOU students, each being an integer in the range from 20 to 80 inclusive. Your task is to draw a histogram on the accumulated data, as illustrated in the figure. The “bar” heights correspond to the number of times the grade has occurred. In the illustration, grade 21 has occurred once and grade 50 has occurred 5 times.
You will have to decide on appropriate scales for your display, but you may assume that no grade has been obtained by more than 15 students. The grades of the students should be submitted using the stack program StakEx1U.bpr you find in the Block III folder.
It is suggested you tackle the problem in the following stages:
- Run the program; push into the stack the grades of 20 students.
- Add a SaveToFile button and write the C++ necessary code so you can save the submitted grades to a data file.
- Use one or more event handlers to read the data on file you saved, and to count (and store) how many times each of the grades from 20 to 80 inclusive occurs in the data. You will need to think carefully about where the variables you use should be declared, because some may become needed by more than on event handler. Note also that to use the fstream library you will need to add the directive : #include <fstream.h> at an appropriate place in the code.
- Write the code to display the data in the form’s OnPaint event handler. You can obtain the necessary code template by double clicking on the form’s OnPaint event in the Object Inspector. The intention is that you should use the MoveTo and LineTo methods of the Canvas for drawing the bars, but exploration of Builder may lead you to alternative ways which you may prefer. Your display should give an indication of (some of) the grades corresponding to the bars and of the scale of the vertical axis.
- compile, debug and run your program to display a histogram of the submitted data
- You may choose to load other data files and view the corresponding histogram. Modify the Exit menu and add a File menu with Load and Quit options. Design and write code for each of these menu items. On selection of File|Load event handler a dialog box should appear listing all the files with extension .txt in the current folder. When one of these is selected the appropriate data file is to be loaded and your program should subsequently display the corresponding histogram. Note that either of the code statements:
Repaint() or Invalidate()
Will clear the form and then invoke the code in the OnPaint handler.
Submit to your tutor the executable version of your final program which also includes the three data files, together with a listing of the unit’s code file.
االسؤال موضح في المل المفرق
اكون شاكرتلكم مساعدتكم