Hi, I am trying to run the below C# code for martix, but getting error
Wrong number of indexes `2' inside [], expected `1' Compilation failed: 1 error(s), 0 warnings
here is my current
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 {
static int formingMagicSquare(int[][] s) {
int[] rowSum = new int[s.GetLength(0)], colSum = new int[s.GetLength(1)];
int sum = 0;
int magiconst = 15;
int cons = 0;
for (int r = 0; r < s.GetLength(0); r++) {
for (int c = 0; c < s.GetLength(1); c++) {
rowSum[r] += s[r, c];
}
sum += Math.Abs(rowSum[r] - magiconst);
}
return sum;
}
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();
}
}
How to resolve this error? Thanks
Hi, all sorry I just made a mistake on my code, should rowSum[r] += s[r, c]; transformed to rowSum[r] += s[r] [c];
Thanks
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly