472,328 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Can I print elements of a simple matrix with out manual nested loop?

Suppose I have a matrix M x N dimension.
Normally, to print all of the elements in the matrix. I write the
following codes:

Suppose M = 5, N = 4:

for(int j=0; j<5; ++j) {
for(int k=0; k<4; ++k) {
cout << matrix[M][N] << " ";
}
cout << endl;
}

If my matrix class has method for return number of dimension and
dimension size such as:

numDimension(matrix); // return 2 becase the matrix has M x N = 2
dimensions.

sizeDimension(0); // return 5 -- size of M
sizeDimension(1); // return 4 -- size of N

How to write some code that print all element of the matrix with out
code nested loops by hand?

Thanks

Nalerk
Jul 23 '05 #1
1 1560
SUPER_SOCKO wrote:
How to write some code that print all element of the matrix with out
code nested loops by hand?


You can just create a double loop, where the outermost loop iterates of
the number of dimensions, while the inner loop goes from 0 to
"sizeDimension(current_dim)".

However, you will need to decide yourself how to lay out a -- say --
5-dimensional matrix in the 2-dimensional "cout space".

You will also need to calculate your indices yourself somehow, but your
already probably have your "matrix" class to help you out here, don't
you? (Or how does it otherwise implement matrix[n][m] in a 2-dim.
matrix, and say matrix[n][m][p][q][r] in a 5-dimensional one?) Along the
way, you will need to maintain som state variables, to keep track of
(and temporarily remember) indices you are starting at in each
direction. Some stack- or list-like functionality probably; hint:
std::vector.

Another way to solve this latter is considering some kind of recursive
implementation.
Regards,

-+-Ben-+-
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array...
4
by: Yudan Yi | last post by:
I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if...
8
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible...
2
by: Fred | last post by:
Hi. Sorry, but I don't know where else to ask. I wrote a access shipping database and we currently use the web interface for DHL to print...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is...
5
by: CindyRob | last post by:
Using .NET framework 1.1 SP1, .NET framework SDK 1.1 SP1, Visual Studio .NET 2003, hotfixes 892202 and 823639. I create a proxy class using...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an...
1
by: Henrik Bechmann | last post by:
All, I'm trying to spoof Google's vertical tabs in a vertical menu structured with nested UL/LI elements. To do this, I need to find out where...
4
by: pc_whocares | last post by:
My forehead is flat from pounding. I am building a DLL in VS2005 C++ for use in another software development platform. I am required to pass...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.