C++

How to generate a random array of integers subject to a certain constraint

I am writing a program that asks the user to enter an integer N and then generates a random array of integers of size 2^N, each of whose entries range from 1 to N+1. The final random array, however, must satisfy the following properties: the integer 1 can be stored in 1 slot, the integer 2 can only be stored in 1 slot, the integer 3 can only be stored in 2 slots, the integer 4 can only be stored in 4 slots... until all 2^N slots are filled with the integers 1, 2, ...,N+1.

Advertisement
I have written some code that asks the user to enter an integer N and then
generates a random array of integers of size 2^N but I cannot generate a random array subject to the constraints that I mentioned above. I have written a function, CheckArray, that returns the value of how many times a certain integer
appears in the array but I don't know how to use it to help me get my desired random array.

Below is the code I have written so far. Note that the function CheckArray is not called in the program since I don't know how to use it yet.
Advertisement

#include
#include
#include
#include
Advertisement
#include
using namespace std;

int RandomNumber(int m);
int exponentiation(int n);
int CheckArray(int IntegerArray[], int sizeOfArray, int CType);

int main()
{ int N;
int NUMOFCELLS;

cout cin>>N;
NUMOFCELLS = exponentiation(N);
cout
cout cout cout
int sample[128]; // this reserves 128 integer elements
int t;

srand(time(0));
getchar();

for(t = 0; t { sample[t] = RandomNumber(N); }

// display the array

for(t = 0; t { cout
cout
getchar();
return 0;
}

int RandomNumber(int m)
{ // generates a random number in the specified range from 0 to (m-1)
int iSecret;
iSecret = rand() % m + 1;
return(iSecret);
}

int exponentiation(int n)
{ int t;
int numberofcells;
numberofcells = 2;
for(t=1; t {numberofcells = 2*numberofcells;}

return(numberofcells);
}

int CheckArray(int IntegerArray[], int sizeOfArray, int CType)
{
int accumulator = 0;
int temp;

for(int i = 0; i { temp = IntegerArray[i];
if(temp == CType)
{accumulator++;}

}

return accumulator;
}
Advertisement

13 answers

Jamesseita ·

https://www.lamarinda.it/

PaulMic ·

Hi everyone! A friend shared something worth checking out and figured I'd post it for you. Check it out — check this out. Hope it helps someone http://spotlightcomicsforum.com/viewtopic.php?p=1179

DennisJidly ·

Lungs [url= https://www.red-gate.com/wp-content/uploads/2026/10/Comprar-Diazepam-online-sin-receta-Espana.pdf ] https://www.red-gate.com/wp-content/uploads/2026/10/Comprar-Diazepam-online-sin-receta-Espana.pdf [/url] Calorie counting https://vardin.fo/wp-content/uploads/2026/10/kob-tramadol-pa-nettet-uden-recept-danmark.pdf Alternative medicine

Olgagap ·

Hi everyone! I came across a helpful resource and figured I'd post it for you. Check it out — [url=https://www.starcraftworld.net/index.php?action=profile;u=47121]check this out[/url]. Hope it helps someone https://www.starcraftworld.net/index.php?action=profile;u=47121

ChrisLox ·

Greetings! I stumbled upon something worth checking out and figured I'd share it here. Have a look — [url=https://feitocurioso.com.br/ciencia/hidratacao-com-babosa-planta-milagrosa/#comment-16459]visit here[/url]. Let me know your thoughts https://feitocurioso.com.br/ciencia/hidratacao-com-babosa-planta-milagrosa/#comment-16459

OlgaTab ·

Hi everyone! I recently found something worth checking out and figured I'd pass it along. Have a look — [url=https://xn--289aq1wp4o.kr/elpisbbs/board.php?bo_table=review&wr_id=66507]read more[/url]. Would love your feedback https://xn--289aq1wp4o.kr/elpisbbs/board.php?bo_table=review&wr_id=66507

Ivantherm ·

Hi everyone! I came across something worth checking out and figured I'd share it here. Feel free to explore — [url=http://forumtest.uv.ro/viewtopic.php?f=2&t=336798]check this out[/url]. Let me know your thoughts http://forumtest.uv.ro/viewtopic.php?f=2&t=336798

ChrisDeate ·

Greetings! I came across a really useful site and figured I'd pass it along. Check it out — [url=http://www.jerry-miller.com/2012/02/29/book-monkey/comment-page-3906/]useful resource[/url]. Hope it helps someone http://www.jerry-miller.com/2012/02/29/book-monkey/comment-page-3906/

Garrettexhix ·

Full access to 276 TB music library, files are available every time - https://www.0daymusic.org
No ads - no waiting time - very fast download speed, ~400 daily 0DAY scene releases BEATPORT, traxsource,
fresh FLAC section , tracks unique section for the top downloaded albums 26 years music archives.
DJ producer applications full labels, music videos, albums files sorted section by date and style, livesets etc..
Account delivery time: 1 to 48 hours. https://scenedance.blogspot.com
Overal server1 MP3 speed: 1 Gb/s.
Overal server2 FLAC speed: 1 Gb/s.

Best regards
Garrett

JasonScold ·

Greetings! I recently found a really useful site and figured I'd pass it along. Check it out — [url=http://www.discusnature.net/forum/showthread.php?tid=63080]more info[/url]. Would love your feedback http://www.discusnature.net/forum/showthread.php?tid=63080

Paulphisy ·

Greetings! A friend shared something worth checking out and figured I'd share it here. Check it out — [url=http://xn--im4bm9e7th34f.com/bbs/board.php?bo_table=yp_estimate01&wr_id=2629]more info[/url]. Would love your feedback http://xn--im4bm9e7th34f.com/bbs/board.php?bo_table=yp_estimate01&wr_id=2629

Paulphisy ·

Greetings! A friend shared something worth checking out and figured I'd share it here. Have a look — [url=http://xn--im4bm9e7th34f.com/bbs/board.php?bo_table=yp_estimate01&wr_id=2629]check this out[/url]. Would love your feedback http://xn--im4bm9e7th34f.com/bbs/board.php?bo_table=yp_estimate01&wr_id=2629

JasonScold ·

Hey folks! A friend shared a handy page and figured I'd share it here. Check it out — [url=http://www.discusnature.net/forum/showthread.php?tid=63080]check this out[/url]. Curious what you all think http://www.discusnature.net/forum/showthread.php?tid=63080

Your answer

Login to answer

Trending today: see what people are reading