Skip to main content

Statement testing with Chat GPT

ChatGPT

You 4.5can fightinguse withany insertSortother function

Chat

InBot. If you do so, change the examplepage belowtitle.

we

Task consider1 statement(student testing1) using

chatgpt.

 TheUsing chatbot'sa taskChat isBot to identify statements for the insertSort functionfunction. -Assign a tag to for each identified statement.  Present the list of statements with tags. Write the prompt and tothe createchatbot labelsanswer. andDiscuss athe graph.result. 

For thisDoes task,it GPTproperly -recognized 4.5 inthat the freeFOR versioninstruction isconsists used.of The3 graphstatements?  is also generated using GPT - the "Create image" function.

Prompt: 

Identify statements for the insertSort function. Assign a tag to for each identified statement. Present the list of statements with tags.
ChatGPT answered:

Here’s a breakdown of a canonical insertSort (in-place insertion sort) in pseudocode, with each statement identified and tagged:

  1. function insertSort(A[0…n-1])
    Tag: FunctionDeclaration

  2. for i ← 1 to n-1 do
    Tag: ForLoop

    • Initialization (i ← 1)
      Tag: LoopInit

    • Condition (i ≤ n-1)
      Tag: LoopCondition

    • Update (i ← i + 1)
      Tag: LoopUpdate

  3. key ← A[i]
    Tag: Assignment

  4. j ← i − 1
    Tag: Assignment

  5. while j ≥ 0 and A[j] > key do
    Tag: WhileLoop

    • Condition (j ≥ 0)
      Tag: Comparison

    • Condition (A[j] > key)
      Tag: Comparison

  6. A[j + 1] ← A[j]
    Tag: Shift

  7. j ← j − 1
    Tag: Decrement

  8. end while
    Tag: LoopEnd

  9. A[j + 1] ← key
    Tag: Insertion

  10. end for
    Tag: LoopEnd


Legend of Tags

  • FunctionDeclaration: Introduces the function or method.

  • ForLoop / WhileLoop: Entry to a loop structure.

  • LoopInit / LoopCondition / LoopUpdate: The three parts of a for loop.

  • Assignment: Storing a value into a variable.

  • Comparison: Evaluating a relational expression.

  • Shift: Moving an array element to make room.

  • Decrement: Reducing a counter by 1.

  • Insertion: Placing the key element into its sorted position.

  • LoopEnd: Marks the end of a loop block.

This mapping helps you see exactly what each line of the algorithm is doing and how it contributes to insertion sort’s overall flow.

image.png



   

 Task 2 (student 2) 

 Using a Chat Bot generate the Control Flow Graph in the DOT language for the insertSort function. Do not force the Chat Bot to create the instruction list. Write the prompt and the chatbot answer. Discuss the result.  

 Does the CFG has the same structure as the CFG created by a human in the previous section of the book? 

 When the Task 1 is finished, use the generated instruction list with tags as the hint for the Chat Bot. Did it help them co correct the CFG (now the CFG should be correct)? Discuss the result. 

 

Code generated by ChatGPT in DOT language:

digraph InsertSortCFG {
    node [shape=box];

    Entry [label="Entry"];
    Init_i [label="i = 1"];
    Check_i [label="i < length?"];
    Exit [label="Exit"];

    Init_value [label="value = a[i]"];
    Init_j [label="j = i - 1"];
    Check_j [label="j >= 0 && a[j] > value?"];
    Shift [label="a[j + 1] = a[j]"];
    Decrement_j [label="--j"];
    Insert [label="a[j + 1] = value"];
    Increment_i [label="++i"];

    // Control flow
    Entry -> Init_i;
    Init_i -> Check_i;
    Check_i -> Init_value [label="true"];
    Check_i -> Exit [label="false"];
    Init_value -> Init_j;
    Init_j -> Check_j;
    Check_j -> Shift [label="true"];
    Shift -> Decrement_j;
    Decrement_j -> Check_j;
    Check_j -> Insert [label="false"];
    Insert -> Increment_i;
    Increment_i -> Check_i;
}

Graph from Graphiz:

image.png

Conclusion:

Human made graph and ChatGPT graph are the same. Chat Bot added  ?  mark in conditional blocks.

 Task 3. (student 3) 

Convince a Chat Bot to write an instruction test for the function insertSort in the C language. Write the prompt and the chatbot answer. Discuss the result, compare it with the code written by a human in the previous section of the book.