Demo - Dog ages

// program has an array of strings (dog names) and an additional array // of their respective ages - this will be a parallel array //we will prompt the user for ages and then show the //user the ages, names in human years #include <iostream> using namespace std; const int ARRAY_SIZE = 3; void getDogInfo(string[], int[]); void showDogInfo(string[],int[]); int humanYears(int); int main() { //1 create a string array of dog namesdogNam string dogName[ARRAY_SIZE]; //2 create a int array for dog ages int dogAges[ARRAY_SIZE]; //3 prompt user for name and age getDogInfo(dogName, dogAges); //4 display: name, age, age of dog in human years showDogInfo(dogName, dogAges); return 0; } //FUNCTIONS **************** //create a function to populate the name and age of the dog in the array void getDogInfo(string pName[], int pAge[]) { for(int i = 0; i < ARRAY_SIZE; i++) { // ask user for dogs name cout << "Enter the dog's name: "; cin >> pName[i]; //ask user for age cout << "Enter dog's age: "; cin >> pAge[i]; } } void showDogInfo(string pName[], int pAge[]) { //loop through the dogs to display name and age for(int i = 0; i < ARRAY_SIZE; i++) { cout << pName[i] << " is " << pAge[i] << " years old" << endl; cout << "In human years, that is " << humanYears(pAge[i]) << endl; } } //Function returns dog years to human years int humanYears(int pAge) { return pAge * 7; }

This C++ program is designed to take information about dogs (their names and ages) as input from the user, store this data in two parallel arrays, and then output the dog's name, age in dog years, and equivalent age in human years. The program uses three functions to achieve this: getDogInfo(), showDogInfo(), and humanYears().

Here's how it works:

  1. const int ARRAY_SIZE = 3;: The program begins by defining a constant, ARRAY_SIZE, set to 3. This will be the number of dogs the program will ask for information about.

  2. In main(), two arrays are created: dogName (of type string) and dogAges (of type int). These will store the names and ages of the dogs, respectively.

  3. getDogInfo(dogName, dogAges); calls a function that prompts the user to enter the name and age for each dog, storing these in the appropriate arrays.

  4. showDogInfo(dogName, dogAges); calls a function that prints out the name and age of each dog, as well as their age in human years.

  5. The getDogInfo() function uses a for loop to prompt the user for each dog's name and age. This data is read from the console and stored in the passed arrays.

  6. The showDogInfo() function also uses a for loop, but this time to output each dog's name and age, as well as their age in human years. It does this by calling the humanYears() function with the dog's age as an argument.

  7. The humanYears() function is very straightforward; it simply multiplies the given age by 7 (since one dog year is roughly equivalent to seven human years) and returns the result.

Finally, the program returns 0 to indicate successful execution

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark