A N*N size matrix is entered. The condition is that each element of the matrix is greater than the elements above it and left to it.
Or, it can be said that all the elements on the right side and below must be greater than the element entered.
Example: for 4 sized (4*4) matrix
1 10 15 30 5 12 18 35 7 14 20 40 19 20 45 60
Now finding any given element in minimum steps.
The code is working fine. I need to optimize this code in terms of time and code length.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*abrupt termation */
void terminate()
{
printf("\nInvalid Entry, Expecting Interger, please re-execute\n");
exit (0);
}
/*checking the validity of input string*/
int isValidExp( char *str )
{
if( str == NULL || strlen(str) == 0)
return 0;
while( *str )
{
if( !(*str>=48 && *str<=57))
return 0;
str++;
}
return 1;
}
int findInleftFromDiagonal(int matrix[100][100],int diagonal,int findEle){
int first=0,middle=0,last = diagonal-1,iteration=0;
middle = (first+last)/2;
while( first <= last ){
if ( matrix[diagonal][middle] < findEle )
first = middle + 1;
else if ( matrix[diagonal][middle] == findEle )
{
return iteration;
}
else
last = middle - 1;
middle = (first + last)/2;
iteration++;
}
return -1;
}
/*find in the right side form the upLane element provided*/
int findInRightFromLane(int matrix[100][100],int row,int col, int size,int findEle){
int first=col,middle=0,last = size-1,iteration=0;
middle = (first+last)/2;
while( first <= last ){
if ( matrix[row][middle] < findEle )
first = middle + 1;
else if ( matrix[row][middle] == findEle )
{
return iteration;
}
else
last = middle - 1;
middle = (first + last)/2;
iteration++;
}
return -1;
}
/*find the element in one particular lane*/
int findInUpLane(int matrix[100][100],int start,int end,int findEle){
int row = start-1,col=start,steps=0;
for(;row>=0;row--){
if(findEle == matrix[row][col]){
return 1;
}else if(findEle > matrix[row][col]){
steps = findInRightFromLane(matrix,row,col,end,findEle);
if(steps != -1){
return steps;
}else{
return -1;
}
}
}
}
/*if element exist calculate the minimum steps*/
int minSteps(int matrix[100][100],int size,int findEle){
int i=0,steps=0;
/*check if element is at it's own place*/
if(matrix[0][0] == findEle){
return 0;
}
/*hit and check the diagonal*/
for(i=1;i<size;i++){
if(findEle==matrix[i][i]){
return i;
}
if(findEle<matrix[i][i]){
/*checking element in left row of diagonal suspect*/
if(findEle>=matrix[i][0]){
steps = findInleftFromDiagonal(matrix,i,findEle);
if(steps != -1){
return steps+i;
}else{
return -1;
}
}
steps = findInUpLane(matrix,i,size,findEle);
if(steps != -1){
return steps+i-1;
}else{
return -1;
}
} //else if(outer)
} // loop
return -1;
}
/*program execution begins here*/
int main(){
char input[10];
int size,matrix[100][100],i=0,j=0,find=0,steps=-1;
printf("Enter the size of matrix : ");
gets(input);
if(!isValidExp(input))
terminate();
size = atoi(input);
//matrix = (int**)malloc(size*size*sizeof(int));
printf("\nEnter the %d elements in right down incresing order: \n\n",size*size);
for(i=0;i<size;i++){
for(j=0;j<size;j++){
gets(input);
if(!isValidExp(input))
terminate();
matrix[i][j] = atoi(input);
}
}
for(i=0;i<size;i++){
for(j=0;j<size;j++){
printf("%d\n",matrix[i][j]);
}
}
printf("\nEnter the number to find : ");
gets(input);
if(!isValidExp(input))
terminate();
find = atoi(input);
steps = minSteps(matrix,size,find);
if(steps == -1){
printf("\nElement is not found\n\n");
}else{
printf("\nMin steps taken are %d \n\n",steps);
}
return 0;
}