Sign in
Log inSign up

Why does this program not work as expected?

Chandrachur Mukherjee's photo
Chandrachur Mukherjee
·Jan 20, 2018
#include <stdio.h>
#include <conio.h> 
void main() 
{     
int mainMat[10][10], minorMat[10][10], i, j, x, y, r, c;     
clrscr();     
printf("This program can find a minor matrix based on any element of a given matrix. \n");     
printf("Please enter the number of rows and columns of the main matrix\n");     scanf("%d%d", &r, &c);     
for(i=0; i<r; i++)     
for(j=0; j<c; j++)     
{         
mainMat[i][j] = 0;     
}     
for(i=0; i<r; i++)     
for(j=0; j<c; j++)     
{         
minorMat[i][j] = 0;     
}     
printf("Now enter the following elements for the matrix: \n");     
for(i=0; i<r; i++)     
for(j=0; j<c; j++)     
{         
printf("Enter the element a%d%d: ", i+1, j+1);         
scanf("%d", &mainMat[i][j]);     
}     
for(i=0; i<r; i++)     
for(j=0; j<c; j++)     
{             
mainMat[i][j] = minorMat[i][j];     
}     
printf("\nNow enter the coordinates of the element in the matrix you want to base the minor matrix upon: ");     
scanf("%d%d", &x ,&y);     
for(i=x; i<r; i++)     
for(j=0; j<c; j++)     
{         
minorMat[i-1][j] = minorMat[i][j];     
}     
for(i=0; i<r; i++)     
for(j=y; j<c; j++)     
{         
minorMat[i][j-1] = minorMat[i][j];     
}     
printf("\nOutput Matrix:\n\n");     
for(i=0; i<r-1; i++)     
for(j=0; j<c-1; j++)     
{         
printf("%d  ", minorMat[i][j]);             
if(j==c-2)             
printf("\n\n");     
}     
getch(); 
}