Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS-116 - C++ Programming I Create a structure that will contain information abou

ID: 3862994 • Letter: C

Question

 CS-116 - C++ Programming I   Create a structure that will contain information about animals. The information includes:    - a name (a string up to 20 characters long)   - a type (up to 10 characters long)   - an age (integer)  Your program should create an array to hold animal information. The maximum number of entries in the array should be 25.  The program should begin by prompting the user for the name of a file (filenames can be up to 32 characters long). Open the file and read animal information from the file into the array of structures.  If the file open fails, display an error message and terminate the program.  The file format is as follows:    - there is one line of data in the file for each animal.   - each line of data will contain the animal's name, type, and age in     successive fields separated by whitespace.   - each line will have a carriage return at the end of the line     except the last line in the file.  Here is a sample file:    Goofy dog 8   Minnie mouse 22   Daisy duck 18   Mickey mouse 25   Pluto dog 10   Donald duck 21  Note: you can assume that names and types in the file are all single words.  Once your program has read the file contents into the array of structures, the following menu should appear:    Menu:     1 -  display all the animals in forward order     2 -  display all the animals in reverse order     3 -  display all animals sorted by age (youngest first)     4 -  determine the average animal age     5 -  determine which animal is oldest     6 -  determine which animal is youngest     q -  quit the program   Choose:  *** Note: the menu options MUST be numbers 1-6 and the           letter 'q'.  Do not deviate from this.  Here's a sample output for each option given the data file presented above:  Option 1 - this is the order that the animal information            appears in the file.    Name                  Type        Age   --------------------  ----------  ---   Goofy                 dog         8   Minnie                mouse       22   Daisy                 duck        18   Mickey                mouse       25   Pluto                 dog         10   Donald                duck        21   Option 2 - this is the reverse order that the animal            information appears in the file.    Name                  Type        Age   --------------------  ----------  ---   Donald                duck        21   Pluto                 dog         10   Mickey                mouse       25   Daisy                 duck        18   Minnie                mouse       22   Goofy                 dog         8   Option 3 - this is ascending order based on the            age for each animal.    Name                  Type        Age   --------------------  ----------  ---   Goofy                 dog         8   Pluto                 dog         10   Daisy                 duck        18   Donald                duck        21   Minnie                mouse       22   Mickey                mouse       25   Option 3:    The average age is 17.3 years.   Option 4:    Mickey is the oldest.   Option 5:    Goofy is the youngest.   Option q:    The program exits   Anything else:    An error message should be displayed.   After a menu option is selected and the appropriate action is taken, the menu should be redisplayed. This should continue until the user selects option 'q' and the program exits.   For this assignment do NOT use global variables (unless they are prefixed with "const").  Implement your programs using functions that pass parameters and return values. There are lots of opportunities to use functions to break this assignment down into small units of functionality.  There will also be some opportunities to use functions to prevent duplicating algorithms. These are things I'll be specifically looking for when grading this assignment.  Your program should be flexible enough to handle a different numbers of animals in the data file up to the maximum of 25.  You are to use #define constant values for the length of the file (you can also define this as a "const int" in the global scope -- the only allowed type of global!), string lengths (for the name and type), and for the maximum number of animals.  You can assume that there are no errors in the contents of the data file. It will follow the rules described above.  You will find a sample executable and sample data file for this assignment on the assignments web page.  You are NOT allowed to use the std::string datatype defined in the <string> header.  All strings needed for this assignment should be implemented with null-terminated character arrays (c-strings).  What to include in your source code:   - correct functionality   - good coding style (commenting, white space, ...)   - good use of functions to break your program     into logical chunks and/or to eliminate duplicate     code   - proper use of streams, including clean formatting     of the output   - proper use of arrays (including     passing them as parameters to functions)   - proper use of structures   - no recursion   - no global variables  Recommendation #1: design this program before trying to write the code. Use a top-down design approach ... it will work very well for this assignment. Use me as a sounding-board for your design if you'd like.  Recommendation #2: check out the FAQ for some suggestions on how to approach this assignment. 

Explanation / Answer

/*Animal.CPP */

#include<iostream>

#include<fstream>

#include<string.h>

using namespace std;

void forward_order();

void reverse_order();

void sorted_by_age();

void average_animal_age();

void oldest_animal();

void youngest_animal();

struct Animal

{

char name[20];

char type[10];

int age;

}an[25];

int main()

{

ifstream infile;

infile.open("animal.txt");

if(infile.is_open()){

for(int i=0;i<25;i++){

infile>>an[i].name;

infile>>an[i].type;

infile>>an[i].age;

}

char ch='';

do{

cout<<" Menu:"<<endl;

cout<<"1 - display all the animals in forward order"<<endl;

cout<<"2 - display all the animals in reverse order"<<endl;

cout<<"3 - display all animals sorted by age (youngest first)"<<endl;

cout<<"4 - determine the average animal age"<<endl;

cout<<"5 - determine which animal is oldest"<<endl;

cout<<"6 - determine which animal is youngest"<<endl;

cout<<"q - quit the program"<<endl;

cout<<"Choose:";

cin>>ch;

switch(ch){

case '1':

forward_order();

break;

case '2':

reverse_order();

break;

case '3':

sorted_by_age();

break;

case '4':

average_animal_age();

break;

case '5':

oldest_animal();

break;

case '6':

youngest_animal();

break;

case 'q':

cout<<"The program exits"<<endl;

break;

default:

cout<<" Invalid input character.please re-enter"<<endl;

}

}while(ch != 'q');

}

return 0;

}

void forward_order(){

cout<<" Name Type Age"<<endl;

cout<<"------------------ ---------- ---"<<endl;

for(int i=0;i<25;i++)

cout<<an[i].name<<" "<<an[i].type<<" "<<an[i].age<<endl;

}

void reverse_order(){

cout<<" Name Type Age"<<endl;

cout<<"------------------ ---------- ---"<<endl;

for(int i=24;i>=0;i--)

cout<<an[i].name<<" "<<an[i].type<<" "<<an[i].age<<endl;

}

void sorted_by_age(){

Animal a[25],temp;

int i,j;

for(i=0;i<25;i++)

a[i] = an[i];

for(i=0;i<25;i++){

for(j=i+1;j<25;j++){

if(a[i].age > a[j].age){

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

cout<<" Name Type Age"<<endl;

cout<<"------------------ ---------- ---"<<endl;

for(int i=0;i<25;i++)

cout<<a[i].name<<" "<<a[i].type<<" "<<a[i].age<<endl;

}

void average_animal_age(){

float sum = 0,avg = 0;

for(int i=0;i<25;i++)

sum += an[i].age;

avg = sum /25;

cout<<" The average age is"<<avg<<"years."<<endl;

}

void oldest_animal(){

char name[20];

int max = 0,i;

for(i=0;i<25;i++)

if(an[i].age > max){

max = an[i].age;

strcpy(name,an[i].name);

}

cout<<name<<" is the oldest."<<endl;

}

void youngest_animal(){

char name[20];

int min = 999,i;

for(i=0;i<25;i++)

if(an[i].age < min){

min = an[i].age;

strcpy(name,an[i].name);

}

cout<<name<<" is the youngest."<<endl;

}

/* animal.txt */

Goofy dog 8

Minnie mouse 22

Daisy duck 18

Mickey mouse 25

Pluto dog 10

Donald duck 21

Snoopy dog 24

Botcha mouse 17

Dumra duck 16

Mippe mouse 14

Chuntra dog 15

Chocko mouse 19

Chasi dog 17

Erond duck 15

Emura dog 27

Pamiso mouse 26

Paltra dog 18

Dalgo mouse 16

Satlu duck 21

Wrond mouse 23

Ramta dog 10

Godght mouse 13

Dotulra duck 6

Hundre mouse 17

Luckda dog 18

/*Sample Input and output*/

Menu:

1 - display all the animals in forward order

2 - display all the animals in reverse order

3 - display all animals sorted by age (youngest first)

4 - determine the average animal age

5 - determine which animal is oldest

6 - determine which animal is youngest

q - quit the program

Choose:1

Name Type Age

------------------ ---------- ---

Goofy dog 8

Minnie mouse 22

Daisy duck 18

Mickey mouse 25

Pluto dog 10

Donald duck 21

Snoopy dog 24

Botcha mouse 17

Dumra duck 16

Mippe mouse 14

Chuntra dog 15

Chocko mouse 19

Chasi dog 17

Erond duck 15

Emura dog 27

Pamiso mouse 26

Paltra dog 18

Dalgo mouse 16

Satlu duck 21

Wrond mouse 23

Ramta dog 10

Godght mouse 13

Dotulra duck 6

Hundre mouse 17

Luckda dog 18

Menu:

1 - display all the animals in forward order

2 - display all the animals in reverse order

3 - display all animals sorted by age (youngest first)

4 - determine the average animal age

5 - determine which animal is oldest

6 - determine which animal is youngest

q - quit the program

Choose:2

Name Type Age

------------------ ---------- ---

Luckda dog 18

Hundre mouse 17

Dotulra duck 6

Godght mouse 13

Ramta dog 10

Wrond mouse 23

Satlu duck 21

Dalgo mouse 16

Paltra dog 18

Pamiso mouse 26

Emura dog 27

Erond duck 15

Chasi dog 17

Chocko mouse 19

Chuntra dog 15

Mippe mouse 14

Dumra duck 16

Botcha mouse 17

Snoopy dog 24

Donald duck 21

Pluto dog 10

Mickey mouse 25

Daisy duck 18

Minnie mouse 22

Goofy dog 8

Menu:

1 - display all the animals in forward order

2 - display all the animals in reverse order

3 - display all animals sorted by age (youngest first)

4 - determine the average animal age

5 - determine which animal is oldest

6 - determine which animal is youngest

q - quit the program

Choose:3

Name Type Age

------------------ ---------- ---

Dotulra duck 6

Goofy dog 8

Ramta dog 10

Pluto dog 10

Godght mouse 13

Mippe mouse 14

Erond duck 15

Chuntra dog 15

Dalgo mouse 16

Dumra duck 16

Chasi dog 17

Botcha mouse 17

Hundre mouse 17

Paltra dog 18

Daisy duck 18

Luckda dog 18

Chocko mouse 19

Satlu duck 21

Donald duck 21

Minnie mouse 22

Wrond mouse 23

Snoopy dog 24

Mickey mouse 25

Pamiso mouse 26

Emura dog 27

Menu:

1 - display all the animals in forward order

2 - display all the animals in reverse order

3 - display all animals sorted by age (youngest first)

4 - determine the average animal age

5 - determine which animal is oldest

6 - determine which animal is youngest

q - quit the program

Choose:4

The average age is17.44years.

Menu:

1 - display all the animals in forward order

2 - display all the animals in reverse order

3 - display all animals sorted by age (youngest first)

4 - determine the average animal age

5 - determine which animal is oldest

6 - determine which animal is youngest

q - quit the program

Choose:5

Emura is the oldest.

Menu:

1 - display all the animals in forward order

2 - display all the animals in reverse order

3 - display all animals sorted by age (youngest first)

4 - determine the average animal age

5 - determine which animal is oldest

6 - determine which animal is youngest

q - quit the program

Choose:6

Dotulra is the youngest.

Menu:

1 - display all the animals in forward order

2 - display all the animals in reverse order

3 - display all animals sorted by age (youngest first)

4 - determine the average animal age

5 - determine which animal is oldest

6 - determine which animal is youngest

q - quit the program

Choose:q

The program exits