C Program to generate sparse matrix.

C Program to generate sparse matrix.
A sparse matrix is a matrix that allows special techniques to take advantage of the large number of zero elements.Sparse matrix is very useful in engineering field, when solving the partial differentiation equations. Read more about how to generate sparse matrix.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10],B[10][3],m,n,s=0,i,j;
clrscr();
printf("nEnter the order m x n of the sparse matrixn");
scanf("%d%d",&m,&n);
printf("nEnter the elements in the sparse matrix(mostly zeroes)n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("n%d row and %d column: ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("The given matrix is:n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(A[i][j]!=0)
{
B[s][0]=A[i][j];
B[s][1]=i;
B[s][2]=j;
s++;
}
}
}
printf("nThe sparse matrix is given by");
printf("n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("n");
}
getch();
}


Read more c programs 
C Basic
C Strings
Matrix in c

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to sort the matrix rows and columns.

C Program to sort the matrix rows and columns. This C program accept a order MxN Matrix, and sort all rows of the matrix in ascending order and all columns in descending order . In this program, we use the for statement to read two dimension arrays. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>

void main ()
{
static int ma[10][10],mb[10][10];
int i,j,k,a,m,n;

printf ("Enter the order of the matrix n");
scanf ("%d %d", &m,&n);

printf ("Enter co-efficients of the matrix n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
mb[i][j] = ma[i][j];
}
}
printf ("The given matrix is n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("n");
}

printf ("After arranging rows in ascending ordern");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
for (k=(j+1);k<n;++k)
{
if (ma[i][j] > ma[i][k])
{
a = ma[i][j];
ma[i][j] = ma[i][k];
ma[i][k] = a;
}
}
}
} /* End of outer for loop*/

for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("n");
}

printf ("After arranging the columns in descending order n");
for (j=0;j<n;++j)
{
for (i=0;i<m;++i)
{
for (k=i+1;k<m;++k)
{
if (mb[i][j] < mb[k][j])
{
a = mb[i][j];
mb[i][j] = mb[k][j];
mb[k][j] = a;
}
}
}
} /* End of outer for loop*/

for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",mb[i][j]);
}
printf ("n");
}

} /*End of main() */
Read more Similar C Programs
C Basic.

C Matrix.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to find the sparse matrix

C program to accept a matrix and determine whether it is a sparse matrix or not?. A sparse matrix is a matrix, which has more zero elements than nonzero elements. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>

void main ()
{
static int m1[10][10];
int i,j,m,n;
int counter=0;

printf ("Enter the order of the matixn");
scanf ("%d %d",&m,&n);

printf ("Enter the co-efficients of the matixn");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&m1[i][j]);
if (m1[i][j]==0)
{
++counter;
}
}
}
if (counter>((m*n)/2))
{
printf ("The given matrix is sparse matrix n");
}
else
printf ("The given matrix is not a sparse matrix n");

printf ("There are %d number of zeros",counter);

} /* EN dof main() */
Read more Similar C Programs
Matrix Programs
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to interchange the main diagonal elements of the matrix

C Program to interchange the main diagonal elements of the matrix. This Program will accept a matrix of order M x N and store its elements and interchange the main diagonal elements of the matrix with that of the secondary diagonal elements .   Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
void main ()
{

static int ma[10][10];
int i,j,m,n,a;

printf ("Enetr the order of the matix n");
scanf ("%d %d",&m,&n);

if (m ==n )
{
printf ("Enter the co-efficients of the matrixn");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%dx%d",&ma[i][j]);
}
}

printf ("The given matrix is n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("n");
}

for (i=0;i<m;++i)
{
a = ma[i][i];
ma[i][i] = ma[i][m-i-1];
ma[i][m-i-1] = a;
}

printf ("THe matrix after changing the n");
printf ("main diagonal & secondary diagonaln");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("n");
}
}
else
printf ("The givan order is not square matrixn");

} /* end of main() */
Read more Similar C Programs
Matrix Programs
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program: Sum Of Elements In A Matrix

Write a C program to read a matrix A (MxN) and to find the following using functions
a) Sum of the elements of each row
b) Sum of the elements of each column
c) Find the sum of all the elements of the matrix
Output the computed results with suitable headings.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
#include <conio.h>

void main()
{
int arr[10][10];
int i, j, row, col, rowsum, colsum,sumall=0;

/* Function declaration */
int Addrow(int A[10][10], int k, int c);
int Addcol(int A[10][10], int k, int c);

clrscr();

printf("Enter the order of the matrixn");
scanf("%d %d", &row, &col);

printf("Enter the elements of the matrixn");
for(i=0; i<row; i++)
{
for(j=0; j< col; j++)
{
scanf("%d", &arr[i][j]);
}
}

printf("Input matrix isn");
for(i=0; i<row; i++)
{
for(j=0;j<col;j++)
{
printf("%3d", arr[i][j]);
}
printf("n");
}

/* computing row sum */
for(i=0; i<row; i++)
{
rowsum = Addrow(arr,i,col);
printf("Sum of row %d = %dn", i+1, rowsum);
}

for(j=0; j<col; j++)
{
colsum = Addcol(arr,j,row);
printf("Sum of column %d = %dn", j+1, colsum);
}

/* computation of all elements */
for(j=0; j< row; j++)
{
sumall = sumall + Addrow(arr,j,col);
}

printf("Sum of all elements of matrix = %dn", sumall);

}

/* Function to add each row */
int Addrow(int A[10][10], int k, int c)
{
int rsum=0, i;
for(i=0; i< c; i++)
{
rsum = rsum + A[k][i];
}
return(rsum);
}

/* Function to add each column */
int Addcol(int A[10][10], int k, int r)
{
int csum=0, j;
for(j=0; j< r; j++)
{
csum = csum + A[j][k];
}
return(csum);
}
Read more Similar C Programs
Matrix Programs
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to find the Multiplication of two matrices

C Program to find the Multiplication of two matrices. C Program Develop functions
a) To read a given matrix
b) To output a matrix
c) To compute the product of two matrices
Use the above functions to read in two matrices A (MxN) B (NxM), to compute the product of the two matrices, to output the given matrices and the computed matrix in a main function
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>
#define MAXROWS 10
#define MAXCOLS 10

void main()
{
int A[MAXROWS][MAXCOLS], B[MAXROWS][MAXCOLS], C[MAXROWS][MAXCOLS];
int M, N;

/*Function declarations*/

void readMatrix(int arr[][MAXCOLS], int M, int N);
void printMatrix(int arr[][MAXCOLS], int M, int N);
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
int M, int N);

clrscr();

printf("Enter the value of M and Nn");
scanf("%d %d",&M, &N);
printf ("Enter matrix An");
readMatrix(A,M,N);
printf("Matrix An");
printMatrix(A,M,N);

printf ("Enter matrix Bn");
readMatrix(B,M,N);
printf("Matrix Bn");
printMatrix(B,M,N);

productMatrix(A,B,C, M,N);

printf ("The product matrix isn");
printMatrix(C,M,N);
}

/*Input matrix A*/
void readMatrix(int arr[][MAXCOLS], int M, int N)
{
int i, j;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
scanf("%d",&arr[i][j]);
}
}
}
void printMatrix(int arr[][MAXCOLS], int M, int N)
{
int i, j;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
printf("%3d",arr[i][j]);
}
printf("n");
}
}

/* Multiplication of matrices */
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
int M, int N)
{
int i, j, k;
for(i=0; i< M ; i++)
{
for ( j=0; j < N; j++)
{
C[i][j] = 0 ;
for (k=0; k < N; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
}
Read more Similar C Programs
Matrix Programs
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to find Matrix addition,Subtraction and trace.

Write a C program to read two matrices A (MxN) and B(MxN) and perform addition ,subtraction of A and B, and Find the trace of the resultant matrix. Output the given matrix, their sum or Differences and the trace. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <conio.h>

void main()
{
int A[10][10], B[10][10], sumat[10][10], diffmat[10][10];
int i, j, M,N, option;

void trace (int arr[][10], int M, int N);

clrscr();

printf("Enter the order of the matrice A and Bn");
scanf("%d %d", &M, &N);

printf("Enter the elements of matrix An");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&A[i][j]);
}
}

printf("MATRIX A isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",A[i][j]);
}
printf("n");
}

printf("Enter the elements of matrix Bn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%d",&B[i][j]);
}
}

printf("MATRIX B isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",B[i][j]);
}
printf("n");
}

printf("Enter your option: 1 for Addition and 2 for Subtractionn");
scanf("%d",&option);

switch (option)
{
case 1: for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
sumat[i][j] = A[i][j] + B[i][j];
}
}

printf("Sum matrix isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",sumat[i][j]) ;
}
printf("n");
}

trace (sumat, M, N);
break;

case 2:for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
diffmat[i][j] = A[i][j] - B[i][j];
}
}

printf("Difference matrix isn");
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%3d",diffmat[i][j]) ;
}
printf("n");
}

trace (diffmat, M, N);
break;
}

} /* End of main() */

/*Function to find the trace of a given matrix and print it*/

void trace (int arr[][10], int M, int N)
{
int i, j, trace = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
if (i==j)
{
trace = trace + arr[i][j];
}
}
}
printf ("Trace of the resultant matrix is = %dn", trace);

}
Read more Similar C Programs
Matrix Programs
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com