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.
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.
Get Answers For Free
Most questions answered within 1 hours.