I'm currently working on a c# assignment. I need to create a function that cross-checks two different arrays according to multiple conditions and changes the incorrect value of the array element into a correct one. I also need the function to return a bool true if there were an incorrect value or values (aka. element change) and false if there weren't.
The two arrays used is as follows:
int[] ticketSold = {0, 3, 5, 10, 34, 66, 89, 100, 101, 321, 657, 1000, 1032, 4520, 6578};
double[] revenue = {0, 1.50, 2.50, 10000, 84, 116, 139, 150, 1101, 1321, 1657, 6000, 2064, 9040, 13156};
The four different conditions for correct values for elements within the revenue array are as follows:
Case1: 0 <= ticketSold[i] <= 10 revenue[i] = ticketSold[i] / 2
Case2: 10 < ticketSold[i] <= 100 revenue[i] = ticketSold[i] + 50
Case3: 100 < ticketSold[i] <= 1000 revenue[i] = ticketSold[i] + 5000
Case4: ticketSold[i] > 1000 revenue[i] = ticketSold[i] * 2
The current function that I made looks like this. Since the if-else statement inside a for loop cannot return multiple boolean values, I instead created a separate bool array 'isChangeRequired' to store whether or not each revenue elements requires a fix. Every time a change is required the bool array receives a true value, and every time it is not required a false value.
Since the fourth element of the revenue array does not match the condition and requires a change, FixElements function should return true. However, when I execute the code it does not return true.
public static bool FixElement(int[] ticketSold, double[] revenue)
{
if (ticketSold.Length == revenue.Length && ticketSold.Length != 0)
{
bool[] isChangeRequired = new bool[ticketSold.Length];
for (int i = 0; i < ticketSold.Length; i++)
{
if (ticketSold[i] >= 0 && ticketSold[i] <= 10 && revenue[i] != ticketSold[i] / 2)
{
revenue[i] = ticketSold[i] / 2;
isChangeRequired[i] = true;
}
else if (ticketSold[i] > 10 && ticketSold[i] <= 100 && revenue[i] != ticketSold[i] + 50)
{
revenue[i] = ticketSold[i] + 50;
isChangeRequired[i] = true;
}
else if (ticketSold[i] > 100 && ticketSold[i] <= 1000 && revenue[i] != ticketSold[i] + 5000)
{
revenue[i] = ticketSold[i] + 5000;
isChangeRequired[i] = true;
}
else if (ticketSold[i] > 1000 && revenue[i] != ticketSold[i] * 2)
{
revenue[i] = ticketSold[i] * 2;
isChangeRequired[i] = true;
}
else
{
revenue[i] = revenue[i];
isChangeRequired[i] = false;
}
}
if (Array.Exists(isChangeRequired, element => element == true))
{
return true;
}
}
return false;
}
Is there sth missing from my code? Also, is there a better way to write the code in a switch statement? I cannot use linq.
Aucun commentaire:
Enregistrer un commentaire