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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-22-2012, 07:18 AM
|
#1
|
|
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 <iostream>
#include <cstdlib>
#include <time.h>
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, 08:01 AM
|
#2
|
|
Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 621
|
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 08:20 AM.
|
|
|
|
10-22-2012, 08:43 AM
|
#3
|
|
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, 09:13 AM
|
#4
|
|
Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 621
|
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:
Alternatively, you may create a one dimensional array of size 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 09:16 AM.
|
|
|
|
10-22-2012, 10:13 AM
|
#5
|
|
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, 10:20 AM
|
#6
|
|
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 10:22 AM.
Reason: Under EDIT
|
|
|
|
10-22-2012, 10:44 AM
|
#7
|
|
Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 621
|
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 10:47 AM.
|
|
|
|
10-22-2012, 10:47 AM
|
#8
|
|
Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 756
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, 11:51 AM
|
#9
|
|
Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 621
|
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 12:00 PM.
|
|
|
1 members found this post helpful.
|
10-22-2012, 12:45 PM
|
#10
|
|
Member
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 822
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, 01:10 PM
|
#11
|
|
Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 756
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, 01:12 PM
|
#12
|
|
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 01:15 PM.
|
|
|
|
10-22-2012, 03:09 PM
|
#13
|
|
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 <iostream>
#include <cstdlib>
#include <time.h>
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, 03:30 PM
|
#14
|
|
Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 621
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:20 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|