Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-21-2012, 08:48 PM   #1
Registered: Sep 2012
Posts: 52

Rep: Reputation: Disabled
How can I modify this program to change it to a 2D array??

I want it to be a 4x4 array the reads out 1-4 at random with repeating


My current program reads out a 1-10 at random without repeating
Can someone help me modify it?
#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-21-2012, 10:49 PM   #2
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 129Reputation: 129
[Wrong forum]

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, 07:19 AM   #3
Registered: Sep 2012
Posts: 52

Original Poster
Rep: Reputation: Disabled
My apologies, I have reposted in another forum


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] Cannot Modify Folder Created By Program And Not By Me hitmen Linux - Newbie 13 12-19-2011 11:09 PM
Modify script loading array with ranges of lines in awk cgcamal Programming 7 03-18-2011 11:14 PM
Difference between Access Modify Change ? hq4ever Linux - General 5 01-17-2010 02:42 AM
program to modify iso image shanenin Linux - Software 6 08-26-2006 06:15 AM
how to modify codes in a program? timberwolf Linux - Software 1 12-08-2003 07:21 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:36 PM.

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