Question

In: Computer Science

You can generate a random number in C using the following code: int myRandomNumber; srand(time(NULL)); //...

You can generate a random number in C using the following code: int myRandomNumber; srand(time(NULL)); // seed the random number generator myRandomNumber = rand(); // each time you call this function there will be a different random number. Generate a random number, and output it. Use if statements to determine if the number is odd or even, and output a message to that effect. Similarly, output if the number is divisible by 3, and if it is divisible by 10. Use the % operator to achieve this. Run the program, and print the results several times using different numbers to be sure that it’s working. Hint: In C you can not declare variables after you write executable code. For example if you call the srand function, and then declare some more variables after that call, the program will not compile. The compiler messages are not very helpful. Put all the variable declarations at the top of the function.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>

int main()
{
int myRandomNumber;
srand(time(NULL));
// seed the random number generator
myRandomNumber = rand();
if(myRandomNumber % 2 == 0){
printf("Random number %d is even number\n",myRandomNumber);
}
else{
printf("Random number %d is odd number\n",myRandomNumber);
}
  
  
if(myRandomNumber % 3 == 0){
printf("Random number %d is divisible by 3\n",myRandomNumber);
}
else{
printf("Random number %d is not divisible by 3\n",myRandomNumber);
}
  
if(myRandomNumber % 10 == 0){
printf("Random number %d is divisible by 10\n",myRandomNumber);
}
else{
printf("Random number %d is not divisible by 10\n",myRandomNumber);
}
return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                                  

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 992903991 is odd number                                                                                                                                                                                                                                    

Random number 992903991 is divisible by 3                                                                                                                                                                                                                                

Random number 992903991 is not divisible by 10                                                                                                                                                                                                                           

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 692479352 is even number                                                                                                                                                                                                                                   

Random number 692479352 is not divisible by 3                                                                                                                                                                                                                            

Random number 692479352 is not divisible by 10                                                                                                                                                                                                                           

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1459191701 is odd number                                                                                                                                                                                                                                   

Random number 1459191701 is not divisible by 3                                                                                                                                                                                                                           

Random number 1459191701 is not divisible by 10                                                                                                                                                                                                                          

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1165304989 is odd number                                                                                                                                                                                                                                   

Random number 1165304989 is not divisible by 3                                                                                                                                                                                                                           

Random number 1165304989 is not divisible by 10                                                                                                                                                                                                                          

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1165304989 is odd number                                                                                                                                                                                                                                   

Random number 1165304989 is not divisible by 3                                                                                                                                                                                                                           

Random number 1165304989 is not divisible by 10                                                                                                                                                                                                                          

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 850567710 is even number                                                                                                                                                                                                                                   

Random number 850567710 is divisible by 3                                                                                                                                                                                                                                

Random number 850567710 is divisible by 10                                                                                                                                                                                                                               

sh-4.2$ main                                                                                                                                                                                                                                                             

Random number 1624618623 is odd number                                                                                                                                                                                                                                   

Random number 1624618623 is divisible by 3                                                                                                                                                                                                                               

Random number 1624618623 is not divisible by 10


Related Solutions

<< Using R code >> Set seed number as "12345" every time you generate random numbers....
<< Using R code >> Set seed number as "12345" every time you generate random numbers. For each answer, use # to explain if necessary. 2) Generate a data.frame "D" with 3 variables. The 1st variable "v1" has 50 number of N(5,3^2) (normal with mean 5, standard deviation 3) The 2nd variable "v2" has 50 number of exp(5) (exponential with parameter 5) The 3rd variable "v3" has 50 random characters from lower case alphabets. 2-1) Rename the variable from "v1",...
<<Using R code>> Set seed nuumber as 12345" every time you generate random numbers. For each...
<<Using R code>> Set seed nuumber as 12345" every time you generate random numbers. For each anser, use # to explain if necessary. 3. Use data "thusen" in ibrary ISwR" 3-1) Remove missing observations in the data, name this set as thu1, and print the first 6 and last 6 observations. 3-2) Rename a variable "short.velocity" -> "x", "blood.glucose" -> "y". 3-3) Draw a scatter plot for "y" by "x", give title "velocity vs.glucose". Put tick marks of x-axis at...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
In many programming languages you can generate a random number between 1 and a limiting value...
In many programming languages you can generate a random number between 1 and a limiting value named LIMIT by using a statement similar to randomNumber = random(LIMIT). Create the logic for a guessing game in which the application generates a random number and the player tries to guess it. Display a message indicating whether the player’s guess was correct, too high, or too low. (After you finish Chapter 4, you will be able to modify the application so that the...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
Using pseudocode design a number guessing game program. The program should generate a random number and...
Using pseudocode design a number guessing game program. The program should generate a random number and then ask the user to guess the number. Each time the user enters his or her guess, the program should indicate it was too high or too low. The game is over when the user correctly guesses the number. When the game ends, the program should display the number of guesses that the user made.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT