Question

In: Computer Science

c++ LINEAR SEARCH ARRAYS OF OBJECTS Use the Item class defined in question #12. 1. (3...

c++

LINEAR SEARCH ARRAYS OF OBJECTS

Use the Item class defined in question #12.

1. (3 pts) Declare an array of 1024 Item objects. Assuming the array is now sorted by Value (price per item * quantity).

2. Write a function (with both declaration/prototype (3 pts) and definition (6 pts) ) that takes an array of Item objects, an integer as array size and two references to integers that represents the start_index and end_index of Item object elements whose Values are 50.0.

The function's implementation should be:

  • initialize both start_index and end_index parameters to -1.
  • perform a linear search to identify the range of all elements whose Values are 50.0. Note: since the array is sorted your searching must stop as soon as some Item exceeds 50.0.
  • If there is only one item is found both start_index and end_index must be the same. Otherwise set start_index and end_index to indicate the range of elements

An example of such array of Items will be the below:

index                   0               1               2             3           4              5           6           7               8              9          ........

Value         21.4      23.3       50.0      50.0      50.0       67.5      88.0       88.3      95.2      141.5    ....... (Note that the array is sorted)

In this example your function must assign 2 to start_index and 4 to end_index.

#12

This question has two parts:

Part 1: Define an Item class with the following specifications:

NOTE: you must declare and define the class separately. Include the definitions of constructors/destructor and member functions in class declaration will result in 50% point deduction.

  • Constructors: (MUST USE member initializer syntax) (2 pts)
    • default constructor (set description to "Unknown", quantity to 0, value to 0.0)
    • non-default constructor (3 parameters with the same names as member data' names)
  • Destructor: output <description> <quantity> and <price per item>   (1 pts)
  • Member data (2 pts): description, quantity (how many), price per item. Do not initialize member data at declaration - it must be done in constructors)
  • static data (1 pts) : company name (initialized to "Foothill Merchandise")). Must be constant string and not accessible from outside of the class.
  • public static function (1 pts): to return company name
  • public member functions (3 pts):
    • accessors/mutators: to save your exam time only provide accessor/mutator for "price per item" member
    • Increment: take a positive integer as its only parameter and add it to quantity. If the parameter is negative simply return.
    • Value: return total value of the item (quantity * price per item)

NOTE: The class definition must follow Google naming convention for class name, member data names and member function names (do not use camelCase convention)

Part 2: (5 pts)

Instantiate two Item objects: one using the default constructor and the other using the non-default constructor (description: "Toys", quantity: 100, price per item: 29.95).

Invoke the company name (static data).

Set the price per item of the default object to the non-default object's price per item.

Output the Value of the non-default object.

Increment the default-object's quantity to 9999.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#include <iostream>
using namespace std;
// Creating an Item class
class Item
{
private:
// Declaring variables
string description;
int quantity;
double value;
static const string companyname;
public:
// Zero argumented constructor
Item()
{
   this->description="Unknown";
this->quantity = 0;
this->value=0.0;
}
Item(string des,int qty,double val)
{
   this->description=des;
this->quantity = qty;
this->value=val;
}
~Item()
{
   cout<<"Description :"<<description<<endl;
   cout<<"Quantity :"<<quantity<<endl;
   cout<<"Value :"<<value<<endl;
   }
   string getDescription()
   {
       return description;
   }
   void setQuantity(int qty)
   {
       this->quantity=qty;
   }
   int getQuantity()
   {
       return quantity;
   }

// mutator function to set the description
void setDescription(string des)
{
this->description = des;
}
void setValue(double val)
{
   this->value=val;
   }
// This function will increment quantity by 1
double increment(int qty)
{
   if(qty>0)
this->quantity +=qty;
else
return quantity*value;
}
double getValue()
{
   return value;
   }
static string getCompanyName()
{
   return companyname;
   }
};

const string Item::companyname="Foothill Merchandise";

int main()
{
   //Creating Two instances of class
Item item1;
   Item item2("Toys",100,29.95);
   cout<<"Company name :"<<Item::getCompanyName()<<endl;
   item1.setValue(45.00);
   cout<<"Item#1 Price per Item :$"<<item1.getValue()<<endl;
   item1.increment(9999);
  
  
  
return 0;
}

===================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

This C++ program will use arrays, recursion, and pass by reference to calculate 02, 12, 22,...
This C++ program will use arrays, recursion, and pass by reference to calculate 02, 12, 22, 32, 42 and then print a message. Write a driver program (main) that includes the following: An array class (not a built-in-array) of 5 integers initialized to 0,1,2,3,4 the bases An integer initialized to 2 to hold the exponent A string initialized to “Good Job!\n”; A for loop that calls the recursive pow function with all of the bases and the exponent. Call the...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
In C++: In this lab we will creating twos arrays of Generic objects, searching through those...
In C++: In this lab we will creating twos arrays of Generic objects, searching through those arrays using BINARY and SEQUENTIAL sorts, calculate the run-time of each method, and determine the BIG O, OMEGA and THETA notation. In our object we should have some way to compare elements to determine if one element is LESS THAN, GREATER THAN, and EQUAL TO. After this Generic Class is defined with a method that allows for comparison is completed, then you will create...
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects....
please use C++ Create a class named Byte that encapsulates an array of 8 Bit objects. The Byte class will provide the following member functions: Byte - word: Bit[8] static Bit array defaults to all Bits false + BITS_PER_BYTE: Integer16 Size of a byte constant in Bits; 8 + Byte() default constructor + Byte(Byte) copy constructor + set(Integer): void sets Bit to true + clear(): void sets to 0 + load(Byte): void sets Byte to the passed Byte + read():...
Question An item is up for auction. Player 1 values the item at 3 while player...
Question An item is up for auction. Player 1 values the item at 3 while player 2 values the item at 5. Each player can bid either 0, 1, or 2. If player i bids more than player j then i wins the good and pays his bid, while the loser does not pay. If both players bid the same amount then a coin is tossed to determine who the winner is, and the winner gets the good and pays...
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary....
This question is in C++ Problem 3 Write a templated C++ class to represent an itinerary. An itinerary has a title, a source position and a destination position. In addition, it may include intermediate positions. All positions have the same abstract type T. The real type can be decided later, for example it can be a cartesian point with x,y,z coordinates, or an airport code, or a city name, or a country name, etc .. The itinerary includes a vector...
C# Questions: 55.A class must be ___________________ before objects of that class can be created. a.added...
C# Questions: 55.A class must be ___________________ before objects of that class can be created. a.added to the Form b.declared as Private c.contain properties d.added to the application’s project 60.If the variable type of the element in a foreach statement cannot be converted to the same variable type as the collection’s objects, ______________. a.the body of the statement is ignored b.a syntax error occurs c.a runtime error occurs d.a logic error occurs 66.C# views each file as a _____________.   ...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
In C++, there are 4 students that are running for class president. Create three arrays: the...
In C++, there are 4 students that are running for class president. Create three arrays: the candidate’s name, the number of votes the candidate receives, and the candidate’s percentage of the total votes. Your program should be broken down into at least 3 functions: find the total number of votes, calculate values for the percentage each candidate received, and sort the number of votes from highest to lowest. Write the candidate’s name, the total number of votes they received, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT