Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
}
share|improve this question

closed as off-topic by 200_success May 30 at 6:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. Such questions may be suitable for Stack Overflow or Programmers. After the question has been edited to contain working code, we will consider reopening it." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

1 Answer

Compiler says:

cr52066.c:13:24: warning: implicitly declaring library function 'strlen' with
      type 'unsigned long (const char *)'
    if( str == NULL || strlen(str) == 0)
                       ^
cr52066.c:13:24: note: please include the header <string.h> or explicitly
      provide a declaration for 'strlen'
cr52066.c:82:1: warning: control may reach end of non-void function
      [-Wreturn-type]
}
^
2 warnings generated.

Test case:

warning: this program uses gets(), which is unsafe.
Enter the size of matrix : 4

Enter the 16 elements in right down incresing order: 

1
10
15
30
5
12
18
35
7
14
20
40
19
20
45
60
1
10
15
30
5
12
18
35
7
14
20
40
19
20
45
60

Enter the number to find : 19

Element is not found
share|improve this answer
    
Compiler warnings doesn't make code broken does it? Besides, the question seems to have been updated. –  Simon André Forsberg May 30 at 17:42
    
@Simon It's the fact that it failed to find 19 that makes it broken. –  200_success May 30 at 17:45
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.