tags:

views:

88

answers:

3

whats the logic to find the sum of alternate elements of a two-dimensional matrix.

+2  A: 

you could cast the 2D array into 1-D array and do the summation in a loop.

int *a = (int*)arr; //arr being the 2 D array
int sum = 0;
for(int i=0;i<M*N;i+=2)
{
   sum += a[i];
}
SysAdmin
Only works if the array is contiguous
Paul R
@Paul R - he has tagged the question under turbo C++. in C/C++ an array is contiguous
SysAdmin
@SysAdmin: in C/C++ there are two very different ways of declaring a 2D array: (i) int a[M][N] and (ii) int **a. (i) is contiguous but (ii) is not, in general.
Paul R
@Paul R: (ii) is not the thing which, in C and C++, is called a 2D array. (ii) may be called a 2D array in other languages.
Steve Jessop
@Steve: I disagree - in C/C++ (ii) is the only way to declare a 2D array where the dimensions will be determined at run-time.
Paul R
@Paul R: What part of what I said do you disagree with? Multidimensional arrays are defined in 8.3.4 to be (i). Where in the standard is (ii) referred to as a multidimensional array? (ii) declares "something" where the dimensions are determined at runtime, but "something" is not a C or C++ 2D array. It's (a ptr to the 1st element of) an array of ptrs to (1st elements of) arrays. It may suit the questioner's purposes (he said "matrix", after all, not "array"). Or a vector<vector<int> > could. That also allows runtime determination of both dimensions, and is easier to resource-manage than (ii).
Steve Jessop
@Steve: the fact that the C++ standard does not cover this is immaterial - (ii) has always been the canonical method for implementing dynamically sized 2D arrays in C. C++ adds alternate methods via various forms of syntactic sugar, but in the current discussion we're talking about `C/C++` (see above).
Paul R
@Pau: I don't really know what language you mean by "C/C++", the questioner says C++. Anyway, in the C standard multidimensional arrays are defined in 6.5.2.1 of n1256.pdf, and they are (i), not (ii), and this has always been the definition of multidimensional arrays in both C and C++. Neither C nor C++ *has* dynamically-sized multidimensional arrays, and what you are canonically doing using (ii) is something other than a multidimensional array.
Steve Jessop
@Steve: Well it was SysAdmin who originally claimed that *"in `C/C++` an array is contiguous"* above. But ultimately usage trumps standards, and pragma trumps dogma - look at any C image processing libraries and you'll find that images are implemented as `T **` so that they can be dynamically sized. You may pedantically insist on not calling them 2D arrays or matrices, but in practice that is what they are.
Paul R
SysAdmin's statement is true of both C and C++, so saying "C/C++" doesn't introduce the problem that you do by *contrasting* "C/C++" with C++. I haven't been into the guts of any C image processing code other than bespoke stuff, but my experience is that it's not unheard-of to use T* together with a stride. "ultimately usage trumps standards" - if you use the phrase "in C", but then introduce terminology which conflicts with that of the standard, then you may be in numerous company, but not IMO particularly good company ;-p I say, why have a standard if you aren't going to use it?
Steve Jessop
A: 
 for (int i = 0; i < N; ++ i)
   for (int j = i%2; j < M; j += 2) {
     std::cout << i << "," << j << std::endl;
     sum += a[i][j];
   }
KennyTM
+3  A: 
//arr is the 2 D array MxN
int sum = 0;
for(int j=0;j<N;j++)
{
  for(int i=j&1;i<M;i+=2)
  {
      sum += a[i][j];
  }
}
Kamil Szot
anurag18294
Thomas Matthews