Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Greetings everyone, I have another question as follows:
CalculateStats: this method must calculate the lowest, highest and average sales
amount. The array of valid sales values is received as a method parameter, as well as, the
count of invalid values. The calculated lowest, highest and average amounts are made
available via out method parameters. A for loop must be used to do the calculations,
but only on the valid values in the array. To calculate the lowest, highest and average values, no built-inarray methods may be used. Thus, you must manually do the calculations. This method
must not contain any Console statements. Note: No try/catch blocks are required.

My problem is that the lowest is alwasy 0 because an invalid entry is also counted, how do I calculate only valid values? I have already dealt with handling these invalid values on a previous method. The problem now is everytime I run the program, lowest is always ==0

Here is my code:

C#
static void CalculateStats( decimal[] salesAmount,out decimal lowest, out decimal highest,int invalidEntries,out decimal average)
        {

             lowest = salesAmount[0];
             highest = salesAmount[0];
             average = 0;
             decimal totalSalesAmount = 0;


            for (int k = 0; k < salesAmount.Length; k++)
            {
                 if (salesAmount[k] < lowest)
                 {
                        lowest = salesAmount[k];
                 }

                if (salesAmount[k] > highest)
                {
                    highest = salesAmount[k];

                }
                totalSalesAmount += salesAmount[k];
            }

            average = totalSalesAmount / (salesAmount.Length-invalidEntries);
        }
Posted
Updated 20-Feb-13 9:44am
v3
Comments
Jibesh 19-Feb-13 14:45pm    
can you provide the sample 'salesAmount' array values you have tried.

Your method looks ok to me. value zero for lowest may be because you have an entry zero in salesAmount array. double check your input
fatand50 19-Feb-13 15:16pm    
Here are the ten values:
2; 12; r; 45; hello; 874.54; 200; g; K; 23
fatand50 19-Feb-13 15:21pm    
Here are the ten values:
2; 12; r; 45; hello; 874.54; 200; g; K; 23

Note that these values: r;hello;g and K are regarded as invalidEntries. So my problem is instead of lowest to be =2, it becomes 0 because of one of the invalid entries.
Jibesh 19-Feb-13 15:46pm    
Please use the reply link to the answer to post a comment so that I get an email notification and can reply to you immediately.

it looks like invalid entries are passed from outside.so instead passing number pass array of index having invalid values and this array length should match with saleAmount array

You haven't stated what defines a valid/invalid value.
In fact, the problem statement is a bit self-inconsistant in that the array is stated as containing "valid sales values", but you are instructed to use only the valid values of the array in the calculations.
If you know what the characteristic of an valid or invalid value is, then you can check for it inside the for loop:
C#
for (int k = 0; k < salesAmount.Length; k++)
{
  decimal amount = salesAmount[k];  // no need to repeat the indexing
  if (/* check if amount is valid */)
  {
    if (amount < lowest)
    {
        lowest = amount;
    }
 
    if (amount > highest)
    {
        highest = amount;
    }
    totalSalesAmount += amount;
  }
}

It seems unusual to provide the count of invalid values if you are expecte to determine which are valid yourself. There seems no point in providing that value, as you can determine it yourself in the loop.
 
Share this answer
 
v2
Comments
fatand50 19-Feb-13 14:59pm    
Okay let me rather explain what I mean, invalid values are counted and then stored to invalidEntries variable, a value is invalid if a user can enter a wrong format. For example a user is required to enter only numbers. But if the user enters let's say 'K' Visual C# regards that as 0 and I don't know why, I want to find a way to make Visual C# discard that 0. Because even if there's 5 as a lowest value, but a user entered 'Z' or "Hello" lowest always becomes 0.
Matt T Heffron 19-Feb-13 15:48pm    
The reason for the 0 if an invalid value depends on how you are parsing the input to a decimal.
The generally-preferred way to convert from a string to a number (int, decimal, double, ...) is to use the .TryParse() method.
This will inform you if the input string was not valid for the number type.
Using .Parse() or Convert.To... and catching a thrown exception is considered very poor style, and is inefficient.
fatand50 20-Feb-13 15:38pm    
Thank you Matt and yes I understand your point about the exception handling, for now we are told to only consider using the the conversion and then the try anc catch blocks. Otherwise you are so right. Thank you a lot. Stay blessed.
The info you have isn't brilliant - it doesn't tell you how to identify invalid values, so it is safe to assume that they are either all at the beginning or at the end - the latter is the most likely.
So all you have to do is change your loop termination condition from:
C#
k < salesAmount.Length
to:
C#
k < salesAmount.Length - invalidEntries
and ignore the invalid entries
 
Share this answer
 
Comments
fatand50 19-Feb-13 15:06pm    
Okay I think I'm the one to blame, I did not explain what are the invalid values. Well invalid values are when a user can enter a string or char, a user is required to enter only numbers. So my problem is everytime a user enters an invalid value, the variable lowest is always 0. For example if amounts are as follows: 3; 56; 123; 7 instead of the lowest to be 3, it always becomes 0 if there is an invalid value entered.
OriginalGriff 19-Feb-13 15:19pm    
If all you have to do is ignore invalid values and all invalid values are zero, then just test at the top of your loop:
for (int k = 0; ....
{
if (salesAmount[k] > 0)
{
// Your existing loop code
}
}
Matt T Heffron 19-Feb-13 15:43pm    
That's what I said... (Solution 1)
fatand50 20-Feb-13 15:42pm    
Oh yes Sir, that is why I also accepted the Solution. Thank you a lot.
fatand50 19-Feb-13 15:43pm    
When I add that statement I see a difference, at least now the invalid values are somehow discarded within the calculation of lowest value, however there are still some minor adjustments I have to do with my code. Thank you a lot all of you guys. Stay blessed.
here is my guess.

the input salesAmount array is read from a textBox where user type sales with 'semicoln' seperated value.

i.e string salesAmountString = "2; 12; r; 45; hello; 874.54; 200; g; K; 23";

so this is what you have to do.
C#
some method()
{
 string salesAmountString = "2; 12; r; 45; hello; 874.54; 200; g; K; 23";
 string []splitValue = salesAmountString.Split(';');

 List<decimal> salesList = new List<decimal>();

 foreach (string str in splitValue)
 {
     decimal parseValue;
     if (decimal.TryParse(str, out parseValue))
     {
       salesList.Add(parseValue);
       }
 }

 CalculateStats(salesList.ToArray(), out lowest, out highest, out average);
}

void CalculateStats(decimal[] salesAmount, out decimal lowest, out decimal highest, out decimal average)
{
 lowest = salesAmount[0];
 highest = salesAmount[0];
 average = 0;
 decimal totalSalesAmount = 0;

  for (int k = 0; k < salesAmount.Length; k++)
  {
    decimal amount = salesAmount[k];
 
    if (amount < lowest)
    {
       lowest = amount;
    }

    if (amount > highest)
    {
       highest = amount;
    }
      totalSalesAmount += amount;
  }
  average = totalSalesAmount / (salesAmount.Length);
}
 
Share this answer
 
Comments
fatand50 20-Feb-13 15:41pm    
Thank you Jibesh, this is just way to advanced for me. I will refer back to this one day. For now we are just introduced to basic concepts and we are also not allowed to use an built-in array methods for calculations. Stay blessed.
Jibesh 21-Feb-13 2:33am    
You are welcome. there isnt much code except the string parsing.

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