Share your knowledge at the LQ Wiki.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Switching a 1D array to at 2D array, any advice?
 User Name Remember Me? Password
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 10-22-2012, 08:18 AM #1 carlosk711 Member   Registered: Sep 2012 Posts: 52 Rep: Switching a 1D array to at 2D array, any advice? (I originally posted in the wrong forum, sorry) I want it to be a 4x4 array the reads out 1-4 at random with repeating EX) 4312 3124 1243 2431 My current program reads out a 1-10 at random without repeating Can someone help me modify it? Code: ```#include #include #include using namespace std; int main () { int A[10]; srand (time(NULL) ); 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; return 0; }```
 10-22-2012, 09:01 AM #2 millgates Member Contributing Member   Registered: Feb 2009 Location: 192.168.x.x Distribution: Slackware Posts: 651 Rep: 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) Last edited by millgates; 10-22-2012 at 09:20 AM.
 10-22-2012, 09:43 AM #3 carlosk711 Member   Registered: Sep 2012 Posts: 52 Original Poster Rep: I need to switch it to a 4x4 array regardless, how would you suggest I do it? Write a completely new program?
 10-22-2012, 10:13 AM #4 millgates Member Contributing Member   Registered: Feb 2009 Location: 192.168.x.x Distribution: Slackware Posts: 651 Rep: 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 }``` Last edited by millgates; 10-22-2012 at 10:16 AM.
 10-22-2012, 11:13 AM #5 carlosk711 Member   Registered: Sep 2012 Posts: 52 Original Poster Rep: It's not homework, I just want to practice 2-D arrays, and by getting examples it will help me
 10-22-2012, 11:20 AM #6 carlosk711 Member   Registered: Sep 2012 Posts: 52 Original Poster Rep: So how should my final program look? I tried to change mine but I'm not following EDIT: And yes, I created the original, so I understand it Last edited by carlosk711; 10-22-2012 at 11:22 AM. Reason: Under EDIT
10-22-2012, 11:44 AM   #7
millgates
Member

Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep:
Quote:
 Originally Posted by carlosk711 I tried to change mine but I'm not following
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.

Last edited by millgates; 10-22-2012 at 11:47 AM.

10-22-2012, 11:47 AM   #8
danielbmartin
Senior Member

Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Rep:
Quote:
 Originally Posted by carlosk711 My current program reads out a 1-10 at random without repeating Can someone help me modify it?
If you have the desired string of 16 numbers,
you may reshape it into a 4x4 with sed ...
Code:
```echo "4312312412432431"  \
|sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{4\}\)/\1\n\2/;ta'```
Daniel B. Martin

10-22-2012, 12:51 PM   #9
millgates
Member

Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep:
Quote:
 Originally Posted by danielbmartin If you have the desired string of 16 numbers, you may reshape it into a 4x4 with sed ... Code: ```echo "4312312412432431" \ |sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{4\}\)/\1\n\2/;ta'``` Daniel B. Martin
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?

Last edited by millgates; 10-22-2012 at 01:00 PM.

1 members found this post helpful.
 10-22-2012, 01:45 PM #10 Elv13 Member   Registered: Apr 2006 Location: Montreal,Quebec Distribution: Gentoo Posts: 825 Rep: From the other thread: 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.
 10-22-2012, 02:10 PM #11 danielbmartin Senior Member   Registered: Apr 2010 Location: Apex, NC, USA Distribution: Ubuntu Posts: 1,167 Rep: [QUOTE=millgates;4812310]... how about just Code: `sed 's/[0-9]\{4\}/&\n/g;s/\n\$//'` Excellent! Your sed is better because it has no loop. Daniel B. Martin
 10-22-2012, 02:12 PM #12 carlosk711 Member   Registered: Sep 2012 Posts: 52 Original Poster Rep: 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.
 10-22-2012, 04:09 PM #13 carlosk711 Member   Registered: Sep 2012 Posts: 52 Original Poster Rep: NEW PROBLEM: I've written it so the rows work Now I need each column to read 1-4 without repeating as well Heres the code where only the rows work: Code: ```#include #include #include using namespace std; int main () { int A[4][4]; srand (time(NULL) ); int j = 0; while (j < 4) { int count = 0; while (count <4) { int k = (rand() % 4) + 1; bool Placed = false; for (int i = 0; (i < count); i++) { if (A[j][i] == k) { Placed = true; } } if (!Placed) { A[j][count] = k; count ++; } } for (int i=0; i<4; i++) { cout << A[j][i] << " "; } cout << endl; j++; } return 0; }```
 10-22-2012, 04:30 PM #14 millgates Member Contributing Member   Registered: Feb 2009 Location: 192.168.x.x Distribution: Slackware Posts: 651 Rep: 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.

 Tags array, c++, program

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post laughing_man77 Linux - Hardware 4 03-23-2012 05:05 PM kalleanka Programming 1 07-27-2010 07:50 AM richman1234 Linux - Newbie 6 07-26-2010 12:19 AM beeblequix Linux - General 2 11-20-2009 11:07 AM

All times are GMT -5. The time now is 12:22 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -