Functions and Arrays
Mar 5, 2012 at 3:05am UTC
I am supposed to modify my current program to such that the user has the choice of entering either 5 males, 5 females, or quitting (the while loop should be retained to repeat the menu until the user chooses to quit. The program also needs to have a function that prints a header (return type of void). It also needs a function that calculates the average height of all 5. I have no idea where to start other than the function is
double average (int ht0, int ht1, int ht2, int ht3, int ht4);
This is the code I already have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
using namespace std;
int main()
{
char sex;
double height;
int weight;
int name;
bool b=true ;
do
{
cout << "Please choose from the following..\n\nA = Data for Female\nB = Data for Male\n\nC = choose to quit" << endl;
cin >> sex;
sex = toupper(sex);
if (sex =='C' )
{
cout << "Good-bye!" << endl;
b=false ;
system("pause" );
}
if (sex == 'A' )
{
cout << "Enter Name" << endl;
cin >> name;
cout << "Enter height in inches" << endl;
cin >> height;
cout << "Enter weight in pounds" << endl;
cin >> weight;
if ((height >= 58) || (height <= 64))
{ if (weight <= 114)
{ cout << "Weight is low!" << endl; }
else if ((weight >= 115) || (weight <= 133))
{ cout << "Weight is on target" << endl; }
else
{ cout << "Weight is high!" << endl; }
}
else if ((height >= 65) || (height >= 71))
{
if (weight <= 117)
{
cout << "Weight is low!" << endl;
}
else if ((weight >= 136) && (weight <= 156))
{
cout << "Weight is on target!" << endl;
}
else
{
cout << "Weight is high!" << endl;
}
}
}
else if (sex == 'B' )
{
cout << "Enter height in inches" << endl;
cin >> height;
cout << "Enter weight in pounds" << endl;
cin >> weight;
if ((height >= 61) || (height <= 67))
{
if (weight <= 114)
{
cout << "Weight is low!" << endl;
}
else if ((weight >= 123) || (weight <= 133))
{
cout << "Weight is on target!" << endl;
}
else
{
cout << "Weight is high!" << endl;
}
}
else if ((height >= 68) && (height <= 75))
{
if (weight <= 155)
{
cout << "Weight is low!" << endl;
}
else if ((weight >= 156) || (weight <= 175))
{
cout << "Weight is on target!" << endl;
}
else
{
cout << "Weight is high!" << endl;
}
}
}
} while (b==true );
}
Mar 5, 2012 at 4:55am UTC
You basically need to just loop through an array, increase the average weight each time, and then divide by the total number of people at the end. I didn't test this yet, but it should work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include <iostream>
using namespace std;
struct Person
{
char sex[7];
float height;
int weight;
char * name;
};
int main()
{
float averageheight = 0.0f;
int averageweight = 0;
Person* people = new Person[5];
for (int i = 0; i < sizeof (people); i++)
{
cout << "Please choose Male, Female or Quit." << endl;
cin >> people[i].sex;
for (int j = 0; j < sizeof (people[i].sex); j++)
{
people[i].sex[j] = (char )toupper((int )people[i].sex[j]);
}
if (people[i].sex == "QUIT" )
{
cout << "Good-bye!" << endl;
break ;
}
else if (people[i].sex == "MALE" )
{
cout << "What is his name?" << endl;
cin >> people[i].name;
cout << "What is his height in inches?" << endl;
cin >> people[i].height;
cout << "What is his weight in pounds?" << endl;
cin >> people[i].weight;
if ((people[i].height >= 58) || (people[i].height <= 64))
{
if (people[i].weight <= 114)
cout << "Weight is low!" << endl;
else if ((people[i].weight >= 115) || (people[i].weight <= 133))
cout << "Weight is on target!" << endl;
else
cout << "Weight is high!" << endl;
}
else if ((people[i].height >= 65) || (people[i].height >= 71))
{
if (people[i].weight <= 117)
cout << "Weight is low!" << endl;
else if ((people[i].weight >= 136) && (people[i].weight <= 156))
cout << "Weight is on target!" << endl;
else
cout << "Weight is high!" << endl;
}
}
else if (people[i].sex == "FEMALE" )
{
cout << "What is her name?" << endl;
cin >> people[i].name;
cout << "What is her height in inches?" << endl;
cin >> people[i].height;
cout << "What is her weight in pounds?" << endl;
cin >> people[i].weight;
if ((people[i].height >= 61) || (people[i].height <= 67))
{
if (people[i].weight <= 114)
cout << "Weight is low!" << endl;
else if ((people[i].weight >= 123) || (people[i].weight <= 133))
cout << "Weight is on target!" << endl;
else
cout << "Weight is high!" << endl;
}
else if ((people[i].height >= 68) || (people[i].height >= 75))
{
if (people[i].weight <= 155)
cout << "Weight is low!" << endl;
else if ((people[i].weight >= 156) && (people[i].weight <= 175))
cout << "Weight is on target!" << endl;
else
cout << "Weight is high!" << endl;
}
}
averageheight += people[i].height;
averageweight += people[i].weight;
if (i == sizeof (people) - 1)
{
averageheight /= sizeof (people);
averageweight /= sizeof (people);
cout << "The average weight is " << averageweight << ", and the average height is " << averageheight << "." ;
}
}
system("pause" );
return 0;
}
Mar 5, 2012 at 11:15am UTC
Obviously. If you use his code I would suggest allocating some memory for the
name variable of each
Person to point to before trying to store stuff where it points. Better yet, use a std::string.
I have no idea where to start other than the function is double average (int ht0, int ht1, int ht2, int ht3, int ht4);
Since you're going to be dealing with arrays:
double average( int values[], int n_values);
might be more appropriate.
Topic archived. No new replies allowed.