Reputation: 151
Getting an error at (private int >>>TotArray<< (int[,] array)) Which tells me I cannot return value. Can anyone help me and tell me what I am doing wrong?
What this code does is it supposed to add all the numbers of this 2 dimensional array. But in the moment it does nothing.
int[,] A = new int[3, 4]
{
{ 4, -5, 12, -2},
{ -9, 15, 19, 6},
{ 18, -33, -1, 7}
};
public Form1()
{
InitializeComponent();
}
private int TotArray(int[,] array)//<<<<<< error
{
int sum = 0;
int rows = array.GetLength(0);
int cols = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sum += array[i, j];
}
}
richTextBox1.Text = ("The sum of the array is " + sum.ToString() + ".");
}
private void button1_Click(object sender, EventArgs e)
{
TotArray(A);
}
Upvotes: 0
Views: 524
Reputation: 101681
Your return type is int
but you are not returning anything from your method.If you don't want to return anything change return type to void
or return something.
private int TotArray(int[,] array)
{
int sum = 0;
int rows = array.GetLength(0);
int cols = array.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sum += array[i, j];
}
}
return sum;
}
private void button1_Click(object sender, EventArgs e)
{
int sum = TotArray(A);
richTextBox1.Text = string.Format("The sum of the array is {0}.", sum);
}
Upvotes: 2
Reputation: 2382
Put
return sum;
At the end of you're TotArray method.
Then place(for testing)
Debug.WriteLine(TotArray(A));
In your calling code, run the program, check the output window and your sum should be returned in the output window.
Upvotes: 0
Reputation: 5538
I think you are getting an error because your TotArray method isn't actually returning an int.
What happens if you switch it to the following:
private void TotArray(int[,] array)
And if that doesnt fix it, can you post the error message?
Upvotes: 2