?
Solved

Array problem

Posted on 1998-12-14
4
Medium Priority
?
174 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:muhbest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 150 total points
ID: 1180179
#include <iostream.h>

/* no point in passing int m=4 if a[4][4] is hard coded */
/* the question you were given is not possible to answer */
/* exactly as specified.  but we cannot do much */
/* about that now... */

/* also the function should return an int not a void */
/* void identity(int a[4][4], int m) { */
int identity(int a[4][4], int m) {
      /* no point initializing i and j as they are */
      /* set in the loop */
      /*      int i=m;      */
      /*      int j=m;      */
      int i;
      /* you have hard-coded 4 in the for loops */
      /*      for(i=0;i<4;i++){      */
      /*            for(j=0;j<4;j++){      */
      /* we should use 'm' instead */
      for(i=0;i<m;i++){
            /* declare j here where it is used */
            /* this is nicer code */
            int j;
            for(j=0;j<m;j++){
                  /* this is CREATING an identity matrix */
                  /* NOT testing for one as was asked */
                  if(i==j)
                        a[i][j]=1;
                  else
                        a[i][j]=0;
                  /* this is not what should be returned */
                  return a[i][j];
            }
      }
      /* no return value from here at all */
      /* a good compiler will detect this error */
}

int main(int argc, char* argv[]) {
      /* you haven't put any data into a[][] yet */
      int a[4][4];
      int m=4;
      /* you haven't done anything with the */
      /* return value from identity */
      /* also the call is wrong .. you are passing */
      /* the value of the [4][4] item of the array */
      /* (which is outside it anyway) instead of the */
      /* whole array */
      /*      identity(a[4][4], m); */
      int result = identity(a, m);
      /* i and j aren't defined */
      /* and you should be printing the result of */
      /* the call to identity(...); */
      /* BTW: if this is supposed to be 'C' code */
      /* then you cannot use 'cout<<' etc */
      /* cout<<a[i][j]; */
      cout << result;
      cout<<'\n';
      return 0;
}

/* I suggest you think about the question */
/* a bit more and come back to me */


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1180180
PS: sorry about formatting .. that is what happens when I copy/paste source code into this silly edit box :-(

0
 
LVL 22

Expert Comment

by:nietod
ID: 1180181
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.)
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1180182
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.


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question