Hi, I'm working on an assignment and I have been able to complete most of it. The only problem I am having is the total weight, which should be 26.56, but it outputs 25.00. I see that it is considering the numbers as integers, but I've defined totalWeight as a double. So I know I'm missing something, I just don't know what. The inputs are 4.6, 9.4, 3.56, and 9.0.
And the output I am currently receiving is:
Enter the weight of Steak 1: 4.6
Enter the weight of Steak 2: 9.4
Enter the weight of Steak 3: 3.56
Enter the weight of Steak 4: 9.0
Mr. Slate's steak weight is 3.56
Pebble's steak weight is 9.40
The total weight of all steaks is 25.00
Here's the code.
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
|
#include <iostream>
using namespace std;
double maxWeight(double steaks[], int numSteaks)
{
int i;
double max;
max = steaks[0];
for (i = 0; i < numSteaks; i++)
{
if (max < steaks[i])
max = steaks[i];
}
return max;
}
double minWeight(double steaks[], int numSteaks)
{
int i;
double min;
min = steaks[0];
for (i = 0; i < numSteaks; i++)
{
if (min > steaks[i])
min = steaks[i];
}
return min;
}
double totalWeight(double steaks[], int numSteaks)
{
int i, sum = 0;
for (i = 0; i < numSteaks; i++)
{
sum += steaks[i];
}
return sum;
}
int main()
{
double steaks[4];
int numSteaks = 4, i;
for (i= 0; i < numSteaks; i++)
{
cout << "Enter the weight of Steak " << i + 1 << ": ";
cin >> steaks[i];
}
cout << "Mr. Slate's steak weight is ";
printf("%.2lf", minWeight(steaks, numSteaks));
cout << endl;
cout << "Pebble's steak weight is ";
printf("%.2lf", maxWeight(steaks, numSteaks));
cout << endl;
cout << "The total weight of all steaks is ";
printf("%.2lf", totalWeight(steaks, numSteaks));
cout << endl;
return 0;
}
|