Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If i have been given 3 arrays, how do i pass these arrays through a function?

e.g

int CarID = { 1041, 7930, 3382, 2251, 1220, 7378, 9065, 9435 };
double CarValue = { 4252, 23001, 14500, 3000, 950, 4200, 1500, 10000 };
int DaysSincePayment= { 23, 35, 2, 14, 7, 51, 1, 45 };

They need to be passed into a function to give a car either 3% interest charge if the days since payment is under 30 or 6% interest if the days since payment is over 30.

C++
using namespace std;
int counter = 0;

struct Car
{
    int CarIdentifier;
    double Value;
    int DaysOverdue;
    double Interest;
};

Car database[8] =
{ { 1001, 9000, 20 },
{ 7940, 11000, 35 },
{ 4382, 14000, 2 },
{ 2651, 4000, 14 },
{ 3020, 3850, 5 },
{ 7168, 7000, 360 },
{ 6245, 12000, 1 },
{ 9342, 10500, 45 } };

double CalcCharge(int a, int b)
{
    double interest;
    if ((a > 10000) || (b > 30))
    {
        interest = a * 0.06;
        return interest;
    }
    else
    {
        interest = a * 0.03;
        return interest;
    }
}

int main()
{

    int counter = 0;

    while (counter != 8)
    {
        CalcCharge(database[counter].CarIdentifier, database[counter].DaysOverdue);
        counter++;
    }

    cout << "Car ID" << setw(10) << "Value" << setw(15) << "Interest Paid" << endl;

    for (int i = 0; i != 8; i++)
    {
        cout << database[i].CarIdentifier << setw(10) << database[i].Value << setw(10) << database[i].Interest << endl;
    }

    return 0;
}


What I have tried:

Using a function of two conditions e.g. func(int a, int b) and using a if < 30 and also an if >30 statement and returning the value however no output is received as i do not understand how to handle arrays in this way.
Posted

1 solution

Quote:
CalcCharge(database[counter].CarIdentifier, database[counter].DaysOverdue);
You are throwing away the return value of the function.

Possibly you meant:
C++
database[counter].Interest = CalcCharge(database[counter].CarIdentifier, database[counter].DaysOverdue);
 
Share this answer
 
Comments
Member 12319159 10-Feb-16 13:42pm    
i managed to get a value output with your code however the numbers are way off, any idea?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900