LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
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

Reply
 
Search this Thread
Old 10-22-2012, 07:18 AM   #1
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Rep: Reputation: Disabled
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;
}
 
Old 10-22-2012, 08:01 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
Old 10-22-2012, 08:43 AM   #3
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
I need to switch it to a 4x4 array regardless, how would you suggest I do it? Write a completely new program?
 
Old 10-22-2012, 09:13 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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 09:16 AM.
 
Old 10-22-2012, 10:13 AM   #5
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
It's not homework, I just want to practice 2-D arrays, and by getting examples it will help me
 
Old 10-22-2012, 10:20 AM   #6
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-22-2012, 10:44 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by carlosk711 View Post
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.
 
Old 10-22-2012, 10:47 AM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,083

Rep: Reputation: 287Reputation: 287Reputation: 287
Quote:
Originally Posted by carlosk711 View Post
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
 
Old 10-22-2012, 11:51 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by danielbmartin View Post
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.
Old 10-22-2012, 12:45 PM   #10
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
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.
 
Old 10-22-2012, 01:10 PM   #11
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,083

Rep: Reputation: 287Reputation: 287Reputation: 287
[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
 
Old 10-22-2012, 01:12 PM   #12
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-22-2012, 03:09 PM   #13
carlosk711
Member
 
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
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;
}
 
Old 10-22-2012, 03:30 PM   #14
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
  


Reply

Tags
array, c++, program


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] mdadm: only give one device per ARRAY line: /dev/md/:raid and array laughing_man77 Linux - Hardware 4 03-23-2012 04:05 PM
Server array (mysql, php) -> gboolean array in c client kalleanka Programming 1 07-27-2010 06:50 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM


All times are GMT -5. The time now is 01:52 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration