vendredi 13 novembre 2020

Drawing a Control Flow Graph (CGF) For Loop

I have posted the code below and I also created a CFG for the first function (addItem). Not sure if I am on the right track at all. Please let me know. I am very confused on how to create a CGF for the second function (removeItem). Would like some help with that one. Thank you.

 public void addItem(int number, int[] array, int position) {
            for (int i = array.length - 1; i > position; i--) {
                array[i] = array[i - 1];
            }
            array[position] = number;
        }

enter image description here

Here is the second function, removeItem:

public void removeItem(int number, int[] array) {
        int[] tempArray = new int[array.length];
        int j = 0;
        for(int i = 0; i < array.length; i++) {
            if(array[i] != number) {
                tempArray[j] = array[i];
                j++;
            }
        }
        for(int i = j; i < array.length; i++) {
            tempArray[i] = 0;
        }
        for (int i = 0; i < tempArray.length;i++)
        {
            array[i] = tempArray[i];
        }
    }

Aucun commentaire:

Enregistrer un commentaire