In: Computer Science
Someone once said, "if you gave everyone in the world a million dollars and if you waited long enough then 1 person would have all the money and everyone else would have nothing". You are going to write a program to test truthfullness of that statement.
Repeat the following actions until nActive equals 1:
using this code
#include "fixed1dintarray.h"
fixed1dintarray::fixed1dintarray(int newnelts)
{
if (newnelts < 2)
newnelts = 16;
size = newnelts;
nelts = 0;
elts = new int[size];
}
bool fixed1dintarray::isEmpty()
{
return nelts == 0;
}
bool fixed1dintarray::isFull()
{
return nelts == size;
}
int fixed1dintarray::getsize()
{
return size;
}
int fixed1dintarray::getnelts()
{
return nelts;
}
bool fixed1dintarray::getelt(int where, int& elt)
{
bool isgood = 0 <= where && where < nelts;
if (isgood)
elt = elts[where];
return isgood;
}
bool fixed1dintarray::member(int value)
{
return find(value) >= 0;
}
int fixed1dintarray::find(int value)
{
int pos = 0;
//while(!(pos < nelts || elts[pos] == value))
//until(pos == nelts || elts[pos] == value)
while (pos < nelts && elts[pos] != value)
pos++;
if (pos < nelts)
return pos;
else
return -1;
}
bool fixed1dintarray::insat(int where, int elt)
{
if (isFull())
return false;
if (where < 0 || nelts < where)
return false;
for (int pos = nelts - 1; pos >= where; pos--)
elts[pos + 1] = elts[pos];
elts[where] = elt;
nelts++;
return true;
}
bool fixed1dintarray::append(int elt)
{
return insat(nelts, elt);
}
bool fixed1dintarray::delat(int where)
{
if (where < 0 || nelts <= where)
return false;
for (int pos = where + 1; pos < nelts; pos++)
elts[pos - 1] = elts[pos];
return false;
}
bool fixed1dintarray::delelt(int elt)
{
int where = find(elt);
return delat(where);
}
void fixed1dintarray::show()
{
for (int i = 0; i < nelts; i++)
cout << elts[i] << ' ';
}
PROGRAM :
#include<iostream>
#include <cstdlib>
using namespace std;
int main()
{
int assets[10];
int nActive=10;
int idx1,idx2,facevalue,transfer=0,threshold=1000000000;
for(int i=0;i<nActive;i++)
assets[nActive]=100;
while(nActive>1)
{
idx1= (rand() % nActive);
idx2= (rand() % nActive);
if(idx1!=idx2)
{
facevalue= (rand() % 2);
if(facevalue==0)
{
if(idx1<idx2)
{
assets[idx1]--;
assets[idx2]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx1]==0)
{
assets[idx1]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
else
{
assets[idx2]--;
assets[idx1]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx2]==0)
{
assets[idx2]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
}
else
{
if(idx1>idx2)
{
assets[idx1]--;
assets[idx2]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx1]==0)
{
assets[idx1]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
else
{
assets[idx2]--;
assets[idx1]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx2]==0)
{
assets[idx2]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
}
}
}
if(transfer>threshold)
cout<<"Even after 1 Billion transfers,"<<
nActive<<"persons have non-zero dollars. So statement is not
correct"<<endl;
else
cout<<"The statement is correct"<<endl;
return 0;
}
The same program with class can be rewritten as follow.
#include<iostream>
#include <cstdlib>
using namespace std;
class Test
{
public:
static void check()
{
int assets[10];
int nActive=10;
int idx1,idx2,facevalue,transfer=0,threshold=1000000000;
for(int i=0;i<nActive;i++)
assets[nActive]=100;
while(nActive>1)
{
idx1= (rand() % nActive);
idx2= (rand() % nActive);
if(idx1!=idx2)
{
facevalue= (rand() % 2);
if(facevalue==0)
{
if(idx1<idx2)
{
assets[idx1]--;
assets[idx2]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx1]==0)
{
assets[idx1]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
else
{
assets[idx2]--;
assets[idx1]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx2]==0)
{
assets[idx2]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
}
else
{
if(idx1>idx2)
{
assets[idx1]--;
assets[idx2]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx1]==0)
{
assets[idx1]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
else
{
assets[idx2]--;
assets[idx1]++;
transfer++;
if(transfer>threshold)
break;
if(assets[idx2]==0)
{
assets[idx2]=assets[nActive-1];
assets[nActive-1]=0;
nActive--;
}
}
}
}
}
if(transfer>threshold)
cout<<"Even after 1 Billion transfers,"<<
nActive<<"persons have non-zero dollars. So statement is not
correct"<<endl;
else
cout<<"The statement is correct"<<endl;
}
};
int main()
{
Test::check();
return 0;
}