Question

In: Computer Science

Part 2 Write a C code to create 5 random numbers between 90 and 100 (included)...

Part 2

Write a C code to create 5 random numbers between 90 and 100 (included) and print the values on the screen.

Part 3

Create a function that will mimic throwing the dice twice. The function will do the following when you call it.

  • The function will generate 2 random numbers between 1 and 6
  • The function will display the two random numbers to the user
  • The function will display “Winner” if the sum of the two numbers are 7 or 11

The function will display “hard luck” otherwise

Solutions

Expert Solution

part2:

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

int main()
{
    int upper=100,lower=90; //Taking upper limit as 100 and lower limit as 90.
    int i,num;
    printf("Five random number between 90 and 100:\n\n");
    srand(time(0));//by this we generate new batch of random numbers every time we run the programm.
    for(i=0;i<5;i++)
    {
        //use rand function to generate random numbers
        num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
        printf("%d\t",num);//show output on screen
    }
    return 0;
}

part 3:

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void throwdice()
{
    int upper=6,lower=1; //Taking upper limit as 6 and lower limit as 1.
    int firstnumber,secondnumber;
    int num,i;
    //use rand function to generate random numbers
    for(i=0;i<2;i++)
    {
        //use rand function to generate random numbers
        num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
        if(i==0)
        {
            firstnumber=num;//store first random number when i==0
        }
        if(i==1)
        {
            secondnumber=num;//store second random number when i==1
        }
    }
    printf("Two random number are: %d  %d\n",firstnumber,secondnumber);//show two random numbers
    if(firstnumber+secondnumber==7 || firstnumber+secondnumber==11)//checking condition as given in question
    {
        printf("\nWinner\n");
    }
    else
    {
        printf("\nHard luck\n");
    }
}
int main()
{
    srand(time(0));//by this we can generate new random numbers every time when we run the program.
    throwdice(); //calling function
    return 0;
}

I also merge two program if u required:

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void throwdice()
{
    int upper=6,lower=1; //Taking upper limit as 6 and lower limit as 1.
    int firstnumber,secondnumber;
    int num,i;
    //use rand function to generate random numbers
    for(i=0;i<2;i++)
    {
        //use rand function to generate random numbers
        num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
        if(i==0)
        {
            firstnumber=num;//store first random number when i==0
        }
        if(i==1)
        {
            secondnumber=num;//store second random number when i==1
        }
    }
    printf("Two random number are: %d  %d\n",firstnumber,secondnumber);//show two random numbers
    if(firstnumber+secondnumber==7 || firstnumber+secondnumber==11)//checking condition as given in question
    {
        printf("\nWinner\n");
    }
    else
    {
        printf("\nHard luck\n");
    }
}
int main()
{
    int upper=100,lower=90; //Taking upper limit as 100 and lower limit as 90.
    int i,num;
    printf("Five random number between 90 and 100:\n\n");
    srand(time(0));
    for(i=0;i<5;i++)
    {
        //use rand function to generate random numbers
        num=(rand()%(upper-lower+1))+lower; //(rand()%(upper-lower+1))+lower formula for generate random number between range.
        printf("%d\t",num);//show output on screen
    }
    printf("\n\n");
    throwdice();
    return 0;
}

plss like if u like my answer.


Related Solutions

Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the...
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the while loop. question2: Write a C++ program that prints all numbers between 10 to 1000 that are divisible by 5
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them...
****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them to a text file on separate lines. Then the program should read the numbers from the file, calculate the average of the numbers, and display this to the screen.
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random numbers from randomData.txt and store them in an array Print the data in the array Find the smallest and the largest of the random numbers and their array position Insert an element of value100 in the 51th position of the array Delete all the elements of the array having values between 50-80 and print the residual array Sort the data in the final array(residual)...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
2)  Suppose I use a random numbers table to randomly choose 900 numbers between 0 and 100....
2)  Suppose I use a random numbers table to randomly choose 900 numbers between 0 and 100. In this particular sample, the mean of the 900 numbers is 50 and the standard deviation is 30. a)Approximately, what proportion of the 900 numbers should fall between 20 and 80 (inclusive)? b)Suppose each student in Static (class size = 140) repeats the experiment described above and computes the mean of his or her 900 numbers. Based on the information given above, estimate the...
write C program to create 4 threads for summing the numbers between 1 and 40 where...
write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following. The sum from 1 to 10 = 55 The sum from 11 to 20 =...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array...
1.) Generate an array of 10 random numbers between 1 - 100 2.) Copy the array to a temp array 3.) Call each of the methods to sort (bubble, selection, insertion, quick, merge), passing it the array 4.) In-between the calls, you are going to refresh the array to the original numbers. 5.) Inside of each sorting method, you are going to obtain the nanoseconds time, before and after the method Subtract the before time from the after time to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT