muhbest
asked on
Array problem
My code
=======
#include<iostream.h>
void identity(int a[4][4], int m){
int i=m, j=m;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(i==j)
a[i][j]=1;
else
a[i][j]=0;
return a[i][j];
}
}
}
void main(){
int a[4][4];
int m=4;
identity(a[4][4], m);
cout<a[i][j]
}
cout<<'\n';
}
My problem
==========
Write a function 'identity' which returns a 1 if the array argument passed to it represents an identity matrix, and returns 0 otherwise. An identity matrix is an m by m array of integers, where the values of the elements on the principle diagonal (row subscript == col subscript)equal 1, and all other elements are 0. for
example:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
is an identity matrix of order 4 by 4,
1 0 0
0 1 0
0 0 1
is an identity matrix of order 3 by 3,and
1 1 5
0 1 0
0 0 1
is NOT an identity matrix. The function identity is passed two arguments: the array, and the order of the matrix ( i e the size of the two dimensions). Thus the prototype of the function would be written as :int identity (int a[][], int m);where a is the array and m represents the number of rows and columns. You may assume that the number of rows always equals the number of columns for the array.
=======
#include<iostream.h>
void identity(int a[4][4], int m){
int i=m, j=m;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(i==j)
a[i][j]=1;
else
a[i][j]=0;
return a[i][j];
}
}
}
void main(){
int a[4][4];
int m=4;
identity(a[4][4], m);
cout<a[i][j]
}
cout<<'\n';
}
My problem
==========
Write a function 'identity' which returns a 1 if the array argument passed to it represents an identity matrix, and returns 0 otherwise. An identity matrix is an m by m array of integers, where the values of the elements on the principle diagonal (row subscript == col subscript)equal 1, and all other elements are 0. for
example:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
is an identity matrix of order 4 by 4,
1 0 0
0 1 0
0 0 1
is an identity matrix of order 3 by 3,and
1 1 5
0 1 0
0 0 1
is NOT an identity matrix. The function identity is passed two arguments: the array, and the order of the matrix ( i e the size of the two dimensions). Thus the prototype of the function would be written as :int identity (int a[][], int m);where a is the array and m represents the number of rows and columns. You may assume that the number of rows always equals the number of columns for the array.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
PS: sorry about formatting .. that is what happens when I copy/paste source code into this silly edit box :-(
RONSLOW, I think the assignment might expect a 1-D array that is to be treated as a 2D matrix, that is the only way to make it sizable at run-time. (That or a 1-D array of 1-D array pointers, but that really needs to be a class.)
It could also be that the lecturer who posed the assignment stuffed up by saying "the prototype of the function would be written as :int identity (int a[][], int m)"
Anyway, as posed in the question, one cannot write such as function (it ain't valid C or C++). If a was a flat array of m*m elements then that could be easily done.
Anyway, as posed in the question, one cannot write such as function (it ain't valid C or C++). If a was a flat array of m*m elements then that could be easily done.