Conversational-AI-Tools/C2/Debugging-with-AI/English
Title of the script: Debugging with AI
Author: Sudhakar Kumar and EduPyramids Team.
Keywords: debugging, errors, messages, optimize, ai, video tutorial.
| Visual Cue | Narration |
| Slide 1
Title Slide |
Welcome to the spoken tutorial on Debugging with AI. |
| Slide 2
Learning Objectives |
In this tutorial, we will learn how to use ChatGPT to
|
| Slide 3
System Requirements To record this tutorial, I am using
|
To record this tutorial, I am using the following setup. |
| Slide 4
Pre-requisites |
To follow this tutorial, learners should have basic knowledge of:
|
| Slide 5
Code Files The following code files are required to practice this tutorial.
These files are provided in the Code Files link of this tutorial page. |
The following code files are required to practice this tutorial.
These files are provided in the Code Files link of this tutorial page. |
| To open the text editor, type the command in the terminal.
Type: gedit con.cpp & #include <iostream> int main() { const int x = 5; int* ptr = &x; *ptr = 10; std::cout << x << std::endl; return 0; } Type: g++ con.cpp -o output to compile the code. Highlight the error message in the compiler |
Open the Linux terminal and type this command to open con.cpp file.
Let us compile the code in the linux terminal. For that type this command and press Enter We can see an error in the code. The error message states the invalid conversion from 'const int*' to 'int*' |
| This error message may not clearly indicate the actual issue in the code.
Let us take help from ChatGPT. | |
| Click on the Projects icon in the left panel.
In the Project name box, type the name as Debugging with AI. Click on the Create project button. |
Create a New Project with the name of Debugging with AI. |
| Highlight Debugging with AI | For this tutorial, we will use this project workspace. |
| Cursor on Chat window
Here is my code: #include <iostream> int main() { const int x = 5; int* ptr = &x; *ptr = 10; std::cout << x << std::endl; return 0; } I have a C++ code that gives the following compilation error: The error message states the invalid conversion from 'const int*' to 'int*' Explain why this error occurs and how to fix it. |
In the Chat window, copy paste the code.
Type the following prompt and Press Enter. |
| Highlight the generated response in the Chat window
Highlight Copy Code in the Chat window. Click on the copy code button. Copy paste the corrected code and click on Save button. To compile type: g++ con.cpp -o output. Type ./outputHighlight the corrected output. |
It first goes through the code and explains why the error is happening.
Next, it suggests how to fix it and gives the corrected code. We can copy the code by clicking the Copy code icon. Open the text editor and paste the copied code. Save the file. Let’s go back to the terminal and compile the corrected code. Compilation is done, let's see the output. Here the output is shown successfully. |
| Open file sumOfElems.cpp in the text editor.
Type: gedit sumOfElems.cpp & Highlight: int sumVector(const std::vector<int>& nums) { int sum = 0; for (int num : nums) { sum += num; } return sum; } |
Let’s see another code.
Open file sumOfElems.cpp in the text editor. We have a function sumVector. It prints, sum of the elements present in the vector. |
| Highlight the following piece of code
std::cout << "Sum of small numbers: " << sumVector(smallNumbers) << std::endl; std::cout << "Sum of large numbers: " << sumVector(largeNumbers) << std::endl; |
The first cout statement should print Sum of small numbers: 15
And, the second cout statement should print: Sum of large numbers: That is forty-five followed by eight zeros. |
| To compile type: g++ sumOfElems.cpp -o output
Type:./output To run the code. Highlight the output |
Let’s compile the code in the terminal
For that type this command and press enter. After the compilation, Type this command to run the code. The output for the first cout statement is given correctly. However, the output for the second cout statement is incorrect. It means that our code works fine for small numbers but not for large numbers. |
| Let us ask ChatGPT to help out. | |
| Point to the file
Click New Chat Type the following in ChatGPT and press Enter. I have attached a file named sumOfElems.cpp. Analyze this code and find any potential bugs or edge cases. Explain why each issue occurs and suggest fixes. |
I have saved the program as sumOfElems.cpp on my system.
Go to the workspace of the project Debugging with AI. In a New chat, click on Add files and upload the file sumOfElems.cpp. Type the following prompt to analyze this code. And, press Enter. |
| Highlight the generated response | ChatGPT points out that the code may not work for large numbers.
ChatGPT suggests alternate corrected code. Users may compile the corrected code on their own. |
| Open primeNums.cpp in text editorType: gedit primeNums.cpp &Highlight:
#include <iostream> vector<int> numbers = { 2, 15, 17, 20, 23, 30 }; cout << "Prime numbers : " << endl; for (int i = 0; i < numbers.size(); i++) { if (isPrime(numbers[i])) { cout << numbers[i] << " "; } } Type: g++ primeNums.cpp -o output to compile the code. Type: ./output |
Now, let’s see the next code about Prime Numbers.
For each element in a vector, the program checks if it is a prime number or not. Then prints it if it is a prime number. Let’s compile the code. Compilation is successful. Now let us run the code. We can see that the prime numbers are correctly printed. |
| Let us ask ChatGPT whether this program can be further optimized. | |
| Highlight the Chat window in the project workspace
Refer to the attached code primeNums.cpp. It prints the prime numbers present in a vector. Review and optimize this code. Explain any changes or improvements to make. |
Go to the workspace of the project Debugging with AI.
Click on Add files and upload primeNums.cpp file In a New chat, type the following and press Enter. |
| Highlight the corresponding terms in the generated response. | ChatGPT offers a few suggestions that we can optimize in our code. |
| To know more about debugging with AI, refer to the Additional reading Material available on this tutorial page. | |
| Slide 8
Summary In this tutorial, we have learnt how to use ChatGPT to
|
With this, we come to the end of this tutorial.
Let us summarise. |
| Slide 9
Assignment Given a positive integer n, write a C++ code to find the nth Fibonacci number. Give this code to ChatGPT and get it reviewed for improvements. |
As an assignment, please do the following. |
| Slide 10
Thank you |
This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited, SINE, IIT Bombay.
Thank you for joining! |