Question: How do I use pointers and dynamic allocation to make this program run? Really I'm just confused on what I need to do. I understand
How do I use pointers and dynamic allocation to make this program run?
Really I'm just confused on what I need to do. I understand that I need to make edits to this program in order to have it utilize an array of pointers, pointing to the data that is useful but I am unsure where to start and the directions below are confusing on top of all of that. For the code below I made all the comments in the code bold to help differentiate between actual code and tidbits of information. If anyone can look this over and shed some light on where to start or what to do that would be appreciated. Not necessarily looking for someone to code this for me but moreso to shed light on what parts need changed.
For this project, you need to study the given program and modify it (document it with your name, etc., but also credit the original author), so that the modified version exhibits the following features:
The main array in the program is no longer an array of employee records, but an array of pointers to employee records. This new data structure allows for each record to be dynamically allocated, so space is not wasted, but used only as needed. (Note that in the given version, 100 records are allocated in the array regardless of how many of them are actually needed.)
Even this array of pointers is itself dynamically allocated (accessed from a base pointer), based on the users estimate of the maximum number of records that would be needed. (Pay attention to the declaration of indexarr and the deallocate function, after the IMPORTANT NOTE in the program supplied to you.) As long as there are more input records, new employee records may be allocated and filled in one by one, and a count may be kept of the records.
In addition to echoing the original data back, two sorted views of the employee records would have to be presented one sorted by the id and the other, by the salary. Note that we talk here about sorted views: The data themselves are not moved at all! You maintain two separate indexes each of which is simply an array of pointers that would allow you to print the data in sorted order (by id or by salary). The sizes of these pointer arrays can be exact, because they can be allocated after the exact number of input records has been determined in step 2!
Sorting in this approach involves starting with an array of pointers (pointing to the data in their original order) and then moving the pointers (rather than the corresponding records) around. The decision to move pointers is based on the sorting criterion (e.g., sort by id or by salary). At the end, when the records are accessed through the appropriate set of pointers in the indexed order (and printed, for example), presto, your data appear in the appropriately sorted views (sorted by the chosen sorting criterion). In the end, you will have effectively achieved efficiency (only pointers have been moved around, not the real data) and flexibility (the same data can be viewed in different ways through separate indexes, i.e., separate sets of pointers).
Here is the code I am working with (everything is in C++):
#include "pch.h" #include
const int MAXDBSIZE = 100;
//Type delcarations - NO MEMORY ALLOCATED (EVEN STATICALLY) struct emprec /*Employee record with two fields*/ { int id; float salary; }; typedef emprec * emprecptr; //Employee record pointer type typedef emprec arr[MAXDBSIZE]; // Array type of pointers to employee records
/* IMPORTANT NOTE:
Note that the definitions of the indexarr pointer and the deallocate function that follow are not part of this version of the program (they are not used anywhere else in this file). You will use it in the modified version of the program, as is! It will be called at the end of the main function after all calls to printdata, in order to deallocate all the data. It is also intended to give you an idea about the data structure you need to build in the modified version of the program. */
typedef emprecptr * indexarr; /* Pointer to a dynamic array of employee record pointers*/ void deallocate (indexarr & ptrs, int & size) //Pre: ptrs is a pointer pointing to an allocated dynamic array of "size" employee record pointers. //Post: ptrs points to a dynamic pointer array. The "size" elements of the dynamic array, filled with //pointers to dynamically allocated employeed records, are now deallocated, and then the array of //pointers that ptrs points to is itself deallocated, with ptrs reset to NULL and size reset to 0.
{ for (int i=0; i void main () { //Rudimentary scope control in C/C++ void readdata (arr, int &); void sortbyID (arr, int); void printdata (arr, int); //Variable declarations - actual memory objects (STATIC ALLOCATION) arr index; int dbsize; char temp; readdata (index, dbsize); cout << "Original data:" << endl; printdata (index, dbsize); sortbyID (index, dbsize); cout << "Data sorted by the ID:" << endl; printdata (index, dbsize); cout << "Please input a non-white space key to end the session." < void readrec (emprec& rec) //Pre: Input available as expected. //Post: Record rec filled with input data. { cin >> rec.id; cin >> rec.salary; }; void readdata (arr DB, int & size) //Pre: DB is an array of MAXDBSIZE employee records. //Post: 0<=size<=MAXDBSIZE and the first "size" elements of the DB array are filled with input data. { void readrec (emprec&); char YN; size = 0; do { cout << "More data (Y/y/N/n)? " << endl; cin >> YN; if ((YN == 'Y') || (YN == 'y')) if (size < MAXDBSIZE) readrec (DB[size++]); else cout << "Database size may not exceed " << MAXDBSIZE << endl; else if ((YN != 'N') && (YN != 'n')) cout << "Please answer Y/y/N/n. "; } while ((YN != 'N') && (YN != 'n')); }; void printrec (emprec rec) //Pre: rec has data. //Post: The data in rec printed out. { cout << rec.id << ' ' << rec.salary << endl; }; void printdata (arr DB, int size) //Pre: 0<=size<=MAXDBSIZE and the first "size" elements of the DB array are //employee records containing data. //Post: They are all printed out in the order pointed at. { void printrec (emprec); int i; for (i=0; i void swap (emprec & x, emprec & y) //Pre: None. //Post: The values of x and y are swapped. { emprec temp; temp = x; x = y; y = temp; }; int selectsmallestID (arr DB, int first, int last) //Pre: "first" through "last" elements of the DB array are employee //records containing data. //Post: A value k is returned such that, for i ranging from first to last, //the relation DB[k].id <= DB[i].id holds. { int j, result; result = first; for (j=first+1; j<=last; j++) if (DB[j].id < DB[result].id) result = j; return result; }; void sortbyID (arr DB, int size) //Pre: 0<=size<=MAXDBSIZE and the first "size" elements of the DB array are //employee records containing data. //Post: for i ranging from 1 to size-2, the relation DB[i].id <= DB[i+1].id holds. { int selectsmallestID (arr, int, int); int i, smallindex; for (i=0; i<=size-2; i++) { smallindex = selectsmallestID (DB, i, size-1); swap (DB[i], DB[smallindex]); }; };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
