Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in c++ Your program first reads some inventory data from a file named p1data.txt, creates a dynamically allocated part record object for each part, with

in c++

Your program first reads some inventory data from a file named p1data.txt, creates a dynamically allocated part record object for each part, with its address stored in the next position (starting from index 0) in a dynamically allocated pointer array data structure. Since you do not know how many parts to expect, you have to assume a large enough size initially (e.g., 50 or whatever). The program then prints out the data in the same order in which it was entered, which we call the original/chronological order. [The starter code allows you to write the code to resize the array dynamically, BUT IT IS OPTIONAL BONUS CREDIT. You can earn 5 bonus points if you also dynamically resize the array to the correct size, which is not hard to do, but requires planning.]

Code to edit

#include

#include

#include

#include

using namespace std;

// Type delcarations - NO MEMORY ALLOCATED (EVEN STATICALLY)

struct Part /*A Part record*/

{

int ID;

float Price;

int Quantity;

};

typedef Part * Partptr;

// Part record pointer type; The type Partptr becomes synonymous with Part *

typedef Partptr* Index;

// The type Index becomes synonymous with Partptr *

int main ()

{

/* Rudimentary scope control in C/C++ */

void readdata (Index &, int &);

void printdata (Index, int);

void copyindex (Index, Index &, int);

void sortbyID (Index, int);

void sortbyPrice (Index, int);

void deallocate (Index &, int &);

Index DBindex, IDindex, Priceindex;

int dbsize;

readdata (DBindex, dbsize);

cout << "Original data:" << endl;

printdata (DBindex, dbsize);

copyindex (DBindex, IDindex, dbsize);

sortbyID (IDindex, dbsize);

cout << "Data sorted by the ID:" << endl;

printdata (IDindex, dbsize);

copyindex (DBindex, Priceindex, dbsize);

sortbyPrice (Priceindex, dbsize);

cout << "Data sorted by the price:" << endl;

printdata (Priceindex, dbsize);

cout << "Data in the original chronological order:" << endl;

printdata (DBindex, dbsize);

cout << "Data sorted by the ID:" << endl;

printdata (IDindex, dbsize);

deallocate (DBindex, dbsize);

if (IDindex != nullptr) delete IDindex;

if (Priceindex != nullptr) delete Priceindex;

};

void eat_white (ifstream & in)

/* Pre: "in" is a valid input file stream.

Post: Any white space skipped over to EOF or the next non-white space character.*/

{

while ((in.peek()!=EOF) && isspace(in.peek())) in.ignore();

};

void resize (Index & Base, int OLDSIZE, int NEWSIZE)

/* Pre: Base is a (dynamic) Index of size OLDSIZE.

Post: Base is "EXPANDED" OR "SHRUNK" into a new (dynamic) Index of size

NEWSIZE, with data copied appropriately.

IMPORTANT NOTE: If being shrunk, it is assumed that there are only NEWSIZE valid

elements in the original array, and only that many are copied.*/

{

// STUDENT IS TO FILL THIS IN (NEEDED ONLY FOR BONUS CREDIT).

}

void readdata (Index & ptrs, int & size)

/*Pre: ptrs is a pointer that could point to a dynamic array of Part record

pointers.

Post: ptrs points to a dynamic pointer array of the right size based on the

input read in from the data file named "p1data".

All the elements of the dynamic array are filled with pointers to dynamically

allocated Part records, which in turn are filled with input data.*/

{

void resize (Index &, int, int);

// STUDENT IS TO FILL THIS IN

};

void printrec (Part rec)

/*Pre: rec has data.

Post: The data in rec printed out.*/

{

cout << rec.ID << ' ' << rec.Price << ' ' << rec.Quantity << endl;

};

void printdata (Index ptrs, int size)

/*Pre: The first "size" elements of the array pointed at by ptrs are Part records

containing data.

Post: They are all printed out in the order pointed at.*/

{

void printrec (Part);

for (int i=0; i

};

void copyindex (Index orig, Index & copy, int size)

/*Pre: The first "size" elements of the array pointed at by orig are Part records

containing data.

Post: copy points to a newly allocated dynamic array of pointers (with size

elements), and

the elements of this array are copies of the first size elements of orig.*/

{

// STUDENT IS TO FILL THIS IN

};

int selectsmallestID (Index ptrs, int first, int last)

/*Pre: "first" through "last" elements of the ptrs array are pointers to Part

records

containing data.

Post: A value k is returned such that, for i ranging from first to last,

the relation ptrs[k]->ID <= ptrs[i]->ID holds.*/

{

// STUDENT IS TO FILL THIS IN; YOU'D NEED TO CHANGE THE FOLLOWING return

STATEMENT, TOO.

return 0;

};

void sortbyID (Index ptrs, int size)

/*Pre: The first "size" elements of the ptrs array are pointers to Part records

containing data.

Post: for i ranging from 1 to size-2, the relation ptrs[i]->ID <= ptrs[i+1]->ID

holds.

ONLY POINTERS ARE MOVED. The Part records THEMSELVES ARE NOT DISTURBED OR MOVED

AROUND.*/

{

int selectsmallestID (Index, int, int);

// STUDENT IS TO FILL THIS IN

};

int selectsmallestPrice (Index ptrs, int first, int last)

/*Pre: "first" through "last" elements of the ptrs array are pointers to Part

records containing data.

Post: A value k is returned such that, for i ranging from first to last,

the relation ptrs[k]->Price <= ptrs[i]->Price holds.*/

{

// STUDENT IS TO FILL THIS IN; YOU'D NEED TO CHANGE THE FOLLOWING return

STATEMENT, TOO.

return 0;

};

void sortbyPrice (Index ptrs, int size)

/*Pre: The first "size" elements of the ptrs array are pointers to Part records

containing data.

Post: for i ranging from 1 to size-2, the relation ptrs[i]->Price <= ptrs[i+1]-

>Price holds.

ONLY POINTERS ARE MOVED. The Part records THEMSELVES ARE NOT DISTURBED OR MOVED

AROUND.*/

{

int selectsmallestPrice (Index, int, int);

// STUDENT IS TO FILL THIS IN

};

void deallocate (Index & ptrs, int & size)

/*Pre: ptrs is a pointer pointing to an allocated dynamic array of "size" Part

record pointers.

Post: ptrs points to a dynamic pointer array. The "size" elements of the dynamic

array, filled with

pointers to dynamically allocated Part records, are now deallocated, and then the

array of

pointers that ptrs points to is itself deallocated, with ptrs reset to nullptr and

size reset to 0.*/

{

for (int i=0; i

delete ptrs;

ptrs = nullptr;

size = 0;

};

sample output file

30 14.99 40000

10 20.79 20000

20 2.95 10000

25 17.09 15000

15 5.99 45000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

Students also viewed these Databases questions

Question

Choosing Your Topic Researching the Topic

Answered: 1 week ago

Question

The Power of Public Speaking Clarifying the

Answered: 1 week ago