English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

ie suppose the matrix which has 3 rows and 3 columns
eg:-
23 45 67
56 34 90
12 23 22
then, when finding the sum of diagonal elements dont take the middle most one(34) twice.ie 23+34+22+67+12(34 should be taken only once)

2007-02-04 00:16:25 · 3 answers · asked by sunitha a 1 in Computers & Internet Programming & Design

3 answers

There is an answer from someone already
but i will but what i know in case only



int Mat [3][3];
int DSum=0;

/* To Fill by a person */
for (i = 0; i < 2; i++)
{
for (j = 0; i < 2; j++)
{
cin>>Mat[i][j];
}
}

/*---To Sum ----------*/

for (i = 0; i < 2; i++)
{
for (j = 0; i < 2; j++)
{
if (i==j) DSum += Mat[i][j]
else
{
if ((i==0) and (j==2) ) DSum += Mat[i][j]
else
{
if ((i==2) and (j==0) ) DSum += Mat[i][j]
}
}
}
}
/* --- End --- */

2007-02-04 02:37:27 · answer #1 · answered by -=*QA*=- 2 · 0 1

sum= 0;
for (i = 0; i < dimension; i++) {
sum += matrix[i][i];
sum += matrix[dimension - i - 1][i];
}
if ((dimension % 2) == 1) {
middle = dimension / 2;
sum -= matrix[middle][middle];
}

2007-02-04 02:01:23 · answer #2 · answered by irwtan9 1 · 0 0

well, here is mine:

01 for(i=0; i < dim; i++){
02      sum += (dim -i -1 == i)? array[i][i] : array[i][i] + array[i][dim -i -1];
03 }

2007-02-04 04:33:01 · answer #3 · answered by Fox 3 · 0 0

fedest.com, questions and answers