printf("ho_tari\n");

sumMatrix 본문

C

sumMatrix

호타리 2023. 9. 1. 12:39
#include <stdio.h>



int main(void)

{

	int matrix[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; 

	// {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

	

	int total = 0;

	

	for (int i = 0; i < 3; ++i)

	{

		for (int j = 0; j < 4; ++j)

		{

			total += matrix[i][j];

		}

	}

	

	printf("sum: %d\n", total);

	

	return 0;

}

 

#include <stdio.h>



int sumMatrix(const int (*pMatrix)[4], int row, int col)

{

	int total;

	for (int i = 0; i < row; ++i)

	{

		for (int j = 0; j < col; ++j)

		{

			total = total + pMatrix[i][j];   // *(pMatrix[i] + j) == *(*(pMatrix + i) + j)

		}

	}

	

	return total;

}



/*

int sumMatrix(int *matrix, int row, int col)

{

	int total = 0;

	

	for (int i = 0; i < row; ++i)

	{

		for (int j = 0; j < col; ++j)

		{

			total = total + *(matrix + i * col +j);

		}

	}

	return total;

}

*/

int main(void)

{

	//int row = 3;

	//int col = 4;

	int matrix[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; 

	

	//int total = sumMatrix(&matrix[0][0], 3, 4);

	int total = sumMatrix(matrix, 3, 4);

	printf("sum: %d\n", total);

	

	return 0;

}

 

<compile 결과>

'C' 카테고리의 다른 글

swap  (0) 2023.09.01
sumOne2Ten  (0) 2023.09.01
sumArray2  (0) 2023.09.01
sumArray  (0) 2023.09.01
stringArray  (0) 2023.09.01