Login
Order Now
Support
Data Structure Assignment Help

Data Structure Assignment Help

  • 14th Apr, 2022
  • 16:03 PM
#include
#include
#include

int LOW ;
int HIGH ;
// this is complete; don't change it
int gen( int low, int high )
{
    int x;
    x = rand() % (high - low +  1) + low;
    return x;
}

// return rightmost digit
int pos(int x)
{
    int last;
    last = x % 10;
    return last;
}

// return -1
int neg(int x)
{
    if(x < 0){
        return -1;
    }
}

// return 1
int zer(int x)
{
    if(x == 0){
        return 1;
    }
}

/*
    For the next 4 functions
    use gen(LOW,HIGH) to produce the numbers
    Assume LOW & HIGH are set outside
*/

// seed the random number generator
// report how many number produced until value t

int one( int seed, int t )
{
    if((t>=LOW)||(t<=HIGH))
    {

        int count = 1;
        srand(seed);
        while(1)
        {
            int k = gen(LOW, HIGH);
            if(k == t){
                return count;
            }
            else
            {
                count++;
            }
        }
    }

}
// seed the random number generator
// report how many number produced until value t
//    appears twice in a row
int temp=0;
int pair( int seed, int t )
{

    int k ;
    int count = 1;
    srand(seed);
    while (1)
    {
       k=gen(LOW,HIGH);
       if(temp == t && k == t){
           return count;
       }
       else
        {
            temp=k;
            count++;
        }
      }
}
// seed the random number generator
// report how many number produced until the
//    same value appears twice in a row
int temp2=0;
int dups( int seed )
{

    int k ;
    int count = 1;
    srand(seed);
    while(1)
    {
       k=gen(LOW,HIGH);
       if(temp2 ==  k ){
           return count;
       }
       else
       {
         temp2=k;
         count++;
       }
    }

}

// Generate numbers until 3 negative values are produced
// If not possible send back 0 times and 0 sum

void until3neg(int seed, int *times, int *sum)
{
    // Note:: for sum do not add the actual values.
    // call the correct pos, zer, neg function for each
    //    generated value and use the value returned to
    //    accumulate the sum
    //  * for instance if random number is 0, call the zer function
    //       and add that value to the sum
    srand(seed);
    int x;
    int posx, negx, zerx;
    int n=0;
    if(LOW>=0)
    {
        *times=0;
        *sum=0;
    }
    else
    {
        while(1)
        {
            x=gen(LOW,HIGH);
            if (x<0)
               n+=1;
            if(x > 0)
            {
                posx = pos(x);
                *sum += posx ;
                *times+=1;
            }
            if(x < 0)
            {
                negx = neg(x);
                *sum +=  negx;
                *times+=1;
            }
            if(x == 0)
            {
                zerx = zer(x);
                *sum +=  zerx;
                *times+=1;
            }
            if (n==3)
                break;
        }
    }
}
int main()
{
    int k,x,t;
   // srand(2);
    LOW = 1;
    HIGH = 6;

    k = one(41, 2);

    printf("%d times to get a 2\n", k);
    k = pair(41, 2);
    printf("%d times to get a 2 2\n", k);
    k = dups(41);
    printf("%d times to get same number twice\n", k);

    LOW = -15;
    HIGH = 30;
    k=0;
    until3neg(26, &k, &t);
    printf("To get 3 negatives: %d vals, sum %d\n",k,t);

    return 0;
}

Share this post

assignment helpassignment helperassignment expertsassignment writing services