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...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
IN JAVA Create a program that asks the user to input the day, high and low...
IN JAVA Create a program that asks the user to input the day, high and low temperature. Display the day, high, and low. Use a While Loop to enter all seven days of the week and high temperatures. Inside the While Loop, the program will count, total, and average the highs. The following will be displayed:                         Day: (variable)                         High: (variable)                         Count High: (variable)                         Total High: (variable)                         Average High: (variable) After the seven days and highs...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT