-3

First of, this is an assignment and only dynamic allocated array could be used(can't use vector or map). The hint that I received was to create another array(with all elements assigned or not,I'm not sure) and compare with the original array.

So, an original array was dynamically allocated with the capacity of 50. I can't assign values or give default values to myArray.

int *myArray = new int[50];

Not all elements are present at every index. myArray may have 0,10, or 50 elements present.I don't know how many or where the elements are presents.by "not present" I meant that an element at a given index has not been initialized.

Let's assume 2 elements are present:

myArray [0] = 10;
myArray [1] = 20;

the goal is to write a bool isPresent(int index) function with 3 requirements:

if index is too big(bigger than 49 in this case),return false;

return true, if the element is present at isPresent(int index);

return false, if the element at given index is not present.

bool isPresent(int 0){}//this should return true
bool isPresent(int 1){}//this should return true
bool isPresent(int 3){}//this should return false
bool isPresent(int 49){}//this should return false
bool isPresent(int 50){}//this should return false

please help me finish the bool isPresent() function. For the second array that I can create to maybe help me, there's no requirement of how to do it. I can maybe do something like the following, but I'm not sure how that can help:

int *myArray2 = new int[50];
for (int i = 0; i < 50; i++)
{
    myArray2[i] = 100;//so I'm assigning 100 to every element for myArray2
                      //to compare?
}

The bool isPresent() function is under a array class that I need to write. The given test codes(I can't change) are in main. From main, an object of my array class will be created and different elements will be assigned to isPresent() from main.

  • 1
    Can you change the initialization of the original dynamic array? i.e. Right now this line: `int *myArray = new int[50];` Will result in undefined behavior if you try to access an element that has not had a value set. However if you change it to `int *myArray = new int[50]();` The value is by default 0 and thus the problem becomes trivial. – Rietty Feb 03 '19 at 22:02
  • yes. "int *myArray = new int[50];" can be changed. However, I can't assign values or give default values to myArray. – ShuffleUrFate Feb 03 '19 at 22:05
  • You might want to consider an array of `bool` as your second array, but any solution would depend upon how values are set in your main array. At the moment, I think this question is too vague and open for StackOverflow. – JaMiT Feb 03 '19 at 22:05
  • Possible duplicate of [Check if value really exists in a dynamic array? c++](https://stackoverflow.com/questions/10273893/check-if-value-really-exists-in-a-dynamic-array-c) – Rietty Feb 03 '19 at 22:07
  • why don't you just initialize the array to be filled with -1 or something that can't possibly be in there and check for that? – Bob Shaffer Feb 03 '19 at 22:13
  • I can't assign values or give default values to myArray. I can create my own array to compare with the first one however I want. But I'm not sure how to/how can it help. – ShuffleUrFate Feb 03 '19 at 22:16
  • How are elements going to be assigned to the array? Is it going into a class or something? – Bob Shaffer Feb 03 '19 at 22:20
  • The bool isPresent() function is under a array class that I need to write. The given test codes(I can't change) are in main. From main, an object of my array class will be created and different elements will be assigned from main. – ShuffleUrFate Feb 03 '19 at 22:23
  • *Not all elements are present at every index* -- You need to tell us what denotes a "present" element as opposed to a "non-present" element. Arrays always have a value at all of the positions. Also, the dynamic array requirement has nothing really to do with the fundamental question -- what denotes a "present" element?. You would run into the same situation using `std::vector`. – PaulMcKenzie Feb 03 '19 at 22:36
  • @PaulMcKenzie sorry I should have said by not present I meant that a element at a given index has not been manually assigned. – ShuffleUrFate Feb 03 '19 at 22:49

1 Answers1

0

You have a dynamically allocated array of integers

int* myArray = new int[size]; // where 'size' is the number of the elements in the array

The function isPresent() has to check whether a value is present at a given index.

The first trivial solution would be to default-initialize all the array elements like this:

int* myArray = new int[size]();

So that all the elements in the array have a default value of 0.

Then the isPresent() function only has to check whether the element in the array at that particular index is 0 or not

if(myArray[index]==0) 
   return false;
return true;

The problem with this implementation is that we are treating 0 as a flag rather than a value. What if the user just wanted to put 0 at index 5? Then our algorithm would merely state that at index 5 there's no element, right?

Another simple yet naive solution would be to choose another value rather than 0 (maybe -999)... but this is clearly a bad solution for the same reason I explained above, unless your array is supposed to only contain positive values!

If using structs is not a problem, I suggest you to check this answer.

gedamial
  • 1,498
  • 1
  • 15
  • 30
  • Thanks for the explanation. Structs can solve the problem, but that's not the solution the instructor is looking for. He wants something to do with creating another array and comparing with the original. – ShuffleUrFate Feb 03 '19 at 22:37
  • @ShuffleUrFate Are you sure you didn't misunderstand the assignment? Also, isn't the first solution (comparing with 0) good for you either? – gedamial Feb 03 '19 at 22:41
  • I can't assign values or give default values to myArray. The original array needs to not have any value manually assigned. – ShuffleUrFate Feb 03 '19 at 22:44
  • @ShuffleUrFate What do you mean my manually assigned? Is it YOU who fills the array like myArray[5] = 97 or is the user (using cin)? – gedamial Feb 03 '19 at 23:05
  • @ gedamial The bool isPresent() function is under a array class that I need to write. The given test codes(I can't change, not written by me) are in main. From main, an object of my array class will be created and different elements will be assigned to isPresent() from main. When I create myArray, I can't assign any value to it. – ShuffleUrFate Feb 03 '19 at 23:07
  • @ShuffleUrFate For the first time now you speak about "classes". So you have to create a class? Please be clear and edit your first post accordingly. Nobody still understands what and how you're trying to achieve... – gedamial Feb 03 '19 at 23:10
  • Sorry new to here... done. I think it doesn't change the goal or approach though. – ShuffleUrFate Feb 03 '19 at 23:18
  • @ShuffleUrFate A good problem statement helps multiple ways. Obviously it puts people in a better position to help you. But less obvious is carefully describing your problem to someone--in person, over the Internet, or to an [inanimate object](https://en.wikipedia.org/wiki/Rubber_duck_debugging)--so they can help you solve the problem is kinda of shakes things loose in your head and often leads to you stumbling across the solution. – user4581301 Feb 03 '19 at 23:55