Question

You are asked to implement a C++ class to model a sorted array of unsigned integers....

You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used like: somearray.setat(index, value) and somearray.getat(index)); (3) provide overloaded operators: assignment (=), index ([]), equality (==), and stream output (<<); (4) make use of move semantics when appropriate; and (5) provide error reporting to client programmers using the class. Finally, you should also provide a test program to demonstrate that your class behaves as expected (and that the array remains sorted).

own header file (.h), implementation file (.cpp), and test file (.cpp).
- here are the simple points of the assignment that I'd to clarify:
1- As stated in the assignment description "you need to provide the appropriate constructors and destructor", so you have to provide an implementation for each of the following:- default constructor, int constructor, copy constructor, move constructor, and destructor.
2- Also you are required to provide an implementation for:
- setAt method - getAt method - copy assignment operator - move assignment operator - equality operator - stream output operator (<<).
3- Also You are required to provide an implementation for index operator [ ] constant and non-constant versions (which allow for both reading and modifying of an array element).
4- The most important part of this assignment is to keep your array sorted at any point of your program, you may think of sorting with any algorithm when inserting a new value to your array.
5- There is a nice trick when trying to use the non-const version of [ ] operator and trying to modify an element of your array. You have to keep your array sorted at any time, you can consider the following code segment.
{ // Assume A is SortedArray object holds { 5, 7, 8, 11}   
A[2] = 3; // after this statement A should be { 3, 5, 7, 11}
cout << A[2]; // should print 7
// ...
}

6- You have to provide a clear error reporting, it's recommended to use exceptions and try to use classes like 'invalid_argument' and 'out_of_range' in and catch error using try-catch block in main. (you can check examples in Ch. 9 of Deitel book to see how to use exceptions).
7- You have to care about input validation and boundary checking, defining a function as const or not, and the return type and parameter type for each function.
8- You may need to initialize array elements at beginning to a default value, assuming that you will sort your array in ascending order, so I recommend to initialize elements by 'UINT_MAX' value.

Homework Answers

Answer #1

Solution :

To provide the index operator[] to enable accessing specific bits in an array

A bitset is an array of bool in which the information is stored in a compressed manner.We can retrieve each bit of bitset individually with help of array indexing operator [] that is bs[3] shows bit at index 3 of bitset bs just like a simple array. We must Remember bitset starts its indexing backward that is for 10110, 0 are at 0th and 3rd indices whereas 1 are at 1st 2nd and 4th indices. i have given you a workaround in case while compiling the program it gives you an error unable to open <bits/stdc++.h> then you can try following things.if the problem still persists contact us.Its giving me error may be because I am using turbo C ++ simulator and it had worked on the real software once so you can try implementing it on the real software and see the results


// C++ program to demonstrate array indexing
#include <bits/stdc++.h>

using namespace std;
  
#define M 32
  
int main()
{
// default constructor initializes with all bits 0
bitset<M> bset1;
  
// bset2 is initialized with bits of 20
bitset<M> bset2(20);
  
// bset3 is initialized with bits of specified binary string
bitset<M> bset3(string("1100"));
  
// cout prints exact bits representation of bitset

cout << bset1 << endl; // 00000000000000000000000000000000
cout << bset2 << endl; // 00000000000000000000000000010100
cout << bset3 << endl; // 00000000000000000000000000001100
cout << endl;
  
// declaring set8 with capacity of 8 bits
  
bitset<8> set8; // 00000000
  
// setting first bit (or 6th index)

set8[1] = 1; // 00000010
set8[4] = set8[1]; // 00010010
cout << set8 << endl;
  
// count function returns number of set bits in bitset
int numberof1 = set8.count();
  
// size function returns total number of bits in bitset
// so there difference will give us number of unset(0)
// bits in bitset

int numberof0 = set8.size() - numberof1;
  
cout << set8 << " has " << numberof1 << " ones and "

<< numberof0 << " zeros\n";
  
// test function return 1 if bit is set else returns 0
cout << "bool representation of " << set8 << " : ";
for (int i = 0; i < set8.size(); i++)
cout << set8.test(i) << " ";
  
cout << endl;
  
// any function returns true, if atleast 1 bit
// is set
if (!set8.any())
cout << "set8 has no bit set.\n";
  
if (!bset1.any())
cout << "bset1 has no bit set.\n";
  
// none function returns true, if none of the bit
// is set
if (!bset1.none())
cout << "bset1 has some bit set\n";
  
// bset.set() sets all bits
cout << set8.set() << endl;
  
// bset.set(pos, b) makes bset[pos] = b
cout << set8.set(4, 0) << endl;
  
// bset.set(pos) makes bset[pos] = 1 i.e. default
// is 1
cout << set8.set(4) << endl;
  
// reset function makes all bits 0
cout << set8.reset(2) << endl;
cout << set8.reset() << endl;
  
// flip function flips all bits i.e. 1 <-> 0
// and 0 <-> 1
cout << set8.flip(2) << endl;
cout << set8.flip() << endl;
  
// Converting decimal number to binary by using bitset
int num = 100;
cout << "\nDecimal number: " << num
<< " Binary equivalent: " << bitset<8>(num);
  
return 0;
}

Please refer to the screenshot for Output:

Output:

I have tried to explain it in very simple language and
I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
c++ just one file for all of it. You are to implement a 'list' class to...
c++ just one file for all of it. You are to implement a 'list' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. your list will be an array. the list has no order, except for the order you insert or delete. The methods you are to implement are as given: constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'list listA(listB)')...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Write in C++. Define a class called Text whose objects store lists of words. The class...
Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy....
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy. Disk blocks are numbered consecutively from the beginning of the file with the first block numbered as 0. Assume that blocks are 4096 bytes in size. Use the supplied C++ files to implement your LRU Buffer Pool based on the instructions below. • Implement a BufferBlock class using the supplied BufferBlockADT.h o Your Buffer Block must inherit BufferBlockADT or you will not get credit...
For this question you will write two (2) constructors in the implementation file: CylinderType.cpp (Assume that...
For this question you will write two (2) constructors in the implementation file: CylinderType.cpp (Assume that the base class CircleType has a default constructor & assigns 1.0 to its private double radius) part (a) // The first constructor is a default constructor. Please use height as the CylinderType's private member name in the body. // It will create a CylinderType object with a radius of 1.0 and height of 1.0 part (b)    // The second constructor should have two...
Part 1 - LIST Create an unsorted LIST class ( You should already have this code...
Part 1 - LIST Create an unsorted LIST class ( You should already have this code from a prior assignment ). Each list should be able to store 100 names. Part 2 - Create a Class ArrayListClass It will contain an array of 27 "list" classes. Next, create a Class in which is composed a array of 27 list classes. Ignore index 0... Indexes 1...26 correspond to the first letter of a Last name. Again - ignore index 0. index...