0% found this document useful (0 votes)
11 views6 pages

C++ Basics: Operator Precedence & Errors

Chapter 4 focuses on basic elements in C++ programming, including operator precedence, arithmetic expressions, and error identification. Exercises involve calculating totals and values for chocolates, evaluating arithmetic expressions, fixing syntax errors, and computing weighted averages. The chapter encourages the use of AI tools like ChatGPT for coding assistance and error correction.

Uploaded by

khanh.nguyenbk25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

C++ Basics: Operator Precedence & Errors

Chapter 4 focuses on basic elements in C++ programming, including operator precedence, arithmetic expressions, and error identification. Exercises involve calculating totals and values for chocolates, evaluating arithmetic expressions, fixing syntax errors, and computing weighted averages. The chapter encourages the use of AI tools like ChatGPT for coding assistance and error correction.

Uploaded by

khanh.nguyenbk25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER 4 : BASIC ELEMENTS IN C++

EXERCISES

Learning Outcomes
After completing this chapter, students will be able to:

• Explain the rules of operator precedence in C and C++.

• Trace and evaluate arithmetic expressions step by step.

• Identify and fix common syntax and identifier errors in C++ programs.

• Write C++ programs using variables, arithmetic operators, and input/output.

Exercise 1 — Integers and basic arithmetic

Problem: Calculate Total & Gift Value (integers)


Minh received 5 strawberry chocolates, 3 matcha chocolates, and 10 Dubai choco-
lates.

1. How many chocolates in total?

2. The prices are: strawberry 20,000 VND each, matcha 30,000 VND each, and
Dubai 50,000 VND each. Calculate the total value of the gift.

Solution — Idea & Explanation:

• Total chocolates: 5 + 3 + 10 = 18

• Total value: 5 × 20000 + 3 × 30000 + 10 × 50000 = 690000 (VND)

• Use int type (value < 231 ).

1
Apply AI: Use AI to code from a description
• Tool: ChatGPT (or IDE Copilot)

• Prompt (example): “Write a pseudocode, then form that write a C++ pro-
gram to calculate the total number of chocolates and their total value given:
strawberry=5 (20000), matcha=3 (30000), dubai=10 (50000). Print 2 lines:
total chocolates, total value (VND).”

• Steps:

1. Ask AI to generate code.


2. Compare with your own version (variable names, types, I/O).
3. Run and check the results (18, 690000).

2
Exercise 2 — Operator Precedence
Recall the precedence rule and associativity rule in the class:

Problem: The order of evaluation


Determine the order of evaluation and the result before coding in C++.

1. 2 + 4 ∗ 3 − 5

2. 10 − 8/2 + 6 ∗ 2 − 3

3. 0/5 ∗ 3 + 4 ∗ 2 − 12/3 ∗ 2 + 7 − 6/2

4. 25 − 7 ∗ 4%3 + 12/2

5. 9 ∗ 4%7 ∗ 6/3 + 8%5 ∗ 2 − 11/2

Suggested C++ Code:


1 # include < iostream >
2 using namespace std ;
3

4 int main () {
5 cout << (2 + 4 * 3 - 5) << ’\ n ’;
6 cout << (10 - 8 / 2 + 6 * 2 - 3) << ’\ n ’;
7 cout << (0 / 5 * 3 + 4 * 2 - 12 / 3 * 2 + 7 - 6 / 2) << ’\ n ’;
8 cout << (25 - 7 * 4 % 3 + 12 / 2) << ’\ n ’;
9 cout << (9 * 4 % 7 * 6 / 3 + 8 % 5 * 2 - 11 / 2) << ’\ n ’;
10 return 0;
11 }

Apply AI: Use AI to explain operator precedence and then verify with
code output
• Tool: ChatGPT (or IDE Copilot)

• Prompt: “Explain step by step the operator precedence in these C++ expres-
sions and then provide code to verify the results.”

• Steps:

1. AI lists the evaluation order.


2. Cross-check manually.
3. Run the program to confirm.

3
Exercise 3

Problem: Find and Fix Syntax, Identifier, I/O Errors


Consider the following C++ program, find and correct the error of the code below.

1 # include < iostream >


2 using namespace std ;
3

4 int main () {
5 int 1a , b &;
6 int sum = 1 a + b &;
7 cout >> " a + b = " >> sum >> endl ;
8 return 0
9 }

4
Apply AI: Use AI to correct compilation errors
• Tool: ChatGPT (or IDE Copilot)

• Prompt: “Analyze the compilation errors in this C++ code. Identify the
lines and types of errors (identifier, I/O operator, semicolon) and provide a
corrected version.”

• Steps:

1. Ask AI to list the compilation errors and their types.


2. Run the corrected code suggested by AI.
3. Compare AI’s explanation and fix with the instructor’s explanation.

5
Exercise 4

Problem: Calculate Weight Average

Hard problem (replacement): Weighted Average (float)


Problem: Minh’s scores: Midterm = 7, Lab = 10, Final = 9.
Weights: 30% – 20% – 50%.
Compute the weighted average using float.

Apply AI: Use AI to generate code and create testcase


• Tool: ChatGPT (or IDE Copilot)

• Prompt: “Write a C++ program to compute a weighted average (mid=7,


lab=10, final=9; weights 0.3, 0.2, 0.5). Print with 2 decimal places.”

• Steps:

1. Ask AI to generate the code.


2. Compare the AI-generated version with your own implementation.
3. Change test values (e.g., mid=8.5) and ask AI to produce additional
test cases for validation.

You might also like