Can anyone check for me....



Last edited on
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
Arrays declared as array[n] have elements [0] to [n-1], not [1] to [n].

Defending Man: I don't see any integers.
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
Topic archived. No new replies allowed.