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

c++, the one everyone is copy pasting does not do what the assignment asks. I ne

ID: 3772739 • Letter: C

Question

c++, the one everyone is copy pasting does not do what the assignment asks. I need different types of sorting and also to intialize the array.

Create a class called Invoice with the properties (Part number, Part Description, Quantity and Price).
Create appropriate methods and data types.
Use the class Invoice and create an array of Invoice objects (Part number, Part Description, Quantity and Price) initialized as shown below:

// initialize array of invoices
Invoice[] invoices = {
new Invoice( 83, "Electric sander", 7, 57.98 ),
new Invoice( 24, "Power saw", 18, 99.99 ),
new Invoice( 7, "Sledge hammer", 11, 21.5 ),
new Invoice( 77, "Hammer", 76, 11.99 ),
new Invoice( 39, "Lawn mower", 3, 79.5 ),
new Invoice( 68, "Screwdriver", 106, 6.99 ),
new Invoice( 56, "Jig saw", 21, 11.00 ),
new Invoice( 3, "Wrench", 34, 7.5 )
};

Write a console application that displays the results:

a) Use a Selection sort to sort the Invoice objects by PartDescription in ascending order.
b) Use an Insertion sort to sort the Invoice objects by Price in descending.
c) Calculate the total amount for each invoice amount (Price * Quantity)
d) Display the description and totals in ascending order by the totals.

Sorted by description ascending order:

83 Electric sander 7 $57.98
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
39 Lawn mower 3 $79.50
24 Power saw 18 $99.99
68 Screwdriver 106 $6.99
7 Sledge hammer 11 $21.50
3 Wrench 34 $7.50

Sorted by price in descending order:

24 Power saw 18 $99.99
39 Lawn mower 3 $79.50
83 Electric sander 7 $57.98
7 Sledge hammer 11 $21.50
77 Hammer 76 $11.99
56 Jig saw 21 $11.00
3 Wrench 34 $7.50
68 Screwdriver 106 $6.99

Explanation / Answer

Your array intialization code isnt right.

Here is the correct code along with all functions and two sorting techniques as asked the the question.

//Function void sortByDesc(Invoice *invoices, int count) uses Selection sort

//Function void sortByPriceDesc(Invoice *invoice, int count) uses Insertionsort

#include<iostream>

#include <fstream>

#include <string>


using namespace std;

class Invoice
{
   private:
   int part_no;
   string desc;
   int qty;
   float price;
   public:  
   Invoice();
   Invoice(int part_no, string desc, int qty, float price);
   int getPartNo();
   string getDesc();
   int getQty();
   float getPrice();
};

Invoice::Invoice()
{
   this->part_no=0;
   this->desc="";
   this->qty=0;
   this->price=0;
}
Invoice::Invoice(int part_no, string desc, int qty, float price)
{
   this->part_no=part_no;
   this->desc=desc;
   this->qty=qty;
   this->price=price;
}
int Invoice::getPartNo()
{
   return this->part_no;
}
string Invoice::getDesc()
{
   return this->desc;
}
int Invoice::getQty()
{
   return this->qty;
}
float Invoice::getPrice()
{
   return this->price;
}

void sortByDesc(Invoice *invoices, int count);
void sortByPriceDesc(Invoice *invoices, int count);
void dispTotalAmount(Invoice *invoices, int count);
void dispByTotal(Invoice *invoices, int count);

void sortByDesc(Invoice *invoices, int count) // Selection sort
{
    Invoice currentMin;
    int i,j,minIndex;
    for (i=0; i<count; i++) {

// find smallest element in elements i to x.length

currentMin = invoices[i];

minIndex = i;

for (j=i+1; j < count; j++) {

if (invoices[j].getDesc().compare(currentMin.getDesc())<0) {

currentMin = invoices[j];

minIndex = j;

}

}

// swap smallest element with element i

if (minIndex != i) {

invoices[minIndex] = invoices[i];

invoices[i] = currentMin;

}

}
}
void sortByPriceDesc(Invoice *invoice, int count) // Insertionsort
{
    Invoice temp;
  
for (int i = 0; i<count; i++)
  
{
  
for (int j = i + 1; j < count; j++)
  
{
  
if (invoice[i].getPrice()<invoice[j].getPrice())
{
  
temp = invoice[i];
  
invoice[i] = invoice[j];
  
invoice[j] = temp;
  
}
  
}
  
}
  
}
void dispTotalAmount(Invoice *invoices, int count)
{
   cout<< " Total amount for each invoice "<< endl;
    for (int i = 0; i<count; i++)
{
   cout << invoices[i].getDesc() << " $" <<invoices[i].getQty()*invoices[i].getPrice()<< endl;
}
}
void dispByTotal(Invoice *invoices, int count)
{
    Invoice temp;
    float t1,t2;
  
for (int i = 0; i<count; i++)
  
{
  
for (int j = i + 1; j <count; j++)
  
{
t1=invoices[i].getQty()*invoices[i].getPrice();
t2=invoices[j].getQty()*invoices[j].getPrice();
if (t1>t2)
{
  
temp = invoices[i];
  
invoices[i] = invoices[j];
  
invoices[j] = temp;
  
}
  
}
}
}



int main()
{
   int count=8;
   int i,j;
   /*
  
   Your array Intialization code is wrong (Try it)
   Invoice[] invoice = {
   new Invoice( 83, "Electric sander", 7, 57.98 ),
   new Invoice( 24, "Power saw", 18, 99.99 ),
   new Invoice( 7, "Sledge hammer", 11, 21.5 ),
   new Invoice( 77, "Hammer", 76, 11.99 ),
   new Invoice( 39, "Lawn mower", 3, 79.5 ),
   new Invoice( 68, "Screwdriver", 106, 6.99 ),
   new Invoice( 56, "Jig saw", 21, 11.00 ),
   new Invoice( 3, "Wrench", 34, 7.5 )
   };
  
  
   */
  
  
   Invoice invoice[] = {
   Invoice( 83, "Electric sander", 7, 57.98 ),
   Invoice( 24, "Power saw", 18, 99.99 ),
   Invoice( 7, "Sledge hammer", 11, 21.5 ),
   Invoice( 77, "Hammer", 76, 11.99 ),
   Invoice( 39, "Lawn mower", 3, 79.5 ),
   Invoice( 68, "Screwdriver", 106, 6.99 ),
   Invoice( 56, "Jig saw", 21, 11.00 ),
   Invoice( 3, "Wrench", 34, 7.5 )
   };
  
  
  
  
  
   sortByDesc(invoice,count);
  
   cout <<" Invoice objects by PartDescription in ascending order "<<endl;
   for(i=0;i<count;i++)
       cout << invoice[i].getPartNo()<<" "<<invoice[i].getDesc()<<" "<<invoice[i].getQty()<<" $"<<invoice[i].getPrice()<< endl;
  
  
   sortByPriceDesc(invoice,count);
   cout <<" Invoice objects by Price in descending order "<<endl;
   for(i=0;i<count;i++)
       cout << invoice[i].getPartNo()<<" "<<invoice[i].getDesc()<<" "<<invoice[i].getQty()<<" $"<<invoice[i].getPrice()<< endl;
      
   dispTotalAmount(invoice, count);
  
   dispByTotal(invoice,count);
  
   cout <<" Invoice description and totals in ascending order by the totals "<<endl;
   for(i=0;i<count;i++)
       cout <<invoice[i].getDesc()<<" $"<<invoice[i].getQty()*invoice[i].getPrice()<< endl;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote