6.5 Exercises

  1. Using the array arr with value a[0] = 9, a[1] = 2, a[2] = 5, a[3] = 4, a[4] = 3, determine the output of the code in Example 6-11.

    Example 6-11. Code for Exercise 1
        1 i = 0
        2 
        3 while (i < a.size)
        4 	puts a[i]
        5 	i = i + 1
        6 end
    
  2. The code in Example 6-12 looks for the first two elements that are out of order and swaps them; however, it is not producing the correct results. Fix the code so that it works correctly.

    Example 6-12. Code for Exercise 2
        1 arr = [5, 22, 29, 39, 19, 51, 78, 96, 84]
        2 i = 0 
        3 while (i < arr.size - 1 and arr[i] < arr[i + 1])
        4     i = i + 1 
        5 end 
        6 puts i 
        7 
        8 arr[i] = arr[i + 1]
        9 arr[i + 1] = arr[i]
    
  3. Write a program that splits an array into two arrays where any element in one array is smaller than any element in the other array. Solutions are not unique, but equally sized splits are desirable. The input can be any size array less than 100.

    Example input: [6, 45, 23, 65, 17, 48, 97, 32, 18, 9, 88]

    Example output: [6, 23, 17, 18 , 9] < [45, 65, 48, 97, 32, 88]

  4. There are many ways to store image data. One way is to store pixel data in a two-dimensional array. The pixel data is itself a three-element array that describes the amount of red, green, and blue in the pixel. The amount of red, green, or blue is a number from 0 to 255. Here are a few example RGB values:

    red = [255, 0, 0]
    green = [0, 255, 0]
    blue = [0, 0, 255]
    black = [0, 0, 0]
    white = [255, 255, 255]
    yellow = [255, 255, 0]

    Suppose you have a picture and need to count red pixels. For a pixel to be red, it must be within the following RGB constraints:

    1. The R value must be greater than 100.

    2. The G and B values must each be less than the R value divided by 4.

    Write this program. Use this sample data to test your program:

    sample =
    [[[ 65, 67, 23], [234,176,  0], [143,  0,  0]],
     [[255, 30, 51], [156, 41, 38], [  3,243,176]],
     [[255,255,255], [  0,  0,  0], [133, 28, 13]],
     [[ 26, 43,255], [ 48,  2,  2], [ 57, 89,202]]]

    This sample has three red pixels.

  5. Function-plotting software must calculate a function at many points to plot it. Given the function:

    Code for Exercise 2
    1. Write a program that calculates and stores 100,000 values for f (x) between x = –50 and x = 50.

    2. Extend the program so that it searches for values for x that are very close to, or are, zero. How many x values between –50 and 50 make f(x) zero? What are they?

  6. The three witches in Hamlet can brew any potion provided they have the right ingredients. Suppose that five ingredients are necessary in making a health potion: eye of newt (eon), toe of frog (tof), wool of bat (wob), adder’s fork (af), and tooth of wolf (tow). Four reactions can occur between these ingredients:

    • 4 eon + 2 wob = 3 af + 4 tow

    • 3 tow + 1 tof = 2 eon

    • 1 wob + 2 af = 1 tof

    • 4 tof + 7 tow + 2 af = 1 health potion

      Assuming you can control the order of reactions, write a program that can calculate the maximum number of health potions one can brew with a given amount of ingredients. Here is example output:

      If I have 34 eon, 59 tof, 20 wob, 5 af, and 20 tow, I can make seven health potions.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.236.27