473,465 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

n x n matrix transposition recursive algorithm.

Warm welcome to all of you, programmers.

In fact I'm beginning my journey with coding, and I just encountered
my first real problem. I would like to find the solution by my own, but
it isn't easy at all.

My main goal is to code simple console application, which can
transpose n x n matrix. I need some help with the algorithm itself. As
far I do know, it should use recursion. I consider it as a challenge.

But can you spare me some tips ? Actually I have no clue how to shape
it up.

best regards !

M.

Apr 5 '06 #1
3 8790
"maev" <ma*****@gmail.com> schrieb im Newsbeitrag news:11*********************@e56g2000cwe.googlegro ups.com...
My main goal is to code simple console application, which can
transpose n x n matrix. I need some help with the algorithm itself. As
far I do know, it should use recursion.


Why do you think you should use recursion? Transposing a matrix A simply means swapping all elements A[i][j] and A[j][i]. I don't see any recursion there, only two nested loops:

for (int i = 0; i < n; ++i)
for (int j = 0; j < i / 2; ++j)
std::swap(a[i][j], a[j][i]);

Heinz
Apr 5 '06 #2

maev wrote:
Warm welcome to all of you, programmers.

In fact I'm beginning my journey with coding, and I just encountered
my first real problem. I would like to find the solution by my own, but
it isn't easy at all.

My main goal is to code simple console application, which can
transpose n x n matrix. I need some help with the algorithm itself. As
far I do know, it should use recursion. I consider it as a challenge.

But can you spare me some tips ? Actually I have no clue how to shape
it up.


Presumably you have enough C++ understanding to write a console program
and to implement some logic once you know what logic you need to
implement.

Get a pencil and a piece of paper. Write down step by step instructions
for how to transpose a 2x2 matrix. Write them in sufficient detail (a
separate instruction for each logical action) that you could give any
2x2 matrix, plus your step by step instructions, to a non-mathematician
and they could transpose the matrix for you. Note that you haven't been
near a C++ compiler yet.

Now, start with

int main()
{
// Define a variable to hold your initial matrix and another
// to hold your transposed matrix. I would suggest a
// std::vector of std::vectors.

// Your logic goes here.

// Display the initial and the transposed matrices
// so you can see that the program worked.
}

and replace

// Your logic goes here

with the C++ implementation of your step by step instructions.

That's 2x2 done. Go back to your pencil and paper and write out the
instructions for transposing a 3x3 matrix. Go down to the same level of
detail. Consolidate your two sets of instructions into one set that
doesn't explicitly mention the matrix size anywhere.

Modify the logic in your program to follow this latest set of
instructions. Now you've got a program that transposes an n x n matrix.

If you have any problems getting code to work, the guidelines in this
FAQ explain how to get help.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Gavin Deane

Apr 5 '06 #3
Thank you Heinz for fast and clearly answer. I am ashamed because of
simplicity of the solution. I seriously need to work with my
concetration and focus.

As for me, next thing to do is to implement this one in simple MPI
application. Actually I mean coding parallel calculation of matrix
transposition with using from 3 - 4 processors. Off to work I go !

To define recursion we must first define recursion ;-)

Mae

Apr 5 '06 #4

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

Similar topics

7
by: sangeetha | last post by:
Hi, i need to transpose a nx1 matrix to 1xn matrix inorder to multiply with nxn matrix in c language ... can anyone help in this coding ..the nx1 matrix is pi the data type is double *pi..this is...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
8
by: lovecreatesbeauty | last post by:
I write a function to rotate a matrix by 90 degrees clockwise, this function works on a matrix of specific size, for example, it rotates a 4*4 matrix of integers in the following code. The function...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
2
by: Zhengzheng Pan | last post by:
Hi all, I'm trying to check whether a (stochastic/transition) matrix converges, i.e. a function/method that will return True if the input matrix sequence shows convergence and False otherwise....
4
by: RSH | last post by:
Okay my math skills aren't waht they used to be... With that being said what Im trying to do is create a matrix that given x number of columns, and y number of possible values i want to generate...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
0
by: Bas | last post by:
On Sep 22, 10:02 am, Al Kabaila <akaba...@pcug.org.auwrote: That argument might have been valid 3 years ago, but as already said by others, Numeric and Numarray are deprecated. Numpy should be the...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.