Jun 13, 2010 at 4:33am UTC
Last edited on Jun 13, 2010 at 11:05am UTC
Jun 13, 2010 at 5:06am UTC
I'm fairly new to this, but there is one thing I noticed right off the bat. You're using integers, which round to the nearest whole number. It looks like you want to have some decimal answers, so using something other than 'int' might be good.
Also, putting code symbols around your code would make it easier to read.
Last edited on Jun 13, 2010 at 5:08am UTC
Jun 13, 2010 at 6:32am UTC
Arrays declared as array[n]
have elements [0] to [n-1], not [1] to [n].
Defending Man: I don't see any integers.
Jun 13, 2010 at 6:53am UTC
Hello,
I think it's better:
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
#include<iostream>
using namespace std;
void CalculateCharges(double CustomerCarPark[], double *finalcharge)
{
double lN = 0;
for (int i = 0; i < 3; i++)
{
lN = CustomerCarPark[i];
finalcharge[i] = (lN <= 3 ? 2 : ((lN >= 19 && lN <= 24) ? 10 : (lN > 3 ? (2 + ((lN - 3) * 0.5)) : NULL)));
}
}
int main()
{
double CustomerCarPark[3], finalcharge[3], lOne = 0, lTwo = 0;
for (int i = 0; i < 3; i++)
{
cout << "Enter the hours parked for car no." << i + 1 << " : " ;
cin >> CustomerCarPark[i];
}
CalculateCharges(CustomerCarPark, finalcharge);
cout << "Car\t" << "Hours\t" << "Charge" << endl;
for (int i = 0; i < 3; i++)
{
cout << i + 1 << " \t" << CustomerCarPark[i] << " \t" << finalcharge[i] << endl;
lOne += CustomerCarPark[i];
lTwo += finalcharge[i];
}
cout << "Total " << lOne << "\t" << lTwo << endl;
return 0;
}
Or like this :)
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
#include<iostream>
double CalculateCharges(double lN)
{
double lFC = 0;
return (lFC = (lN <= 3 ? 2 : ((lN >= 19 && lN <= 24) ? 10 : (lN > 3 ? (2 + ((lN - 3) * 0.5)) : NULL))));
}
int main()
{
double lCCP[3], lFC[3], lA[2] = {0, 0};
for (int i = 0; i < 3; i++)
{
std::cout << "Enter the hours parked for car no." << i + 1 << " : " ;
std::cin >> lCCP[i];
lA[0] += lCCP[i];
}
std::cout << "Car\t" << "Hours\t" << "Charge" << std::endl;
for (int i = 0; i < 3; i++)
{
lA[1] += lFC[i] = CalculateCharges(lCCP[i]);
std::cout << i + 1 << " \t" << lCCP[i] << " \t" << lFC[i] << std::endl;
}
std::cout << "Total " << lA[0] << "\t" << lA[1] << std::endl;
return 0;
}
The results of the programme:
Enter the hours parked for car no.1 : 3
Enter the hours parked for car no.2 : 8
Enter the hours parked for car no.3 : 19
Car Hours Charge
1 3 2
2 8 4.5
3 19 10
Total 30 16.5
Press any key to continue . . .
Last edited on Jun 13, 2010 at 7:15am UTC