Question

In: Computer Science

Someone once said, "if you gave everyone in the world a million dollars and if you...

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.

  1. Declare an N element integer array where N equals 10. Name the array assets.
  2. Assign M to each element of the array assets where M equals 100.
  3. Assign N to nActive, the number of array elements greater than 0.

Repeat the following actions until nActive equals 1:

  1. Generate 2 distinct random numbers, idx1 and idx2, between 0 and nActive –
  2. Generate 1 random number, facevalue, between 0 and 1.
  3. if facevalue equals 0 then transfer 1 from the smaller of the two array elements at idx1 and idx2 to the larger of the two array elements at idx1 and idx2. (transfer means subtract 1 from one array element and add 1 to the other array element)
  4. if facevalue equals 1 then transfer 1 from the larger of the two array elements at idx1 and idx2 to the smaller of the two array elements at idx1 and idx2.
  5. After transfering 1 between the two array elements at idx1 and idx2, one of the array elements at idx1 and idx2 may equal 0. When this occurs subtract 1 from nActive, the number of non-zero array elements and make sure that the program does not select this zero-value array element again. The easiest way to prevent the program's selecting the zero-value array element is to exchange the zero-value array elemement at idx1 or idx2 with the last non-zero array element. When the program starts the last non-zero array element is at nActive – 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] << ' ';

}







Solutions

Expert Solution

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;

}


Related Solutions

Indira Gandhi, the late Indian prime minister once said: 'The idea of a better world is...
Indira Gandhi, the late Indian prime minister once said: 'The idea of a better world is one in which medical discoveries will be free of patents and there will be no profiteering from life and death.'. Patents are property rights over intellectual discoveries. What do you think of Gandhi’s statement above? in 600 words
It has been said that if you are at a party and you tell someone you...
It has been said that if you are at a party and you tell someone you have a terrible disease or you are a terrorist, the conversation happily continues. But, if you tell them you are religious, an icy chill hits the air. Peter Kreeft believes that much of this is due to ignorance in the area of religious studies. His personal pet peeve is the statement: “All religions are the same deep down.” He states: “That is simply factually...
If someone gave you $10,000, how long would it take you to turn that $10,000 into...
If someone gave you $10,000, how long would it take you to turn that $10,000 into $1,000,000? What is your plan? Also, make sure that somewhere within your response to include the use of Future Value of Money formula as shown within the Chapter. A cash flow diagram would be helpful as well. For some reason i could only think of Roth IRAs or Mutual Funds is there other ways excluding stock market that have good return but not as...
An admitted call girl once said that prostitution is a profitable business if you market it...
An admitted call girl once said that prostitution is a profitable business if you market it correctly and properly screen your clients. The U.S. Department of Commerce claims that the underground economy (especially illegal gambling and prostitution) represents between 3 and 40 percent beyond the legal economy. Fifi defends the right to sell sex between consenting adults and argues that a person has the right to use his or her body in any way that maximizes income. “We allow people...
Think about a time when someone gave you good advice that you did not take. For...
Think about a time when someone gave you good advice that you did not take. For this discussion post, provide the situation and explain why the “good” advice was not taken. Next, discuss whether or not it makes sense to ignore “good” advice. When is it appropriate to do so? When is it not? From a writing perspective, think about how you can separate someone’s good intentions from good advice in writing. Explain what this process looks like. Finally, determine...
If someone gave you $1,000 what stock investing strategy would you pursue in order to maximize...
If someone gave you $1,000 what stock investing strategy would you pursue in order to maximize your return on investment?
If you said someone had a velocity of -12mph and they were traveling north? Wouldn't it...
If you said someone had a velocity of -12mph and they were traveling north? Wouldn't it mean that they were traveling 12mph south? This is a quote from here: if something [object-x] moving to the right was taken to have positive momentum, then one should consider something [object-y] moving to the left to have negative momentum. But isn't Momentum a vector, so the direction should be specified separately to the number. What I mean is, object-y with -1200kg m/s and...
Mary Wells Lawrence once said, “In this business, you can never wash the dinner dishes and...
Mary Wells Lawrence once said, “In this business, you can never wash the dinner dishes and say they are done. You have to keep doing them constantly.” If you were a business executive or information technology manager, discuss how this statement relates to information technology risks, security, and disaster recovery.
Mr. Y said, “Everyone should buy medical insurance for the whole family.
Mr. Y said, “Everyone should buy medical insurance for the whole family. Especially for new born babies, you will only need to pay premium for the first 18 years, after that, you don’t have to pay anymore, and the protection will continue for the rest of your children’s lives. So, these plans provide good protection for your children. At the same time, these plans are cheap and they save you a lot of money.”Comment on Mr. Y statement.                                                                                                                                         
1. If you have current ratio of 1.9, current liabilities of 6 million dollars, and long term assets of twelve million dollars
 1. If you have current ratio of 1.9, current liabilities of 6 million dollars, and long term assets of twelve million dollars, and equity of 8.2 million dollars what is your long term debt? 2. Using information from question 1., what is the debt to equity ratio? 3.What is the goal of financial management?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT