matrices array

Hi i am new to C++ and want to know how to create an n x n matrix where the numbers go in sequential order in the first row and first column and then continue to increase until n is reached again, whereby the loop starts from 1 again.
e.g. a 5x5 matrix would look like:

1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4

here is what I have so far:


any help would be much appreciated!
Last edited on
thanks
Last edited on
The code in your second post will not compile. The size of an array must be constant.

http://www.cplusplus.com/doc/tutorial/arrays/
I am not sure what you want to achieve with this line?

 
A[i][j] = (i + 1, j + 1); // ?? 


What it will do is use the value j + 1 and ignore the value i + 1.

I suspect you really want this:

 
A[i][j] = i + j + 1;


But that still doesn't solve your whole problem because the numbers still don't wrap round.

You need some calculation that will start again from zero if it goes above the maximum size.

Try looking into the % operator. That can help you here!

Last edited on
to Archaic: I have now ammended it and it should now work, although I am not sure why it wasn't compiling for you.

to Galik: with respect to that line, i have altered that too and this is my new code:

but now i need help making every even number negative.
i.e.
1 -2 3 -4 5
-2 3 -4 5 1
3 -4 5 1 -2
-4 5 1 -2 3
5 1 -2 3 -4

I can get every other element to be negative but the problem arises when you loop around from an odd number back to 1 (i.e. from 5 to1 in this example or 7 to 1 etc.)

Any help on this would be much appreciated

Last edited on
by the way thanks for the help so far guys! appreciate it
That's great you cracked it!

By the way, you don't have to call the function fmod() because c++ has an operator that will do that with integers:

A[i][j] = ((i + j) % n) + 1;

That would be a lot faster.

% is the 'modulus' operator, it gives the remainder after an integer division.
Last edited on

but now i need help making every even number negative.
i.e.
1 -2 3 -4 5
-2 3 -4 5 1
3 -4 5 1 -2
-4 5 1 -2 3
5 1 -2 3 -4


One way to do that might be to divide the number by 2 and check the remainder. The modulus operator again:

 
v % 2


That would give 0 or 1; 0 for even and 1 for odd numbers.

But you want your output to be -1 or 1, -1 for even and 1 for odd numbers.

We can get that by scaling the values, or multiplying them to give a difference of 2, and then shifting them into our range.

Consider:

 
(v % 2) * 2 


That gives a number in the range 0 and 2. But if we subtract 1 from that we would get -1 and 1, just what we were looking for:

 
((v % 2) * 2) - 1





Last edited on
thanks a lot Galik! you have been really helpful!

I have figured out a way using fmod again though to make the even numbers negative.

I was wondering if you know how to find permutations using factoriadic numbers.

Thanks
Thanks Galik again. Ive created a method that will generate all the factoriadic number upto my inputted value of n. I now want to change this factoriadic number into a permutation. I know the procedure i want to follow, but not sure how i can program it.
I want toiIterate through the factoradic array and create the permutation.
But i am struggling to creat the code and would appreciate the help!!
thanks again!
I'm not an expert on this. I am just going on what Wikipedia says.

From what I understand you need to iterate through your factoradic number array removing one of your values from your input number set each time to build up your permutation.

So it seems to me you could use a std::vector for your factoradic number and another vector containing your input number set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>

std::vector<int> fact_to_perm(const std::vector<int>& fact)
{
	std::vector<int> perm; // our return permutation

	// Initialise our list of all possible values.
	std::vector<int> list;
	for(size_t i = 0; i < fact.size(); ++i)
	{
		list.push_back(i);
	}

	// Then you need to do these things:

	// iterate through the factoradic number vector fact

		// get current factoradic value
		// use that to select a permutation value from the list
		// remove that permutation value from the list
		// add it to the permutation
	}

	return perm;
}

Topic archived. No new replies allowed.