Home » Interview Questions » Interviews » Story Details
Printable Version

Interview Question: Print both diagonals of a n*n matrix

by Kamal Rawat on Feb 15, 2012

Given a n*n matrix, write code to print both the diagonals (top-left to bottom-right and bottom-left to top-right)
Comments: 0    Views: 516

MCN Professionals | Interview Question of the day

To receive one interview question in your mailbox daily, please subscribe to "A Question A Day" at MCN Professionals home page.
-----------------------------------------------------------------------------------------------------------------------------------------

Today's Question:

Given a matrix of order N*N, write code to print both the diagonals of that matrix. For example: the matrix and its 2 diagonals are given below:
mat_diagonal.jpg
In the above diagram, I have colored the elements in first diagonal as red and elements in 2nd diagonal as green.
Solution:

Printing first diagonal (Red):
All the elements of first diagonals are at (i,i) positions, do it is easy to print the first diagonal
for (int i=0; i<n; i++)
   cout << arr[i][i];
Note that some of my students have written the code like this:
for(int i=0; i<n; i++)
    for(int j=0; j<n; j++)
        if(i==j)
            cout << arr[i][j];
But, this is unnecessarily traversing all the elements of the matrix (even elements which are not diagonal). Hence the complexity of this code is O(n2). Where as the code above (with one loop) takes O(n) time.
Printing Second diagonal (Green):
The elements in the second diagonal are at following positions
(3,0)  (2,1)  (1,0)   (0,3)
or, if I generalize:
(n-1, 0) (n-2, 1) ..... (0, n-1)
Hence, the code will be:
for(int i=0; i<n; i++)
    cout << arr[n-1-i][i];
The complete code (function) which will print both the diagonals will be
#define N 4
void printdiagonal(int arr[N][N])
{
   cout<< "First Diagonal :";
   for(int i=0;i<N;i++)
      cout<< " "<<arr[i][i];
   
   cout<<"\n Second Diagonal :";
   for(int i=0;i<N;i++)
      cout<< " "<<arr[N-i-1][i];
}
---------------------------------------------------------------------------

Interview Questions Archive:

         To see all the questions in the category Click Here...


Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName