ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

So what's the problem? Why don't you just create another loop to run the algorithm as many times as you need? BTW, the algorithm you use to generate permutation is not very efficient. It would probably be better to generate a sorted array first and than shuffle its elements randomly.

Edit: Oh, and don't double post. If you create a thread in the wrong forum, the way to go is to kindly ask a moderator to move it (You can do it using the "Report" button below the post)

Is this a homework? Do you understand how the original program works? The change required to make it do what you want is quite simple.
I am not sure what exactly is the problem for you.
You can declare a 4x4 array like this:

Code:

int A[4][4];

Alternatively, you may create a one dimensional array of size 16

Code:

int A[16];

where Aij = A[4 * i + j]

You can then create a loop:

Code:

for (unsigned j = 0; j < numberOfRows; j++) {
// do whatever you do in the first program, just with A[j][whatever] instead of A[whatever]
// and the loops will go from 0 to 3 instead of 0 - 9
}

What exactly do you not understand? What your program does is to create a random permutation of numbers in the 1D array A. Now what you want to do is create a 2D 4x4 array (so that's an array of four 1D arrays). So you you iterate over a new index, say j from 0 to 3 and for each such j you do with A[j][i] exactly what you did with A[i] in your first program. So all you have to do is put this part of your program:

Code:

int count = 0;
while (count <10){
int k = (rand() % 10) + 1;
bool Placed = false;
for (int i = 0; (i < count); i++){
if (A[i] == k){
Placed = true;
}
}
if (!Placed){
A[count] = k;
count ++;
}
}
for (int i=0; i<10; i++)
{
cout << A[i] << " ";
}
cout << endl;

inside a loop over j
and add [j] to every occurence of A in the program. If you get stuck, just show us what you've got so far.

I believe the original poster's intention is to practice using 2D arrays in C++, not formatting a string. But I may be wrong...
Anyway, that's a very nice sed there

BTW, how about just

Code:

sed 's/[0-9]\{4\}/&\n/g;s/\n$//'

carlosk711: Ehm, now when I think about it, do you want to just print four random permutations of {1..4} or do you want a matrix with elements unique in both rows and columns?

As this is C++, using static array is not necessary, use Vectors or Lists so you have a the find() function.

As for the 2D static array, a neat trick is to keep your existing code intact (but use 14 as size instead of 10). Then make a new array of size 4 and type int*. Place the newArr[0] = A, newArr[1] = newArr[1] = A+4; and so on. With that, you can have both a continuous and a 2D view of the same dataset. Again, it is a little trick, not something that would work in a pure algorithm point of view. It work because of the way C does static array pointer.

That's interesting, I'll have to try it out! But I doubt my professor would accept it on an exam. Right now I'm just practicing using 2-D arrays.

AND to respond earlier a question on a practice exam says to create a 2-D array that will take in numbers 1-4 at random and read out each row, without repetition.

for example:

2134
1243
4312
3124

EDIT: Yes it's for C++ using basic programming

Last edited by carlosk711; 10-22-2012 at 02:15 PM.

Ok. So what I would do is to initialize the array with a trivial set of numbers:

Code:

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

This is a state where no number is repeated in any row or column. It is obvious that if I swap any two rows, the resulting state will also be free of repetitions. Analogically, swapping any two columns will result in a non-repetitive permutation. So, I would randomly shuffle rows and then columns (not necessarily in that particular order). The result would then be a random matrix of numbers without repetition.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.