Question: MORE BARNYARD ANIMALS In this assignment we write a program that stores 5 strings in a string array, and 5 ints in an integer array

MORE BARNYARD ANIMALS

In this assignment we write a program that stores 5 strings in a string array, and 5 ints in an integer array and then writes the contents of the two arrays to the console.

Copy the following program into a C++ file, save it as animals2.cpp, and then compile and run the starter program to make sure you copied it correctly.

#include  using namespace std; int main() { // Enter your code here return 0; } 

At the top of main declare a string array named animals of size 5.

Note that you should store the size of the array in a separate constant variable:

In other words, you should now have two variables like these at the top of main:

const int SIZE = 5; string animals[SIZE];

Then, let's declare an integer array of size 5 named number_animals.

Store the following values in the string array, one at each index:

"sheep"

"donkeys"

"goats"

"geese"

"cats"

Remember: you can do this one of two ways:

Option 1: static initialization:

string animals[SIZE] = {"sheep", "donkeys", "goats", "geese", "cats"};

Option 2: Non-static initialization:

string animals[SIZE];

animals[0] = "sheep";

animals[1] = "donkeys";

//What goes here?

Pick ONE of the above options to store the string values in your string array.

Next, pick one option to store the following values in the integer array (number_animals), one at each index:

5 3 8 2 4

We are now going to write the contents of our arrays to the console.

First, inside of main, add the following cout statement:

cout << "Other animals at our farm" << endl;

Next, write a for loop that prints out the the two arrays.

Your for loop should print the arrays to the console, side-by-side, like this:

5 sheep

3 donkeys

8 goats

2 geese

4 cats

See the class notes if you get stuck for an example of using for loops with arrays.

When you have written your for loop, compile and run your code and verify that the following output appears in the console window:

Other animals at our farm 5 sheep 3 donkeys 8 goats 2 geese 4 cats

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!