Code Generator | C/C++

C++ Animal Structure Manipulation

This example demonstrates the creation and manipulation of an Animal structure in C++. It initializes an array of Animal objects, displays their names, and modifies the weight of one animal with output before and after the change.


Empty image or helper icon

Prompt

1. Создать структуру Animal. Добавить в структуру следующие поля,
характеризующие животное: возраст(int), название(string) и вес(double).
2. В функции main создать пять объектов Animal, вывести на экран только
названия животных
3. В функции main изменить вес одного из животных. Вывести на экран вес “до” и
“после” изменения.

Answer

Creating and Manipulating an Animal Structure in C++

Below is a well-documented and robust C++ code snippet to achieve the required tasks:

Necessary Imports

#include 
#include 

Animal Structure Definition

/**
 * @struct Animal
 * @brief A structure to represent an animal with age, name, and weight.
 *
 * @var Animal::age Age of the animal.
 * @var Animal::name Name of the animal.
 * @var Animal::weight Weight of the animal.
 */
struct Animal {
    int age;
    std::string name;
    double weight;
};

Main Function

int main() {
    // Creating an array to store 5 Animal objects
    Animal animals[5] = {
        {5, "Elephant", 1200.5},
        {3, "Tiger", 200.3},
        {1, "Kangaroo", 85.7},
        {2, "Penguin", 15.2},
        {4, "Zebra", 300.0}
    };

    // Output only the names of the animals
    std::cout << "Animal names:" << std::endl;
    for (int i = 0; i < 5; ++i) {
        std::cout << animals[i].name << std::endl;
    }

    // Changing the weight of the third animal (Kangaroo)
    double oldWeight = animals[2].weight;
    animals[2].weight = 95.5;

    // Output the weight before and after the change
    std::cout << "\nWeight of " << animals[2].name << " before: " << oldWeight << " kg" << std::endl;
    std::cout << "Weight of " << animals[2].name << " after : " << animals[2].weight << " kg" << std::endl;

    return 0;
}

Input Validation Note

For this specific scenario and given that the data is hardcoded, input validation is not required. In case of dynamic input from the user, additional checks should be added to validate the correctness of the input values.

Commentary

  • Imports: We include <iostream> for input-output operations and <string> to handle the string data type.
  • Animal Structure: Defined with three fields: age, name, and weight to represent essential attributes of an animal.
  • Main Function:
    • Initializes an array of five Animal objects.
    • Loops through the array to print the names of the animals.
    • Changes the weight of the third animal and prints the weight before and after the change.

Code Usage Example

To run this code, you would typically save it in a file named main.cpp, then compile and execute it using a C++ compiler.

g++ -o main main.cpp
./main

Output:

Animal names:
Elephant
Tiger
Kangaroo
Penguin
Zebra

Weight of Kangaroo before: 85.7 kg
Weight of Kangaroo after : 95.5 kg

This efficiently demonstrates the creation, manipulation, and display of structured data in C++. For more advanced data structures and algorithms, consider taking advanced courses on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This example demonstrates the creation and manipulation of an Animal structure in C++. It initializes an array of Animal objects, displays their names, and modifies the weight of one animal with output before and after the change.