diagonales code example

Example: diagonales

RECORRER DIAGONALES

La prinicpal no
	|	0|	1	|	2	|	3	|	4	|	5|
0|_P_|__*_|__*|_*_|_*_|_*|
1|___|_P__|__*|__*|_*_|_*|
2|___|____|_P_|_*_|_*_|*_|
3|___|____|___|P_|_*_|_*_|
4|___|____|___|___|_P_|*_|
4|___|____|___|___|___|P_|


			d1				d2				d3				d4				d5
		(0, 1)		(0, 2)		(0, 3)		(0, 4)		(0, 5)
		(1, 2)		(1, 3)		(1, 4)		(1, 5)
		(2, 3)		(2, 4)		(2, 5)
		(3, 4)		(3, 5)
		(4, 5)

		(i, j)

#define kDIM 6
typedef int TMatriz[kDIM][kDIM]; //es cuadrada

void imprimir_diagonales(TMatriz m){
	int d, i, j, suma;

	for(d = 1; d < kDIM; d++){
		i = 0;
		j = d;
		suma = 0;
		while(j < kDIM){
			suma = suma + m[i][j];
			i++;
			j++;
		}
		printf("%d\n", suma);
	}
}

//si no fuera cuadrada:
void imprimir_diagonales(TMatriz m){
	int d, i, j, suma;

	for(d = 1; d < columnas; d++){
		i = 0;
		j = d;
		suma = 0;
		while(i < filas && j < columnas){
			suma = suma + m[i][j];
			i++;
			j++;
		}
		printf("%d\n", suma);
	}
}




	|	0|	1	|	2	|	3	|	4	|	5|
0|_*_|__*_|_*_|_*_|_*_|*_|
1|__*|_*__|__*|__*|_*_|__|
2|_*_|__*_|_*_|_*_|___|__|
3|_*_|_*__|_*_|___|___|__|
4|__*|__*_|___|___|___|__|
5|_*_|____|___|___|__|___|


	d0				d1				d2				d3				d4				d5
(0, 0)		(1, 0)		(2, 0)		(3, 0)		(4, 0)		(5, 0)
					(0, 1)		(1, 1)		(2, 1)		(3, 1)		(4, 1)
										(0, 2)		(1, 2)		(2, 2)		(3, 2)
															(0, 3)		(1, 3)		(2, 3)
																				(0, 4)		(1, 4)
																									(0, 5)

																									(i, j)
      
      void sumamayor(TMatriz m){
  int d, i, j;
  int suma = 0;

  for(d = 0; d < kDIM; d++){
  	suma = 0;
  	for(i = d, j = 0; i >= 0; i--, j++){
  		suma = suma + m[i][j];

  	}
      printf("suma: %d\n ", suma);
  }

}

//recorrer todas las diagonales a partir de la esquina inferior izquierda
std::vector<int> diagonal(const TMatrix& a){
	int d, i, j;
	std::vector<int> res;
	int filas, columnas;
	int suma;

	filas = a.size();
	columnas = a[0].size();

//1ª parte:
	for(d = filas - 1; d >= 0; d--){
		i = d;
		j = 0;
		suma = 0;
		while(i < filas && j < columnas){
			suma = suma + a[i][j];
			i++;
			j++;
		}
		res.push_back(suma);
	}
//2ª parte:
	for(d = 0; d < columnas; d++){
		i = 0;
		j = d;
		suma = 0;
		while(i < filas && j < columnas){
			suma = suma + a[i][j];
			i++;
			j++;
		}
		res.push_back(suma);
	}
	return res;
}

Tags:

C Example