Vertex AI documentation is no longer being updated
Vertex AI's services are now part of Gemini Enterprise Agent Platform. See the most up-to-date information in the Agent Platform documentation.
Debug check sum parity
Stay organized with collections
Save and categorize content based on your preferences.
Debug the given C++ code.
|
You can query a model directly and test the results returned when using
different parameter values with the Cloud console, or by calling the
Vertex AI API directly.
|
Debug the code:
#include <iostream>
#include <exception>
using namespace std;
void check_sum_parity(int* arr, int arrayLength) {
for (int i = 0; i < arrayLength; i++) {
sum_of_numbers += arr[i];
}
if (sum_of_numbers % 2 == 0) {
cout << "Sum is odd." << endl;
}
else {
cout << "Sum is even." << endl;
}
}
int main(void) {
int arr[] = { 10, 2, 21, 50};
int arrayLength = sizeof(arr) / sizeof(int);
check_sum_parity(arr, arrayLength);
}
Response
Of course! Let's debug the code.
There are two main issues in the check_sum_parity function:
- Compilation Error: The variable
sum_of_numbers is used in the for loop without being declared or initialized first. You must declare it as an integer and initialize it to 0 before you start adding to it.
- Logical Error: The
if/else block has its output messages swapped. When sum_of_numbers % 2 == 0 is true, it means the sum is even, but the code prints "Sum is odd." The same mistake is in the else block.
Corrected Code
Here is the corrected version of the code with comments highlighting the fixes.
#include <iostream>
// #include <exception> // This include is not used, so it can be removed.
using namespace std;
void check_sum_parity(int* arr, int arrayLength) {
// FIX 1: Declare and initialize the sum variable before using it.
int sum_of_numbers = 0;
for (int i = 0; i < arrayLength; i++) {
sum_of_numbers += arr[i];
}
// FIX 2: Correct the logic in the if/else block.
if (sum_of_numbers % 2 == 0) {
// If the remainder when divided by 2 is 0, the number is even.
cout << "Sum is even." << endl;
}
else {
// Otherwise, the number is odd.
cout << "Sum is odd." << endl;
}
}
int main(void) {
int arr[] = { 10, 2, 21, 50};
int arrayLength = sizeof(arr) / sizeof(int);
check_sum_parity(arr, arrayLength);
return 0; // Good practice to return 0 from main
}
Summary of Changes:
- Added
int sum_of_numbers = 0;: This line was added before the for loop to properly initialize the variable that will hold the sum.
- Swapped
cout Statements: The messages inside the if and else blocks were swapped to correctly report whether the sum is even or odd.
Output of the Corrected Code
For the given array { 10, 2, 21, 50 }, the sum is 10 + 2 + 21 + 50 = 83. Since 83 is an odd number, the corrected program will now correctly print:
Sum is odd.
|
| Model: |
gemini-2.5-pro |
| Temperature: |
0.2 |
| Max output tokens: |
8192 |
| TopK: |
40.0 |
| TopP: |
0.95 |
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-06-13 UTC.
[null,null,["Last updated 2026-06-13 UTC."],[],[]]