devnieL
9/30/2012 - 5:57 AM

Suma de niveles de una matriz, los bordes ...

Suma de niveles de una matriz, los bordes ...

#include <iostream>
#include <fstream>
#include <exception>

using namespace std;

void print2dArray(int **arr,int x, int y){
	for(int i=0;i<x;i++){
		for(int j=0;j<y;j++){
			cout << "matrix[" << i << "][" << j << "]" << " = " << arr[i][j] << endl;
		}
	}
}

int sumLevel(int **arr, int x, int y, int level){
	int total = 0;
	int i = level;
	int j = level;

	for(;i<x;i++){
		cout << "EMPEZANDO i = " << i << endl;
		j = level;
		for(;j<y;j++){
			cout << "x : " << x  << " y: " << y << " i: " << i << " j : " << j << endl;

			if((i==level || i==x-level-1) && j<y-level){
				total += arr[i][j];
				cout << "Sumando matrix[" << i << "][" << j <<"] = " << arr[i][j] << endl;
				cout << "Total parcial : " << total << endl;
			}

			if(i>level && i<x-level-1){
				if((j==level || j==y-level-1)){
					total += arr[i][j];
					cout << "2do IF - Sumando matrix[" << i << "][" << j <<"] = " << arr[i][j] << endl;
					cout << "Total final : " << total << endl;
				}
			}
		}
	}

	return total;	
}

int main(){
	ifstream source("input.txt");
	//ofstream destination("output.txt");

	int x;
	int y;

	source >> x >> y; // Reads one int from source-file.txt
	// source.close(); // close file as soon as we're done using it
	
	cout << x << " " << y << endl;

	int **matrix = new int*[x];

	for(int i=0;i<x;i++){

		// Me faltó esto ...
		matrix[i] = new int[y];

		for(int j=0;j<y;j++){
			int tmp;
			source >> tmp;
			matrix[i][j] = tmp;
			// source >> matrix[i][j];
			// int tmp = *matrix[i][j];
			cout << "matrix[" << i << "][" << j << "]" << " = " << matrix[i][j] << endl;
		}
	}

	cout << "===============================" << endl;

	print2dArray(matrix,x,y);

	int total = sumLevel(matrix,x,y,0);

	cout << "TOTAL, level 0 : " << total << endl;

	cout << "================================================" << endl;

	int totalLevel1 = sumLevel(matrix,x,y,1);

	cout << "TOTAL , level 1 :  " << totalLevel1 << endl;

	source.close();	

	return(0);
}