0

I am trying to get a jagged array that will solve the sums of the elements from dailyTemperature and that will successfully display them in the Main () under the Weather class. This is what I have so far.

 public int[,] GetTemperatures()
        {
            int[,] dailyTemperature =
            {
                {38, 40, 42, 34},
                {55, 41, 40, 30},
                {28, 39, 21, 60},
                {61, 52, 43, 42},
                {35, 36, 30, 29},
                {24, 33, 37, 40}
            };

            Console.Write(" ");
            for (int i = 0; i < dailyTemperature.Length; i++)
            {
                Console.Write(dailyTemperature[i] + " ");
            }
            Console.WriteLine();
            for (int i = 0; i < dailyTemperature.GetLength(0); i++)
            {
                Console.Write(dailyTemperature[i].Substring(0, dailyTemperature[i].Length - 1) + " "); 

                for (int j = 0; j < dailyTemperature.GetLength(1); j++)
                {
                    Console.Write(dailyTemperature[i, j] + "    "); 
                }
                Console.WriteLine();
            }
            return dailyTemperature;
}

{

     public int[] GetTemperatureSum(int [,] dailyTemperature)
        {
            int[][] temperatureSum= new int[6][];
            temperatureSum[0][1] = dailyTemperature[0, 0] + dailyTemperature[0, 1] + dailyTemperature[0, 2] + dailyTemperature[0, 3];
            temperatureSum[0][2] = dailyTemperature[1, 0] + dailyTemperature[1, 1] + dailyTemperature[1, 2] + dailyTemperature[1, 3];
            temperatureSum[0][3] = dailyTemperature[2, 0] + dailyTemperature[2, 1] + dailyTemperature[2, 2] + dailyTemperature[2, 3];
            temperatureSum[0][4] = dailyTemperature[3, 0] + dailyTemperature[3, 1] + dailyTemperature[3, 2] + dailyTemperature[3, 3];
            temperatureSum[0][5] = dailyTemperature[4, 0] + dailyTemperature[4, 1] + dailyTemperature[4, 2] + dailyTemperature[4, 3];
            temperatureSum[0][6] = dailyTemperature[5, 0] +dailyTemperature[5, 1] + dailyTemperature[5, 2] + dailyTemperature[5, 3];
            for (int i = 0; i < temperatureSum.GetLength(0); i++)
            {
                for (int j=0; j<temperatureSum.GetLength(1); j++)
                {
                    Console.Write(temperatureSum[i][j]);
                }
                Console.WriteLine();
            }
            return temperatureSum;
        }
        static void Main(string[] args)
        {
            Weather weather= new Weather();
 {
                    weather.GetTemperatures();
                    weather.GetTemperatureSum(dailyTemperature);

        }
};

My strategy here is to display all of these methods through the Main() method, however I am getting an error under (dailyTemperature) by weather.GetTemperatureSum. This is what the error says, "An object reference is required for the nonstatic field, method, or property 'member'".

I'd love it if someone could help me on this, although as I understand the formatting has to be relatively similar to this. I absolutely need to, for example, solve the sums through the computer as opposed to doing so manually. Thanks.

  • My apologies if the brackets here are formatted weirdly, that's not how it is in my code. – ronalddonaldronald Nov 27 '22 at 02:02
  • It's nicer if you give a clearer indication of where the error occurs. I'm guessing it's the line that starts: `temperatureSum[0][1] =`. A _jagged array_ is better described as _an array of arrays_. You declare and construct an array of arrays `int[][] temperatureSum= new int[6][];`, but you don't construct any of the arrays it is to contain. As a result, `temperatureSum[0]` is null – Flydog57 Nov 27 '22 at 02:12
  • I don't know exactly where the error occurs, other than what is present at the Main() method. Anyways, so this means I would have to use a normal 1d array? – ronalddonaldronald Nov 27 '22 at 02:15
  • Yes you do know where the error occurs. If you run the code in the debugger, stepping through the code, it will tell you where the error occurs. Even if you don't step through, the _stack_ associated with the exception will point to the offending line. I can't tell why you are using a jagged array, and that wasn't your question, so I don't really have an answer as to whether a 1D array would work better – Flydog57 Nov 27 '22 at 02:17
  • The reason I'm using a jagged array is just so I can add the sums and display the values. That's it really, there's no other reason. – ronalddonaldronald Nov 27 '22 at 02:29
  • You really should be using loops over the dimensions of `dailyTemperature`, since am array of any size could be passed to the method – Rufus L Nov 27 '22 at 05:08
  • Why not use a single dimensional array to store the values like you were before? Your jagged array is only holding a single array of data. – Rufus L Nov 27 '22 at 05:30
  • I don't know, I was just trying things. But I suppose a normal single dimensional array would be better then. – ronalddonaldronald Nov 27 '22 at 06:49
  • You're not capturing the return values from your method calls in `Main` – Rufus L Nov 27 '22 at 06:50
  • „My apologies if the brackets here are formatted weirdly“ - no need for apologies, just edit the question and fix it – Sir Rufo Nov 27 '22 at 08:18

1 Answers1

0

More details about:

int[,] dailyTemperatures =
{
    { 38, 40, 42, 34 },
    { 55, 41, 40, 30 },
    { 28, 39, 21, 60 },
    { 61, 52, 43, 42 },
    { 35, 36, 30, 29 },
    { 24, 33, 37, 40 }
};

Weather.GetTemperatures(dailyTemperatures);
Weather.GetTemperatureSum(dailyTemperatures);

public static class Weather
{
    public static int[,] GetTemperatures(int[,] dailyTemperatures)
    {
        
        var rows = dailyTemperatures.GetUpperBound(0) + 1;
        var columns = dailyTemperatures.Length / rows;

        for (var i = 0; i < rows; i++)
        {
            for (var j = 0; j < columns; j++)
            {
                Console.Write($"{dailyTemperatures[i, j]} \t");
            }
            Console.WriteLine();
        }

        return dailyTemperatures;
    }

    public static int[] GetTemperatureSum(int[,] dailyTemperature)
    {
        var temperatureSum = new int[6];
        temperatureSum[0] = dailyTemperature[0, 0] + dailyTemperature[0, 1] + dailyTemperature[0, 2] + dailyTemperature[0, 3];
        temperatureSum[1] = dailyTemperature[1, 0] + dailyTemperature[1, 1] + dailyTemperature[1, 2] + dailyTemperature[1, 3];
        temperatureSum[2] = dailyTemperature[2, 0] + dailyTemperature[2, 1] + dailyTemperature[2, 2] + dailyTemperature[2, 3];
        temperatureSum[3] = dailyTemperature[3, 0] + dailyTemperature[3, 1] + dailyTemperature[3, 2] + dailyTemperature[3, 3];
        temperatureSum[4] = dailyTemperature[4, 0] + dailyTemperature[4, 1] + dailyTemperature[4, 2] + dailyTemperature[4, 3];
        temperatureSum[5] = dailyTemperature[5, 0] + dailyTemperature[5, 1] + dailyTemperature[5, 2] + dailyTemperature[5, 3];
        for (var i = 0; i < temperatureSum.GetLength(0); i++)
        {
            Console.WriteLine(temperatureSum[i]);
        }
        return temperatureSum;
    }
}

In the GetTemperatureSum method, the returned type is an array (int[]). It is better to pass dailyTemperatures variable as a method parameter

Viachaslau S
  • 106
  • 1
  • 5