-3

I'm going through some practice for arrays (let me specify this is NOT homework i just completed a test which i know in my soul i failed and i'm practicing) and performing functions on them and I'm running into this problem. My method for finding highest number, average, and total works but, find the lowest number isn't and I'm honestly stuck as to why.

To explain my array: index 0 is where i want to store my highest number index 1 is where i want to store my lowest number

my method starts at index 2 to find lowest / highest numbers since the lowest / highest would be store in the first two element of the array. my array size also takes this into concideration as well as the file reader, they all start at index 2.

I've also tried value returning methods while still getting the same end result.

Below are the methods i've tried that aren't putting out the desired information:

this method assigns highest to salesArray[0] and lowest to salesArray1, and accomodates array size and array read starts at 2.

private void Lowest()
{
    //method variables
    double lowest = salesArray[1];

    //find lowest sale
    for (int index = 2; index < salesArray.Length; index++)
    {
        if(salesArray[index] < lowest)
        {
            lowest = salesArray[index];
        }
    }

This way I used making both highest/lowest index 0 to try what was suggest in a previous comment:

private void Lowest()
    {
        //method variables
        double lowest = salesArray[0];

        //find lowest sale
        for (int index = 1; index < salesArray.Length; index++)
        {
            if(salesArray[index] < lowest)
            {
                lowest = salesArray[index];
            }

This is the value returning method I tried:

private double Lowest(double[] salesArray)
        {
            //method variables
            double lowest = salesArray[0];

            //find lowest sale
            for (int index = 1; index < salesArray.Length; index++)
            {
                if (salesArray[index] < lowest)
                {
                    lowest = salesArray[index];
                }
            }

            return lowest;
        }

Below is a picture of the output: ProgramOutput

  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Oct 20 '22 at 18:25

2 Answers2

0

Arrays are indexed starting at 0, not 1. Also, be careful when you access to element at position 1, it will crash for arrays with size less than 2.

Stéphane Janel
  • 326
  • 1
  • 10
  • but doesn't it need a place to store the values of highest lowest? for example if it steps through the array to find the highest and says that highest = salesArray[0] doesn't that element need to be unchangeable to avoid it being written over? – Ashley Brown Oct 20 '22 at 18:28
0

the problem was found in trying to use two variables for highest/lowest and changing the array size and read count for it.

after changing first initialization for index to 0 and making highest/lowest index 0 it is now working.

Thanks for all the help that help lead me here.