Zybook Labs - Programming and Scripting Foundations Flashcards Part 1Given 3 integers, output their average and their product, using integer arithmetic.Ex: If the input is:10 20
5the output is:11 1000Submit the above for grading. Your
program will fail the test cases (which is expected), until you complete part 2 below but check that you are getting the correct average and product using integer division.Part 2Using the same 3 integers, output the average and product, using floating-point arithmetic.Output all floating-point numbers with five digits after the decimal
point, which can be achieved as follows:Put x to output with
- decimal placesEx: If the input is:10 20 5the first line of
output uses integer arithmetic and the second line output
uses floating-point arithmetic:11 100011.66667 1000.00000
// Your solution goes here//part 1//declare variablesinteger input1integer input2integer input3integer averageinteger productfloat average2float product2//inputinput1 = Get next inputinput2 = Get next inputinput3 = Get next input//processaverage = (input1 + input2 + input3)/3product = (input1input2input3)//outputPut average to outputPut " " to outputPut product to output//part 2//inputs are used from part one//processaverage2 = (input1 + input2 + input3)/3.0product2 = (input1input2input3)//outputPut "" to outputPut average2 to output with 5 decimal placesPut " " to outputPut product2 to output with 5 decimal places
Summary: Given integer values for red, green, and blue,
subtract the gray from each value.Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255.Thus (255, 0, 0) is bright red, (130, 0, 130) is a medium purple, (0, 0, 0) is black, (255, 255, 255) is white, and (40, 40, 40) is a dark gray. (130, 50, 130) is a faded purple, due to the (50, 50, 50) gray part. (In other words, equal amounts of red, green, blue yield gray).Given values for
red, green, and blue, remove the gray part.Ex: If the input
is:130 50 130the output is:80 0 80Hint: Find the smallest value, and then subtract it from all three values, thus removing the gray.// Your solution goes here//variablesinteger redinteger greeninteger blueinteger minValue//inputred = Get next inputgreen = Get next inputblue = Get next input//process//finding min valueminValue = redif green < minValueminValue = greenif blue < minValueminValue = blue//subtract min value from all colorsred = red - minValuegreen = green - minValueblue = blue - minValue//outputPut red to outputPut " " to outputPut green to outputPut " " to outputPut blue to output Given an integer representing a 7-digit phone number, excluding the area code, output the prefix and line number, separated by a hyphen.Ex: If the input is:5551212the output is:555-1212Hint: Use % to get the desired rightmost
digits. Ex: The rightmost 2 digits of 572 is gotten by 572 %
100, which is 72.Hint: Use / to shift right by the desired
amount. Ex: Shifting 572 right by 2 digits is done by 572 /
100, which yields 5. (Recall integer division discards the fraction).For simplicity, assume any part starts with a non-zero digit. So 011-9999 is not allowed.// Your solution goes here//variablesinteger phoneNuminteger first3integer last4//inputphoneNum = Get next input//processfirst3 = phoneNum / 10000last4 = phoneNum % 10000//outputPut first3 to outputPut "-" to outputPut last4 to output Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer
in binary. For an integer x, the algorithm is:As long as x is
greater than 0 Output x % 2 (remainder is either 0 or 1) x =
x / 2Note: The above algorithm outputs the 0's and 1's in
reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.// Your solution goes here//variablesinteger userNum//inputuserNum = Get next input//process & outputwhile userNum > 0//outputPut userNum % 2 to output//processuserNum = userNum / 2
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366
days: An extra day, February 29th. The requirements for a
given year to be a leap year are:1) The year must be
divisible by 42) If the year is a century year (1700, 1800, etc.), the year must be evenly divisible by 400Some example leap years are 1600, 1712, and 2016.Write a program that takes in a year and determines whether that year is a leap year.Ex: If the input is:1712the output is:1712 is a leap year.Ex: If the input is:1913the output is:1913 is not a leap year.//variablesinteger inputYear//inputinputYear = Get next input//outputPut inputYear to outputPut " is " to output//processif (inputYear % 4) == 0if ((inputYear % 100) == 0) and ((inputYear % 400) != 0)Put "not " to outputelsePut "not " to output//outputPut "a leap year." to output Write a program that reads a list of 10 integers, and outputs those integers in reverse. For coding simplicity, follow each output integer by a space, including the last one. Then, output a newline.Ex: If the input is:2 4 6 8 10 12 14 16 18
20the output is:20 18 16 14 12 10 8 6 4 2To achieve the
above result, first read the integers into an array. Then output the array in reverse.//variablesinteger array(10) userIntsinteger ifor i = 0; i < userInts.size; i = i + 1userInts[i] = Get next inputfor i = userInts.size - 1; i >= 0; i = i - 1Put userInts[i] to outputPut " " to outputPut "" to output Write a program that reads three integers as inputs, and outputs the largest of the three values.Ex: If the input is:7
15 3the output is:15
// Your solution goes here//variablesinteger num1integer num2integer num3//inputnum1 = Get next inputnum2 = Get next inputnum3 = Get next input//processif (num1 >= num2) and (num1 >= num3)//outputPut num1 to outputelseif (num2 >= num1) and (num2 >= num3)//outputPut num2 to outputelseif (num3 >= num1) and (num3 >= num2)//outputPut num3 to output On a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * rn, where n is the distance (number of keys) from that key, and r is 2(1.0/12.0). Given an initial key frequency, output that frequency and the next 4 higher key frequencies.Output all frequencies with five digits after the decimal point, which
can be achieved as follows:Put frequency to output with 5
decimal placesEx: If the input is:440.0(which is the A key near the middle of a piano keyboard), the output
is:440.00000 466.16376 493.88330 523.25113
554.36526Note: Use one statement to compute r =
2(1.0/12.0) using the RaiseToPower() function. Then use that r in subsequent statements that use the formula fn = f0
- rn with n being 1, 2, 3, and finally 4.
// Your solution goes here//variablesfloat f0float frequency1float frequency2float frequency3float frequency4float r//inputf0 = Get next input//processr = RaiseToPower(2, (1.0/12.0))frequency1 = f0 * RaiseToPower(r, 1)frequency2 = f0 * RaiseToPower(r, 2)frequency3 = f0 * RaiseToPower(r, 3)frequency4 = f0 * RaiseToPower(r, 4)//outputPut f0 to output with 5 decimal placesPut " " to outputPut frequency1 to output with 5 decimal placesPut " " to outputPut frequency2 to output with 5 decimal placesPut " " to outputPut frequency3 to output with 5 decimal placesPut " " to outputPut frequency4 to output with 5 decimal places Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average (using integer division) and max. A negative integer ends
the input and is not included in the statistics.Ex: When the
input is:15 20 0 5 -1the output is:10 20Assume that at least one non-negative integer is input.// Your solution goes here//variablesinteger userIntinteger sumIntsinteger numIntsinteger maxInt//inputsumInts = 0numInts = 0maxInt = -1userInt = Get next input//processwhile userInt >= 0sumInts = sumInts +