Question

In: Computer Science

1. Create a program that generates n values in range [low, high]. • Create a function...

1. Create a program that generates n values in range [low, high].

• Create a function generate_vals which takes as input an array of double values and three integers representing the size of the array, low value of range, and high value of range, respectively. The function should use rand() to generate the requested number of values.

• Prompt the user to enter the size of their array as shown below. It cannot exceed a maximum size of 128. If it does, set the size equal to 128 and continue with the program.

• Prompt the user to enter a low and high value as shown below. These values will be used to limit the range that each generated number can be.

• Print the generated values such that it matches the format below.

Example Run:

Enter size: 8

Enter low value: 9

Enter high value: 18

16.05, 16.19, 17.20, 10.78, 12.02, 15.91, 11.50, 13.99

Solutions

Expert Solution

SOURCE CODE

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void generate_vals(double arr[], int size, int low, int high);

int main()

{

    // seed the random number generator

    srand(time(0));

    int size, low, high;

    double arr[128];

    

    // taking input

    printf("Enter size: ");

    scanf("%d", &size);

    printf("Enter low value: ");

    scanf("%d", &low);

    printf("Enter high value: ");

    scanf("%d", &high);

    // calling function

    generate_vals(arr, size, low, high);

    // printing values

    for (int i = 0; i < size; i++)

    {

        printf("%.2f ", arr[i]);

    }

    

    return 0;

}

// function to generate random float values in a given range

void generate_vals(double arr[], int size, int low, int high)

{

    // in order to generate a random float of 2 decimal places

    // we shall first generate random int numbers between range

    // low*100 and high*100. Dividing the result by 100 shall

    // give us a decimal random number of 2 decimal places

    int l_range = low*100, h_range = high*100;

    for (int i = 0; i < size; i++)

    {

        // genrating random int numbers between range

        // low*100 and high*100

        arr[i] = rand() % (h_range - l_range + 1) + l_range;

        // dividing by 100 to get result

        arr[i] /= 100.0;

    }

}

SOURCE CODE SCREENSHOT

OUTPUT


Related Solutions

Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
create a python function (only) that generates 10 random numbers within a specific range. Every time...
create a python function (only) that generates 10 random numbers within a specific range. Every time a number is generated, it is appended to a list (you need to initialize a list). Then the function counts how many times a number is repeated in the created list (you can choose any number to count). The function returns the list, the number you have chosen and its count. --define the function here ---
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
pyramid.py program for i in range(1,6): for j in range(i): print("*",end=' ') print("\n",end='') Exercise: Create the...
pyramid.py program for i in range(1,6): for j in range(i): print("*",end=' ') print("\n",end='') Exercise: Create the pyramid similar to the above pyramid.py program using a while loop. Name your program whilepyramid.py and submit it on blackboard.
Write a C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
1.The range from high to low tide in a particular bay is found to be d...
1.The range from high to low tide in a particular bay is found to be d meters. The variation in height of the water acts as simple harmonic motion with a period of 12.5 hours. How long does it take for the tide to subside (go down) a distance of 0.35d from its maximum level? Your answer should be in hours, but enter only the numerical part in the box. 2. A simple harmonic oscillator is made up of a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT