Hello,

So i'm new to C#, but I know c++ well.

I'm having problems inputting data into a two dimensional array

Here is how I initialized the array

int[,] temps = new int [12, 2];

And I attempted to write the input function and i have this so far.... any sugggestions?

public static void getTemps(int temps)
    {
       
            for (int i = 0; i <= 11; i++)
            {
                for(int e = 0; e <2; e++)
                {
                    temps[i][0] = int.Parse(System.Console.ReadLine());
                    temps[i][1] = int.Parse(System.Console.ReadLine());
                    //constArray[e][] = inputfunctions.getLow;
                }        
            }
     }

afcourse I get an error, i'm sure the syntax is off, but i can't find any references online on how to input into a 2 dimensional array

Recommended Answers

All 8 Replies

Multidimensional arrays in C# are done with one set of [], not two.
Your code should read:

for (int i = 0; i < 12; i++) {
    for (int e = 0; e < 2; e++) {
        temps[i,e] = int.Parse(Console.ReadLine());
    }
}

(Not going into how to determine array size programatically at this time :))

The syntax you are using is for jagged arrays.

Thanks for the reply

i get this error:
Cannot apply indexing with [] to an expression of type 'int'

public static void getTemps(int temps) Method signature says temp is a single int. Change it to be a 2D array: public static void getTemps(int[,] temps)

static public void getTemps(int[,] temp) {
    for (int i = 0; i < temp.GetLength(0); i++) {
        for (int j = 0; j < temp.GetLength(1); j++) {
            while (Int32.TryParse(Console.ReadLine(), out temp[i, j]) == false) {
                Console.WriteLine("That wasn't an integer");
            }
        }
    }
}

thanks for the response....works perfectly now

now i'm trying to go through each value that was inputted and get the highest and lowest of each column

i wrote this so far

public static int coolestLOW(int[,] temps)
     {
         int lowest = 0;

         for (int i = 0; i < 11; i++)
             for (int e = 0; e < 2; e++)
                 if (temps[i, 1] > temps[i + 1, 1])
                 {
                     int temp = temps[i, 1];
                     //temps[i, 1] = temps[i + 1, 1];
                     //temps[i, 1] = lowest;
                     temp = lowest;
                 }
         return lowest;

what i'm trying to do is compare each value and iterate through the whole array column, if one is less than then you copy it into temp, and you keep iterating until the end of the array

Line 5: Your array has 12 elements so change the 11 to a 12.
Line 7: You have a loop variable for the column (e) but you hard code the column. Why?
Line 9: temp only exists within the block it was declared in, so as soon as you leave the if statement it is no longer available.

You need to rethink what you are doing. Maybe this will help some:

void MinMax(int[] array) {
    int min = Int32.MaxValue;
    int max = Int32.MinValue;

    for (int i = 0; i < array.Length; i++) {
        if (array[i] < min) min = array[i];
        if (array[i] > max) max = array[i];
    }

    Console.WriteLine("Minimum value = {0}", min);
    Console.WriteLine("Maximum value = {0}", max);
}

And before someone comes along and refers to Array.Min/Max, yes, I know they exist, but we aren't trying to use them here since ilinan87 needs the values by column, not the whole array.

Oh, another hint here: Column first, row second :)

thanks i got it to work with this code

public static int coolestLOW(int[,] temps)
     {
         int lowest = temps[0,1];

         for (int i = 0; i < 12; i++)
             for (int e = 0; e < 2; e++)
                 if (temps[i, 1] < lowest)
                 {
                     lowest = temps[i, 1];
                 }
         return lowest;
     }

Just one question, what's line 6 doing for you?

good question..... not sure
i can probably delete, seems like its not used for anything

thanks for your replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.