GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Oct-2005, 08:22
stguisy stguisy is offline
New Member
 
Join Date: Oct 2005
Posts: 5
stguisy is on a distinguished road

Nested For-loops problem


I am attempting to create a code where the user inputs an equal number of rows and clumns and the output is the identity matrix. For example, with 3 rows and 3 columns, the output would look like this:

100
010
001

I am required to use nested for-loops. Here is what I thought would work, but won't:

CPP / C++ / C Code:
//identityMatrix.cpp

#include<iostream>
using namespace std;

int main(void)
{
   int rows;
   int columns;
   while(rows != columns)
   {
   cout << "Columns and rows must be equal. \n Please enter number of rows: ";
   cin >> rows;
   cout << endl << "Please enter number of columns: ";
   cin >> columns;
   cout << endl;
   }
   for(int i = 1; i < (rows + 1); i = i + 1)
   {
      for(int j = 1; j < (columns + 1); j = j + 1)
      {
	 if(i = j)
	    cout << "1";
	 else
	   cout << "0";	  
      }
      cout << endl;
   }
}

Any help I could get would be much appreciated. Thank you!
  #2  
Old 14-Oct-2005, 09:48
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Nested For-loops problem


Hi,

Just change
CPP / C++ / C Code:
 if(i = j)
    cout << "1";

to this:
CPP / C++ / C Code:
 if(i == j)
    cout << "1";
I hope you understand what error you have committed.

The double equals sign == is the C notation for ``is equal to''. This symbol is used to distinguish the equality test from the single = that C uses for assignment. A word of caution: The result is usually a legal expression, so you will get no warning.

Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 14-Oct-2005, 10:14
stguisy stguisy is offline
New Member
 
Join Date: Oct 2005
Posts: 5
stguisy is on a distinguished road

Re: Nested For-loops problem


Thank you so much, Paramesh! I am so new at this and I never realize when my silly mistakes are there.
  #4  
Old 14-Oct-2005, 10:28
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Nested For-loops problem


You are Welcome.

and Read this $ 20 Million Bug story:

Quote:
The $20 Million Bug:
In Spring 1993, in the Operating System development group at SunSoft, we had a "priority one" bug report come in describing a problem in the asynchronous I/O library. The bug was holding up the sale of $20 million worth of hardware to a customer who specifically needed the library functionality, so we were extremely motivated to find it. After some intensive debugging sessions, the problem was
finally traced to a statement that read :
CPP / C++ / C Code:
x==2; 

From: Expert C Programming-Deep C Secrets
By Peter van der Linden

Cheers,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
Last edited by Paramesh : 14-Oct-2005 at 10:59.
  #5  
Old 14-Oct-2005, 13:18
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

Re: Nested For-loops problem


What you have works, but just a couple notes on possibly making it easier to read and easier to program.

instead of useing
CPP / C++ / C Code:
  for(int i = 1; i < (rows + 1); i = i + 1)
   {
      for(int j = 1; j < (columns + 1); j = j + 1)
      {
	 if(i = j)
	    cout << "1";
	 else
	   cout << "0";	  
      }

try this... it does the same thing you can replace
--------------------------------
i < (rows + 1) with i <= rows
--------------------------------
j < (columns + 1) with j <= columns
--------------------------------

Also cpp as a build in operator for adding or subtracting 1 from a number, for example.
--------------------------------
i = i + 1 is the same as i++
--------------------------------
i = i - 1 is the same as i--
--------------------------------

also a couple more handy operators are += and -= there are also *= and /= operators that work in the same way.
--------------------------------
i = i + n is the same as i += n
--------------------------------
i = i - n is the same as i -= n
--------------------------------

CPP / C++ / C Code:
  for(int i = 1; i <= rows; i++)
   {
      for(int j = 1; j <= columns; j++)
      {
	 if(i == j)
	    cout << "1";
	 else
	   cout << "0";	  
      }

Not sure if u will ever check back on this post but those are just a couple of operators in c++ that you might find handy. They make my life much easier, hope it helps you too.
__________________
There are 10 kinds of people in this world. Those who speak binary and those who dont.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03
Help with nested for loops...please. sorry newbie keperry C++ Forum 14 16-Oct-2004 11:25
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:50.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.