Question

In: Computer Science

( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and...

( C language only )Generate a function  called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number;

the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15

. You must use bitwise arithmetic to calculate the modulus of the random numbers

Solutions

Expert Solution

Code:

#include<stdio.h>  // library file for standard input/output
#include<stdlib.h>
#include<stdbool.h> // very important to include if you want to pass value of neg
#include<time.h> // important for generating random numbers

int randomnum(int n,int m,bool neg){
    
    srand(time(0));  // used to maintain uniqueness of numbers generated
  
    int num = rand()%(m-n+1)+n;  // generating a random number within (n,m)
    if(num>15)return 0; // if its greater than 15, return nothing
    if(neg==true){  // if neg is true, return the negative of the number
        return -1*num;
    }else{
        return num;
    }
  
    return 0;
}
// driver code
int main(){

    // generate two random numbers
    int b = randomnum(5,10,false);
    int c = randomnum(1,5,true);

    int res = b & c; // apply bitwise & to calculate the modulus
    printf("%d",c); // print the result

}        

Output:

Note: Your question was a bit unclear on the bitwise part so if this what you were looking for, its all good or comment below if you need something to be changed.


Related Solutions

( C language only and all values should be float )Generate a function called randomnum(n,m,neg); where...
( C language only and all values should be float )Generate a function called randomnum(n,m,neg); where n and m are the lower and upper bounds for the random number; the generated number is negative if a variable named neg is true .These numbers should be non-zero with a maximum absolute value of 15 . You must use bitwise arithmetic to calculate the modulus of the random number
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
Question 1 a) Determine whether the language {a n b m c n | n >...
Question 1 a) Determine whether the language {a n b m c n | n > 0} is regular or not using pumping Lemma. b) Prove that the language {(ai bn | i, n > 0, i = n or i = 2n} is not regular using the Pumping Lemma.
C program only There is a documented prototype for a function called get_a_line in the .c...
C program only There is a documented prototype for a function called get_a_line in the .c file. Write a definition for get_a_line—the only function called from that definition should be fgetc. You can use the given main function to test your code. You should run the executable several times to check different cases: Type Ctrl-D in response to the prompt for input. get_a_line should fail to read any characters, put a ’\0’ character at the beginning of the array, and...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
Write and test a C implementation of the Mobius function M(n) defined as follows M(n) =...
Write and test a C implementation of the Mobius function M(n) defined as follows M(n) = 1 if n = 1           = 0 if any prime factor is contained in N more than once = (‐1)p if n is the product of p different prime factors Examples M(78) = ‐1     78 = 2 * 3 * 13                   M(34) = 1      34 = 2 * 17                M(45) = 0       45 = 3 * 3 * 5 The first values of M(n)...
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
3. Only using the runif function with default settings, generate: (a) n = 1e4 iid realizations...
3. Only using the runif function with default settings, generate: (a) n = 1e4 iid realizations from a Bernoulli distribution with probability of success parameter, θ = 0.7. (b) n = 1e4 iid realizations from a Binomial distribution with probability of success parameter, θ = 0.7 and number of trials= 20. (c) n = 1e4 iid realizations from an Exponential distribution with mean µ = 7 (hint: if X ~ Exp(µ) then Var(X) = µ2 ). For each case comment...
Prove that the language L={(M, N): M is a Turing machine and N is a DFA...
Prove that the language L={(M, N): M is a Turing machine and N is a DFA with L(M) =L(N)} is undecidable. You need to derive a reduction from Atm={(M, w)|Turing machine M accepts w} to L. (In layman's terms please, no other theorems involved)
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me know?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT