Write a program to read two matrices of real numbers (up to 10 x 10 in size) and print out the number of rows which are identical between the two matrices and the number of columns which are identical. The input consists of two integers, r and c, specifying the number of rows and columns in the matrices (they have the same dimensions), followed by the values of matrix1 (r rows of c real numbers) followed by the values of matrix2 (r rows of c real numbers). Your program must consist of a main program and the following two functions.
int count_equal_rows(int r, int c, double matrix1[][10], double matrix2[]][10])
int count_equal_colums(int r, int c, double matrix1[][10], double matrix2[]][10])
my program:
int count_equal_rows(int r, int c, double matrix1[][10], double matrix2[][10])
{
int match=0,i,j,row,col;
scanf("%d%d%lf%lf",%26amp;row,%26amp;col,%26amp;matrix1[...
for(i=0;i%26lt;row;++i)
{
if (matrix1[i][j]= matrix2[i][j])
match +=1;
}
return(match);
}
Help me with this c program(arrays)?
Here it is dear:
#include%26lt;stdio.h%26gt;
int count_rows(int r,int c,double a[][10],double b[][10])
{
int dup_rows,rowmatch,i,j;
rowmatch=dup_rows=0;
for(i=0;i%26lt;r;i++)
{
rowmatch=0;
for(j=0;j%26lt;c;j++)
{
if(a[i][j]==b[i][j])
{
rowmatch++;
}
}
if(rowmatch==c)
{
dup_rows++;
}
}
return dup_rows;
}
int count_cols(int r,int c,double a[][10],double b[][10])
{
int dup_cols,colmatch,i,j;
colmatch=dup_cols=0;
for(i=0;i%26lt;c;i++)
{
colmatch=0;
for(j=0;j%26lt;r;j++)
{
if(a[j][i]==b[j][i])
{
colmatch++;
}
}
if(colmatch==r)
{
dup_cols++;
}
}
return dup_cols;
}
void main()
{
double a[10][10],b[10][10];
int i,j,r,c,rows,cols;
printf("\n Enter rows %26amp; cols : ");
scanf("%d%d",%26amp;r,%26amp;c);
printf("\n Enter the elements : ");
for(i=0;i%26lt;r;i++)
{
for(j=0;j%26lt;c;j++)
{
scanf("%lf",%26amp;a[i][j]);
}
}
printf("\n Enter the elements : ");
for(i=0;i%26lt;r;i++)
{
for(j=0;j%26lt;c;j++)
{
scanf("%lf",%26amp;b[i][j]);
}
}
rows=count_rows(r,c,a,b);
cols=count_cols(r,c,a,b);
printf("\n Duplicate Rows are : %d .",rows);
printf("\n Duplicate Columns are : %d .",cols);
}
OM NAMAH SHIVAY
Reply:The scanf should be placed in the main program, that way you only need to read the matrices once to perform both tests.
The main program should be like this:
(1) Declare 10x10 arrays
(2) Read dimensions
(3) Read elements of matrix1
(4) Read elements of matrix2
(5) Invoke count_equal_rows
(6) Invoke count_equal_columns
(7) Display results
You need to test each pair of elements in the row to see in the entire row is equal. The algorithm, using a pseudo-language, would be:
is-this-row-equal returns boolean with
_ parameter rows as integer (1..10)
_ parameter cols as integer (1..10)
_ parameter i as integer (0 .. rows-1)
_ parameter matrix1 as array (rows x cols) of double
_ parameter matrix2 as array (rows x cols) of double
begin
_ variable are-equal as boolean %26lt;- true
_ repeat j as integer (0 .. cols - 1) do
___ if matrix1@(i,j) neq matrix2@(i,j) then are-equal %26lt;- false
_ done
_ return are-equal
end
It shouldn't be difficult for you to write that in C to use inside your match-counting loop.
(There, I think I managed to have some similarities to shell script, fortran, pascal, vhdl, and register transfer notation at the very least)
Reply:man man man .. where is the main function, you should read the two matrixes in the main function not inside the count functions ..
void main()
{
doucble mat1[][], mat2[][];
int r, c;
printf("Enter number of rows: ");
scanf(%d, %26amp;r);
printf("Enter number of columns: ");
scanf(%d, %26amp;c);
double mat1[r][c], mat2[r][c];
int row_match = count_equal_rows(r, c, mat1, mat2);
int col_match = count_equal_columns(r, c, mat1, mat2);
}
I think this is enough .. sorrry, but for your advantage you should write the remaining code .. it is easy .. you can do it man ..
Reply:Well, first of all you want a double ==. This is a common mistake. You have = (matrix 1 v matrix 2) which assigns the value. I particularly dislike that part of C, it's prone to many errors.
Second you'll want to scan through the whole row and only return a count if the whole row matches.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment