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.
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.
#include
#include
#include
#include
#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<< "Enter the maximum number of cell divisions you want: ";
cin>>N;
NUMOFCELLS = exponentiation(N);
cout<<"The maximum number of cells will be: ";
cout<
cout<<"n";
int sample[128]; // this reserves 128 integer elements
int t;
srand(time(0));
getchar();
for(t = 0; t <= NUMOFCELLS; t++)
{ sample[t] = RandomNumber(N); }
// display the array
for(t = 0; t < NUMOFCELLS; ++t)
{ cout << sample[t] << ' ' ; }
cout<<"n";
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 < n; ++t)
{numberofcells = 2*numberofcells;}
return(numberofcells);
}
int CheckArray(int IntegerArray[], int sizeOfArray, int CType)
{
int accumulator = 0;
int temp;
for(int i = 0; i < sizeOfArray; i++)
{ temp = IntegerArray[i];
if(temp == CType)
{accumulator++;}
}
return accumulator;
}
Syntax Help
I'm trying to make a form that edits a php file which contains config settings. I'm using strings that are defined by the user form inputs. This is what my file should look like when the form is
First root of a number
Hi, I can't find any function in php to give me the first root of a number.Is there any built-in function for it? For example: 81 => 39 => 364 => 2256 => 2I wrote this
Please help with code for SMTP authenticated PHP email form
Hello, I'm creating a PHP email form, and for this particular server, I have to use SMTP Authentication. They hosting service sent me this link: http://email.about.com/od/emailprogramm ...
How to get exact 3 moths post date from today date using javascript
How to get exact 3 moths post date from today date using javascriptPlease help
Why is the logic of this simple code not working?
Hey, I'm trying to determine if a table already exists in mysql, but doing a query first with mysql_query, then checking $sql as if it a were TRUE of FALSE. Which I though it would be. My Code
Read from forum
Haven't written anything forever and I would like to get back.What I'm trying to do is getting the data from all the threads in a sub forum.Can someone roughly point out which functions I'll need for
Code error with Index.php
Error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/runevid/public_html/index.php on line 25<html><head><title>CrazyQuest -
Shuffle Array
Hi,I am writing a script for a game that needs players to randomly be assigned a target (another player) in a loop so that each player is matched with someone other than who has them. See below:Andy
Anti Spam Code Problems
Ok where do i start? Probably by telling you I have very little working knowledge of PHP and that I have been working on this problem for 3 days and have had no luck!Here we go:I have a rate and
mr8m - reverse document
Friends,