473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Printing the diagonal elements of an array using for loop..

Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Thanks in advance
Bala...
Aug 10 '07 #1
11 66726
JosAH
11,448 Expert 8TB
Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Thanks in advance
Bala...
Just think a bit: what is characteristic about the index values of the elements
on the diagonal of a matrix? And what is 'Plzzzzzz'? Keyboard got stuck?

kind regards,

Jos
Aug 10 '07 #2
Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Thanks in advance
Bala...
develop one for loop for row traversing in the array and
inside this nest another for loop for column traversing within
the row.

In side the inner for loop just check whether row value and
column value are equal; if yes then print the array element
with row and column subscripts.

Do you feel you got the solution or still struggling ?

Regards,
Girish.
Aug 10 '07 #3
Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Thanks in advance
Bala...

Hi,

Try out this ....


for(int i=0;i<3;i++)
{
for (int j=0;j<=i;j++)
{
if(i==j)
{
cout<<"the diagonal elements are"<<a[i][j];
}
}
}

U too could have even tried...anyways...try the above

Gauri.
Aug 10 '07 #4
JosAH
11,448 Expert 8TB
Hi,

Try out this ....

<code snipped>
Better not; a nested loop is not necessary to iterate over the diagonal of a matrix.

kind regards,

Jos
Aug 10 '07 #5
__0 1 2 3
0| 1 0 0 1 |
1| 0 1 0 1 |
2| 1 0 0 0 |
3| 1 0 1 1 |

coordinates of diagonal elements
(0,0)
(1,1)
(2,2)
(3,3)

don't you see the pattern :) ?

Sam
Aug 11 '07 #6
I aggree with Josah, a single loop will do the job.

Also remember that the diagonal of a matrix (array) only makes sense if the matrix is square, ie number of rows equals number of columns.

Expand|Select|Wrap|Line Numbers
  1. int rows;
  2. int columns;
  3.  
  4. //Assign a value to rows and columns
  5.  
  6. //Also assign values to the matrix
  7.  
  8. if(rows !=columns){
  9.     cout<<"Cannot obtain the diagonal of a non-square matrix\n";
  10. }
  11. else{
  12.      for(int i=0;i<rows;i++){
  13.            cout<<a[i][i]<<endl;
  14.      }
  15. }
  16.  
Aug 11 '07 #7
Lestat
1
Just go with the program hint given by Gauri this program
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<3;i++)
  2. {
  3. for (int j=0;j<=i;j++)
  4. {
  5. if(i==j)
  6. {
  7. cout<<"the diagonal elements are"<<a[i][j];
  8. }
  9. }
  10. }
Jan 22 '13 #8
but what if we want to print both the digonals????
Jan 31 '14 #9
Banfa
9,065 Expert Mod 8TB
There is still a relationship between the 2 indexes you just need to work it out.
Jan 31 '14 #10
I think this should work
#include<stdio.h>
int main()
{
int a[3][3],i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{

scanf("%d",&a[i][j]);


}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)
{
printf("%d",a[i][j]);
}

}


}
Sep 25 '15 #11
Mathss
1
In Java,

for(int i=0; i<Arr.length; i++){
System.out.println( Arr[i][i] + " " + Arr[i][Arr.length-1-i]);
}
Feb 5 '19 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nigel | last post by:
I successfully create a .NET Component (Visual Basic .NET) that would print, unfortunately when used within a web browser it appears that .NET security doesn't allow you to run code that interacts...
6
by: Vikram | last post by:
I have added some input elements on a page using javascript at client side. when i submit the page, i am unable to access the values of input elements created using request.form. Are elements...
31
by: arun | last post by:
suppose i have a pointer to an array of integers.can i initialize each member of the array using pointers?plz explain
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
1
by: phfle1 | last post by:
Hi, Is it possible to change the cursor style for all elements without using the document.body.style.cursor . I'm trying to change the cursor globaly, but I have problems with it since my...
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
2
by: serave | last post by:
Hi, i need some help printing a mutlidimensional array, i can do it for square Matriz MxM but i need to print an MXN matrix. here is my code : function printMatrix($matrix){ echo "<table...
2
by: mvalor | last post by:
Can anyone tell me what would be the general staement in code to load values into an array using a For Loop? mvalor
2
by: hanaa | last post by:
Hello I need to call a function that returns a 2D array. I need to dynamically allocate memory for the array in the callee. Now, I return using a pointer to a pointer. In the callee, I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.