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

ex: square matrix of 3X3
enter the no. of rows and columns: 3
3
enter the elements:1
2
3
4
5
6
7
8
9
output:
1 2 3
4 5 6
7 8 9

the diagonal elements are: 1,5,9

2007-11-19 00:32:58 · 3 answers · asked by sonam_tuteja 2 in Computers & Internet Programming & Design

3 answers

for(int i=0;i for(int k=0;k if(i==k)
printf("%d",matrix[i][k]);

this is the worst algorithm but it works I think. hmm maybe yep ther we need a break let me put it this way

for(int i=0;i for(int k=0;k if(i==k){
printf("%d",matrix[i][k]);
break;
}

that break will change a lot if matrix get bigger and actually there are a lot of ways to solve this just try to think like you are looping both sides/dimensions(width and height) and that way you can find diagonal easily ;)

2007-11-19 00:45:28 · answer #1 · answered by mascix 2 · 0 0

You have two for loops
The first uses i
The second loop is within the first and uses j
in the second loop
if (i=j)
print matrix[i,j]
end-if
end-loop
end-loop

2007-11-19 00:44:46 · answer #2 · answered by AnalProgrammer 7 · 0 0

void considerable() {int i,j,n,a[20]; cout<"n=";cin>>n; //n is the form of the rows of the matrix for ( i=a million;i<=n;i++) for(j=a million;j<=n;j++) {cout<<"a["<>a[i][j];} int s=0; for ( i=a million;i<=n;i++) for(j=a million;j<=n;j++) { if ( i == ok ) s = s + a[i][j];} cout<

2016-12-09 01:43:27 · answer #3 · answered by ? 4 · 0 0

fedest.com, questions and answers