C++ Help - I can't seem to figure out the last part of this code
I'm trying to get the program to display the current height of the child and the estimated height of the child.
I have it displaying the before and after for the first child but can't get it to display the estimated height for the rest of the children.
I would be very grateful if anyone could help me figure this out. Thanks!
开发者_如何转开发Here is the code that I have:
#include <iostream>
using namespace std;
int main()
{
double height [10];
double chgHeight [10];
for (int x = 0; x < 10; x = x + 1)
{
height[x] = 0.0;
chgHeight[x] = 0.0;
}
cout << "You will be asked to enter the heights of ten children."<< endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << "Enter height of child " << endl;
cin >> height[x];
}
chgHeight[0] = height[0] * .05 + height[0];
for (int x = 0; x < 10; x = x + 1)
{
cout << "Child " << x+1 << ": Current " << height[x] << " Expected "<< chgHeight[x] << endl;
}
system("pause");
return 0;
}
chgHeight[0] = height[0] * .05 + height[0];
You're only ever setting the first child's chgHeight
.
Edit:
For your output, you're going through the array or heights, which is indexed by the child number (x
):
for (int x = 0; x < 10; x = x + 1)
{
cout << "Child " << x+1 << ": Current " << height[x]
<< " Expected "<< chgHeight[x] << endl;
}
Your estimated height is calculated from the child's current height, which you have in this loop ( height[x]
). So, you have everything you need right there to output the estimated height.
If you have no need to save the calculation for later, there's really no need to create a second chgHeight[]
array in your code; just calculate and output the estimated height in that loop for each child.
You are not setting the estimated height for the rest of the children, only the first:
chgHeight[0] = height[0] * .05 + height[0];
Put that in a loop.
chgHeight[0] = height[0] * .05 + height[0];
This line only calculates the estimated height of the first child. You'll need to put this in a loop as well (changing the indices to your loop variable) to calculate all 10.
精彩评论