2 Replies - 323 Views - Last Post: 03 July 2013 - 05:13 PM Rate Topic: -----

#1 winniekaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-July 13

print matrix

Posted 03 July 2013 - 03:46 AM

Write a program using nested loop to print out a $n$ x $n$ matrix with 1s on diagonal and zeros in the rest.
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0

i know how to make it print like this:
10000000
01000000
00100000
00010000
00001000
00000100
00000010
00000001

could anyone help me print the inverse :helpsmilie:


#include<iostream>
using namespace std;

int main()
{

	/*int i = 1;
	int j = 1;*/

	int size;
	cin >> size;
	

	for(int i=1; i <= size; i++)		//row
	{
		for(int j=1; j <= size; j++ ) // column
		{
			if (i==j)
			{
				cout << "1";
			}	
			else
				cout << "0";
	
		}

		//cout << "1" << endl;
		cout<< endl;
	}
	return 0;



Is This A Good Question/Topic? 0
  • +

Replies To: print matrix

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,386
  • Joined: 25-December 09

Re: print matrix

Posted 03 July 2013 - 07:39 AM

What have you tried? Do you realize that a loop can start at "size" and be decremented?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 winniekaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-July 13

Re: print matrix

Posted 03 July 2013 - 05:13 PM

thank you...i got this one solved :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1