C Programming matrix
Posted
by
Bilal Khan
on Programmers
See other posts from Programmers
or by Bilal Khan
Published on 2012-10-04T03:06:20Z
Indexed on
2012/10/04
3:52 UTC
Read the original article
Hit count: 312
c
In this program the user enters the # of columns of the matrix and then the entries of the matrix. So, for example, if the user enters 2 for column # and 1 2 3 4 for entries then the program develops a 2 by 2 matrix with 1 2 3 4 as entries. My program works perfectly in such a case. However, if the user for example had only entered 1 2 3 then my program makes a matrix with garbage values. I would like the program in such a case to exit the program.
It is a simple question, but it has me baffled.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m,x, n, c = 0, d,k, matrix[10][10], transpose[10][10], product[10][10];
printf("Enter the number of columns of matrix ");
scanf("%d",&m);
if(m<=0){
printf("You entered a invalid value.");
exit(0);
}
else{
printf("Enter the elements of matrix \n");
for( c = 0 ; c < 10 ; c++ )
{
for( d = 0 ; d < m ; d++ )
{
scanf("%d",&matrix[c][d]);
if (matrix[c][d] == 99)
// 'x' is character variable I declared to use as a break
break;
// c = c+1;
}
if (matrix[c][d] == 99)
break;
}
}
printf("\nHere is your matrix:\n");
int i;
for(i=0;i<c;i++)
{
for(d=0;d<m;d++)
{
printf("%3d ",matrix[i][d]);
}
printf("\n");
}
© Programmers or respective owner