The previous two versions of the program given here were unnecessarily long and complicated. I am providing a much more simple and succinct version. I have constructed it on the basis of two 3*3 matrices. However you can easily change the order by replacing the "3"s of the program with your required order, provided the number of columns of the premultiplier is equal to the number of rows of the postmultiplier. If you want to dynamically allocate the orders of the matrices, you will have to use calloc and malloc, which I don't know as of yet!
#include
int main()
{
int i,j,l;
int matrix[3][3],matri[3][3],matr[3][3]={{0,0,0},{0,0,0},{0,0,0}};
printf("Enter 1st matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf_s("%d",&matrix[i][j]);
}
printf("\n\n");
printf("Enter 2nd matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf_s("%d",&matri[i][j]);
}
for (j=0;j<3;++j)
{
for (i=0;i<3;++i)
{
for (l=0;l<3;++l)
matr[i][j] = matr[i][j] + (matrix[i][l])*(matri[l][j]);
}
}
printf("The resultant matrix is:\n\n");
for (i=0;i<3;++i)
{
for (j=0;j<3;++j)
printf("%4d",matr[i][j]);
printf("\n\n");
}
return( 0 );
}
Recent changes to the above code
30/4/12:
Error: main() must return a value.
Error: matr must be initialised in declaration, not afterwards.
Warning: Variable k is unreferenced (removed).
Warning: Use of scanf is unsafe. Replaced with scanf_s
Benign: Commented code removed for clarity.
An alternate version with dynamic arrays is shown below.
#include
typedef unsigned int UINT;
// Forward declarations.
int ** CreateMatrix( UINT rows, UINT cols );
void InputMatrix( int ** Matrix, UINT rows, UINT cols, char * Title );
void PrintMatrix( int ** Matrix, UINT rows, UINT cols, char * Title );
void ReleaseMatrix( int ** Matrix, UINT rows );
int main()
{
// Initialise some variables.
UINT rows=0, comm=0, cols=0;
UINT i=0, j=0, k=0;
// 2D arrays (pointer-to-pointer-to-int).
int ** M1 = NULL;
int ** M2 = NULL;
int ** M3 = NULL;
printf("Calculate the dot products of two matrices.\n\n");
printf( "Enter the number of rows in the 1st matrix:\t");
scanf_s("%u", &rows );
printf( "Enter the number of common elements:\t\t");
scanf_s("%u", &comm );
printf( "Enter the number of columns in the 2nd matrix:\t");
scanf_s("%u", &cols );
// Allocate the 2D arrays:
M1 = CreateMatrix( rows, comm );
M2 = CreateMatrix( comm, cols );
M3 = CreateMatrix( rows, cols );
// Input values:
InputMatrix( M1, rows, comm, "Matrix 1" );
InputMatrix( M2, comm, cols, "Matrix 2" );
// Calculate the dot product.
for( i=0; i
for( j=0; j
for( k=0; k
M3[i][k] += M1[i][j] * M2[j][k];
// Report.
PrintMatrix( M1, rows, comm, "\nMatrix 1" );
PrintMatrix( M2, comm, cols, "Matrix 2" );
PrintMatrix( M3, rows, cols, "Dot Product of Matrix 1 and Matrix 2" );
printf("\n");
// Release memory.
ReleaseMatrix( M1, rows );
ReleaseMatrix( M2, comm );
ReleaseMatrix( M3, rows );
return( 0 );
}
int ** CreateMatrix( UINT rows, UINT cols )
{
UINT i = 0;
int ** Matrix = (int **) malloc(rows * sizeof( int * ));
for( i=0; i
{
Matrix[i] = (int *) malloc( cols * sizeof( int ));
memset( Matrix[i], 0, cols * sizeof( int ));
}
return( Matrix );
}
void InputMatrix( int ** Matrix, UINT rows, UINT cols, char * Title )
{
printf( "\nValues for %s:\n\n", Title );
UINT i=0, j=0;;
for( i=0; i
{
for (j=0; j
{
printf("Enter value for row %u, col %u:\t", i+1, j+1);
scanf_s("%d", &Matrix[i][j]);
}
}
}
void PrintMatrix( int ** Matrix, UINT rows, UINT cols, char * Title )
{
printf( "\n%s:\n\n", Title );
UINT i=0, j=0;
for( i=0; i
{
for (j=0; j
printf( "%4d", Matrix[i][j] );
printf("\n");
}
}
void ReleaseMatrix( int ** Matrix, UINT rows )
{
UINT i = rows;
while(i)
free( Matrix[--i] );
free( Matrix );
}
how to write a program that counts automorphic number from 1 to 999
Don't write, it is already written, google for 'cpp'.
Divide it by 1000.
there is no solution of this problem...........that's it..........
Its limited only by available memory.
No.
how to write a program that counts automorphic number from 1 to 999
By learning how to program on C+.
Don't write, it is already written, google for 'cpp'.
No.
Divide it by 1000.
You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.
Its limited only by available memory.
there is no solution of this problem...........that's it..........
printf ("x")
Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.
int x= 1; int y= 2;