Question

In: Computer Science

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.

Class will provide (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).

-----> You must consider it has its 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 // ... } 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.

Solutions

Expert Solution

ANSWER:

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;
}

Output:

If you do not get anything in this solution ,please put a comment and i will help you out .

Do not give a downvote instantly.It is a humble request.

If you like my answer,please give an upvote....Thank you.


Related Solutions

In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
c++ You will implement a template class with the following members: An array of our generic...
c++ You will implement a template class with the following members: An array of our generic type. The size of this array should be determined by an integer argument to the constructor. An integer for storing the array size. A constructor capable of accepting one integer argument. The constructor should initialize the array and set the array size. A method, find Max, that returns the maximum value in the array. A method, find Min, that returns the minimum value in...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
Write a program to compute intersection of two sorted array of integers and compute the CPU...
Write a program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator. Test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE NO HASH SET
For this assignment you will implement a dynamic array. You are to build a class called...
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following: MyDynamicArray(); Default Constructor. The array should be of size 2. MyDynamicArray(int s); For this constructor the array should be of size s. ~MyDynamicArray(); Destructor for the class. int& operator[](int i); Traditional [] operator. Should print a message...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT