Errors & Fixes
1. Missing semicolon in printf()
o Fix: printf("The value of x is: %d\n", x);
2. Division by zero (int z = x / y;)
o Fix: Check if y is zero before division.
Fixed Code:
#include <stdio.h>
int main() {
int x = 5;
int y = 0;
printf("The value of x is: %d\n", x);
if (y != 0) {
int z = x / y;
printf("Result of division: %d\n", z);
} else {
printf("Error: Division by zero is not allowed.\n");
}
return 0;
}
Answers to the Lab Questions
1. What are the three most important things to generate correct code with ChatGPT?
1. Use clear and specific prompts (e.g., "Implement sine function using Taylor series
in C").
2. Understand and verify the output (ChatGPT might generate incorrect code, so test
it).
3. Iterate and refine (Ask ChatGPT to improve precision, fix errors, or optimize
performance).
2. Why is it important to test the generated code?
• Generated code might have logic errors or edge cases ChatGPT didn’t consider.
• Testing ensures the implementation is accurate and meets the required precision.
• Helps identify hidden bugs or inefficiencies.
3. Why is it still essential to learn to write code manually?
• Debugging skills: Understanding code helps in fixing errors when AI generates
incorrect results.
• Efficiency & Optimization: AI-generated code may not be optimized for speed or
memory.
• Real-world application: Many programming tasks require creativity and problem-
solving, which AI alone cannot handle.