please help me make this program run faster....thanks

////////////////////////////////////////////////////////////////////////////////
//Author: Linh Tran
//This program will create a square matrix A (given). Based on this matrix, the
//program will find a square matrix B, which has same size as A. The values of
//B will be found based on values from A, such as:
//- B(i,i) = 0
//	- if i < j then B(i,j) = max {A(k,m) | where k <= i and m >= j}
//	- if i > j then B(i,j) = max {A(k,m) | where k >= i and m <= j}
//In this program, i suppose that the size of matries is 5x5.
//This program takes NxN running times.
//It takes me 40 minutes to creat this program.
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstdlib>
#include <iomanip>
using std::cout;
using std::endl;

#ifndef SQUAMATRIX5_H
#define SQUAMATRIX5_H
///////////////////////////////////////////////////
////Begining of class
///////////////////////////////////////////////////
class SquaMatrix{
public:
SquaMatrix(int row =0, int col=0);	//constructor
int Max1(int, int);			//find max value of A when in B has row > colume
int Max2(int, int);			//find max value of A when in B has row > colume
void Print();				//print result
void Compute();				//find matrix B bases on A
void Generate();			//generate given matrix A
int i ;						//holding index of matries
int j ;
int B[10][10],A[10][10];
int temp;					//temporary holding value of matrix A
};


#endif
///////////////////////////////////////////////////////////
//////constructor to initialize the size of matries
///////////////////////////////////////////////////////////
SquaMatrix::SquaMatrix(int row, int col){
	i = row;
	j = col;
	temp = 0;
}
//////////////////////////////////////////////////////////
////Generate the given matrix A
//////////////////////////////////////////////////////////
void SquaMatrix::Generate(){
	for(int a = 0; a < i; a++){
		for(int b  =0; b< j; b++){
			A[a][b]= 1+rand() % 4;	//generate value of matrix from 1 to 4

		}

	}
}
////////////////////////////////////////////////////////////
////////find matrix B based on matrix A
////////////////////////////////////////////////////////////
void SquaMatrix::Compute(){


for (int i_B = 0; i_B < i; i_B++){
	for (int j_B =0; j_B <j; j_B++){
		if (i_B == j_B ){
			B[i_B][j_B] = 0;

		}
		else{
			if( i_B < j_B ){
				B[i_B][j_B] = Max1(i_B,j_B);

			}
			else{
				if( i_B > j_B ){
					B[i_B][j_B] = Max2(i_B, j_B);
				}
			}
		}

	}

}
}
//////////////////////////////////////////////////////////////
/////find the max value from matrix B when row < colume
/////////////////////////////////////////////////////////////
int SquaMatrix::Max1(int i_B, int j_B){

for (int a =0; a<= i_B; a++){
	for (int b=j_B; b< j; b++){
		if (a <= i_B && b >= j_B ){

			if( A[a][b] > temp ){
				temp = A[a][b];

			}
		}
	}
}
return temp;
	;
}
//////////////////////////////////////////////////////////////
///////find the max value from matrix A when row > colume
//////////////////////////////////////////////////////////////
int SquaMatrix::Max2(int i_B, int j_B){


for (int a =i_B; a< i; a++){
	for (int b=0; b<= j_B; b++){
		if (a >= i_B && b <= j_B ){

			if( A[a][b] > temp ){
			temp = A[a][b];
			}
		}
	}
}
return temp;
}

////////////////////////////////////////////////////////////
/////Print the results
////////////////////////////////////////////////////////////
void SquaMatrix::Print(){

cout<<"Square Matrix A: "<<endl;

for (int a = 0; a < i; a++){
	for(int b=0; b<i; b++){
		cout<<A[a][b];
	}
	cout<<endl;
}

cout<<"Square Matrix B: "<<endl;

for (int a2=0; a2<j; a2++){
	for(int b2=0; b2<j; b2++){
		cout<<B[a2][b2];
	}
	cout<<endl;
}

}
///////////////////////////////////////////////////////////
/////creat a main() to test the computer()
//////////////////////////////////////////////////////////
int main(){

SquaMatrix S_M(5,5);	//initialize the size of matries

S_M.Generate();			//i need the given Square matrix A

S_M.Compute();			//Compute Square matrix B bases on A

S_M.Print();			//Print both Matries

return 0;
}

Parents Reply Children
More questions in this forum