How do I check to see if the array PFA (Partially-full array) is full and new elements can not be added? If it is full, it should return true
.
boolean estaCheio(int[] data, int size){
??? }
I have 5 possible options. I believe that option A returns true
if the array is full. Is correct?
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. How could the above staCheio()
function prevent this?
int insert(int[] data, int size, int newData){
if( ??? )
data[size++] = newData;
return size;
}
Which of these options should I use? I think it's option C. Is that 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;
}