The C language implements the example of adding the diagonals of a 3*3 array

  • 2020-06-23 01:28:26
  • OfStack

I don't need to say any more, just code it!


#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<windows.h>
 
 
int SUM(char arr[][3])
{
	int i = 0;
	int j = 0;
	int sum = 0;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			if (i == j)
			{
				sum += arr[i][j];
			}
			if (i + j == 2)
			{
				sum += arr[i][j];
			}
		}
	}
	sum -= arr[1][1];
	return sum;
}
int main()
{
	char arr[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	printf("the answer is %d\n",SUM(arr));
	system("pause");
	return 0;
}

Related articles: