How to check the size of a PFA-type array?

Asked

Viewed 155 times

2

How do I check whether the array PFA (Partially-full array) is full and new elements cannot be added? If it is full, it should return true.

boolean estaCheio(int[] data, int size){
       ??? }

I have 5 possible options. I believe the A option returns trueif the array is full. That’s right?

a) return size == data.length;
b)return size == data.length-1;
c)return size <= data.length;
d)return size <= data.length-1;
e)return size < data.length;

If I create an insert function, this function will give error if the list is full. As the function above staCheio() could prevent this?

int insert(int[] data, int size, int newData){
   if( ??? )
      data[size++] = newData;
   return size;
  }

Which of these options should I use? I believe is option C. That’s correct?

a)!isFull(data, size+1)
b)isFull(data, size+1)
c)!isFull(data, size)
d)isFull(data, size)
e)!isFull(data, size-1)

To test the above code I use this code:

void setup(){
  int MAX_SIZE = 4;
  int[] numbers = new int[MAX_SIZE];
  int howMany = 0;

  howMany = insert(numbers,howMany,34);
  howMany = insert(numbers,howMany,99);
  howMany = insert(numbers,howMany,14);
  howMany = insert(numbers,howMany,22);
// howMany = insert(numbers,howMany,50);
  int newSize = insert(numbers, howMany, 57);
  if( newSize == howMany )
      println("Insert failed!! " + newSize);
  howMany = newSize;

  delete(numbers, newSize);


 // howMany = insert(numbers,howMany,50);
  printAll(numbers,howMany);
  println (isFull (numbers,howMany));
}

void printAll(int[] data, int size){
  for(int i=0; i<size; i++)
    print(data[i]+"  ");
  println();
}


 int insert(int[] data, int size, int newData){
   if( !isFull(data, size) )
      data[size++] = newData;
   return size;

}

boolean isFull(int[] data, int size){
       return size == data.length;
     }


      int delete(int[] data, int size){
    if(size>0){
      for( int i=1; i<size; i++ )
        data[i-1]=data[i];
      size--;
    }//if
    return size;
  }
  • You saw on the [tour] that you can vote on everything on the site?

1 answer

0


The two options are correct, within normal logic would be this, although I think the code is bad.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.