For this lab you will be creating your own struct, called Frog. You will need to create an array of Frog, and prompt the user for information to create Frogs and fill in the array.

(3 pts) First, create a Frog struct (first test). It should have the following fields: Name, Age, and Leg Length. Expect sample input to look like this:

Todd Jones //Name
12 //Age
12.34 //LegLength
(1 pt) After that, ask the user how many frogs they would like to create:

How many frogs do you want?
(2 pts) Next, loop for the supplied number of times, asking for user input:

What is the name of Frog 1?
What is the age of Frog 1?
What is the leg length of Frog 1?
What is the name of Frog 2?
What is the age of Frog 2?
What is the leg length of Frog 2?

Respuesta :

Answer:

The solution code is written in c++

  1. #include <iostream>
  2. using namespace std;
  3. struct Frog {
  4.  string Name;
  5.  int Age;
  6.  float LegLength;
  7. };
  8. int main ()
  9. {
  10.   Frog frog_test;
  11.   frog_test.Name = "Todd Jones";
  12.   frog_test.Age = 12;
  13.   frog_test.LegLength = 12.34;
  14.  
  15.   int num;
  16.   cout<<"Enter number of frogs: ";
  17.   cin>>num;
  18.  
  19.   Frog frog[num];
  20.  
  21.   int i;
  22.   for(i = 0; i < num; i++){
  23.      
  24.       cout<<"What is the name of Frog "<< i + 1 <<" ?";
  25.       cin>> frog[i].Name;
  26.      
  27.       cout<<"What is the age of Frog "<< i + 1 <<" ?";
  28.       cin>> frog[i].Age;
  29.      
  30.       cout<<"What is the leg length of Frog "<< i + 1 <<" ?";
  31.       cin>> frog[i].LegLength;
  32.   }
  33. }

Explanation:

Firstly, define a Frog struct as required by the question (Line 4 - 8).

We create a test Frog struct (Line 12 - 15).

If we run the code up to this point, we shall see a Frog struct has been build without error.

Next, prompt user to input number of frogs desired (Line 17 -19).

Use the input number to declare a frog array (Line 21).

Next, we can use a for loop to repeatedly ask for input info of each frog in the array (Line 23- 34).

At last, we shall create num elements of Frog struct in the array.