Error : Operator '-' cannot be applied to operands of type 'int[]' and 'int[]'


Hi,I am using C# 3-D arrays program and getting error "Operator '-' cannot be applied to operands of type 'int[]' and 'int[]'", how I can resolve this error my current code is as below :

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution {

 // Complete the formingMagicSquare function below.
 static int formingMagicSquare(int[][] s) {
  // Magicsquare1
  // Declare local jagged array with 3 rows.
  int[][] magicsquare1 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare1[0] = new int[3] {
   8,
   1,
   6
  };
  // Set second row.
  magicsquare1[1] = new int[3] {
   3,
   5,
   7
  };
  // Set third row, using array initializer.
  magicsquare1[2] = new int[3] {
   4,
   9,
   2
  };

  // Magicsquare2
  // Declare local jagged array with 3 rows.
  int[][] magicsquare2 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare2[0] = new int[3] {
   6,
   1,
   8
  };
  // Set second row.
  magicsquare2[1] = new int[3] {
   7,
   5,
   3
  };
  // Set third row, using array initializer.
  magicsquare2[2] = new int[3] {
   2,
   9,
   4
  };

  // Magicsquare3
  // Declare local jagged array with 3 rows.
  int[][] magicsquare3 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare3[0] = new int[3] {
   4,
   9,
   2
  };
  // Set second row .
  magicsquare3[1] = new int[3] {
   3,
   5,
   7
  };
  // Set third row, using array initializer.
  magicsquare3[2] = new int[3] {
   8,
   1,
   6
  };

  // Magicsquare4
  // Declare local jagged array with 3 rows.
  int[][] magicsquare4 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare4[0] = new int[3] {
   2,
   9,
   4
  };
  // Set second row.
  magicsquare4[1] = new int[3] {
   7,
   5,
   3
  };
  // Set third row, using array initializer.
  magicsquare4[2] = new int[3] {
   6,
   1,
   8
  };

  // Magicsquare5
  // Declare local jagged array with 3 rows.
  int[][] magicsquare5 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare5[0] = new int[3] {
   8,
   3,
   4
  };
  // Set second row.
  magicsquare5[1] = new int[3] {
   1,
   5,
   9
  };
  // Set third row, using array initializer.
  magicsquare5[2] = new int[3] {
   6,
   7,
   2
  };

  // Magicsquare6
  // Declare local jagged array with 3 rows.
  int[][] magicsquare6 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare6[0] = new int[3] {
   4,
   3,
   8
  };
  // Set second row.
  magicsquare6[1] = new int[3] {
   9,
   5,
   1
  };
  // Set third row, using array initializer.
  magicsquare6[2] = new int[3] {
   2,
   7,
   6
  };

  // Magicsquare7
  // Declare local jagged array with 3 rows.
  int[][] magicsquare7 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare7[0] = new int[3] {
   6,
   7,
   2
  };
  // Set second row, .
  magicsquare7[1] = new int[3] {
   1,
   5,
   9
  };
  // Set third row, using array initializer.
  magicsquare6[2] = new int[3] {
   8,
   3,
   4
  };

  // Magicsquare8
  // Declare local jagged array with 3 rows.
  int[][] magicsquare8 = new int[3][];
  // Create a new array in the jagged array, and assign it.
  magicsquare8[0] = new int[3] {
   2,
   7,
   6
  };
  // Set second row, initialized .
  magicsquare8[1] = new int[3] {
   9,
   5,
   1
  };
  // Set third row, using array initializer.
  magicsquare8[2] = new int[3] {
   4,
   3,
   8
  };

  int cost1 = 0;
  int cost2 = 0;
  int cost3 = 0;
  int cost4 = 0;
  int cost5 = 0;
  int cost6 = 0;
  int cost7 = 0;
  int cost8 = 0;

  foreach(int[] a in s) {
   foreach(int[] b in magicsquare1) {
    if (a != b) {
     cost1 = cost1 + Math.Abs(a - b);
    }
   }
  }
  foreach(int[] a in s) {
   foreach(int[] b in magicsquare2) {
    if (a != b) {
     cost2 = cost2 + Math.Abs(a - b);
    }
   }
  }
  foreach(int[] a in s) {
   foreach(int[] b in magicsquare3) {
    if (a != b) {
     cost3 = cost3 + Math.Abs(a - b);
    }
   }
  }
  foreach(int[] a in s) {
   foreach(int[] b in magicsquare4) {
    if (a != b) {
     cost4 = cost4 + Math.Abs(a - b);
    }
   }
  }
  foreach(int[] a in s) {
   foreach(int[] b in magicsquare5) {
    if (a != b) {
     cost5 = cost5 + Math.Abs(a - b);
    }
   }
  }
  foreach(int[] a in s) {
   foreach(int[] b in magicsquare6) {
    if (a != b) {
     cost6 = cost6 + Math.Abs(a - b);
    }
   }
  }
  foreach(int[] a in s) {
   foreach(int[] b in magicsquare7) {
    if (a != b) {
     cost7 = cost7 + Math.Abs(a - b);
    }
   }
  }
  foreach(int[] a in s) {
   foreach(int[] b in magicsquare8) {
    if (a != b) {
     cost8 = cost8 + Math.Abs(a - b);
    }
   }
  }
  int[] smallestcost = new int[] {
   cost1,
   cost2,
   cost3,
   cost4,
   cost5,
   cost6,
   cost7,
   cost8
  };
  int smallestcostmin = smallestcost.Min();
  return smallestcostmin;

 }
 static void Main(string[] args) {
  TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
  int[][] s = new int[3][];
  for (int i = 0; i < 3; i++) {
   s[i] = Array.ConvertAll(Console.ReadLine().Split(' '), sTemp => Convert.ToInt32(sTemp));
  }
  int result = formingMagicSquare(s);
  textWriter.WriteLine(result);
  textWriter.Flush();
  textWriter.Close();
 }
}

Thanks.


Asked by:- LuneAgile
0
: 3957 At:- 7/21/2018 3:17:30 PM
c# arrays 3D







2 Answers
profileImage Answered by:- vikas_jk

Your decription doesn't explain what you are trying to do but error is you are trying to delete array from array using this line of code

 cost8 = cost8 + Math.Abs(a-b); // not possible

this is not correct code, you can get element of an array and subtract from it like

 cost8 = cost8 + Math.Abs(a[i]-b[j]); // where i, j are integer values

Hope it helps

1
At:- 7/21/2018 7:03:02 PM


profileImage Answered by:- LuneAgile

Thanks so much  Vikas.

 

0
At:- 7/22/2018 4:18:02 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use