# Algorithms And Logic For Computer Programming

Algorithms And Logic For Computer Programming

1,

## Branches: Leap Year

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 4

2) If the year is a century year (1700, 1800, etc.), the year must be evenly divisible by 400

Some 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:

`1712`

the output is:

`1712 is a leap year.`

Ex: If the input is:

`1913`

the output is:

`1913 is not a leap year.`

`2, `

Branches: Remove gray from RGB

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 130`

the output is:

`80 0 80`

Hint: Find the smallest value, and then subtract it from all three values, thus removing the gray

3, Branches: Largest number

Write a program that reads three integers as inputs, and outputs the largest of the three values.

Ex: If the input is:

`7 15 3`

the output is:

`15`

`3,`

Variables/Assignments: Divide by x

Write a program using integers userNum and x as input, and output userNum divided by x four times.

Ex: If the input is:

`2000 2`

the output is:

`1000 500 250 125`

Note: In Coral, integer division discards fractions. Ex: 6 / 4 is 1 (the 0.5 is discarded).

4, Variables/Assignments: Driving costs

Driving is expensive. Write a program with a car’s miles/gallon and gas dollars/gallon (both floats) as input, and output the gas cost for 10 miles, 50 miles, and 400 miles.

Output the gas cost (gasCost) with two digits after the decimal point, which can be achieved as follows:

`Put gasCost to output with 2 decimal places`

Ex: If the input is:

`20.0 3.1599`

the output is:

`1.58 7.90 63.20`

Note: Small expression differences can yield small floating-point output differences due to computer rounding. Ex: (a + b)/3.0 is the same as a/3.0 + b/3.0 but output may differ slightly. Because our system tests programs by comparing output, please obey the following when writing your expression for this problem. First use the dollars/gallon and miles/gallon values to calculate the dollars/mile. Then use the dollars/mile value to determine the cost per 10, 50, and 400 miles.

Note: Real per-mile cost would also include maintenance and depreciation.

5, Variables/Assignments: Using math functions

Given three floating-point numbers x, y, and z, output x to the power of y, x to the power of (y to the power of z), the absolute value of x, and the square root of (x * y to the power of z).

Output all results with five digits after the decimal point, which can be achieved as follows:

`Put result to output with 5 decimal places`

Ex: If the input is:

`5.0 2.5 1.5`

the output is:

`55.90170 579.32402 5.00000 6.64787`

Hint: Coral has built-in math functions (discussed elsewhere) that may be used.

6, Variables/Assignments: Using math functions

Given three floating-point numbers x, y, and z, output x to the power of y, x to the power of (y to the power of z), the absolute value of x, and the square root of (x * y to the power of z).

Output all results with five digits after the decimal point, which can be achieved as follows:

`Put result to output with 5 decimal places`

Ex: If the input is:

`5.0 2.5 1.5`

the output is:

`55.90170 579.32402 5.00000 6.64787`

Hint: Coral has built-in math functions (discussed elsewhere) that may be used.

7, Arrays: Middle item

Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd.

Ex: If the input is:

`2 3 4 8 11 -1 `

the output is:

`4`

The maximum number of inputs for any test case should not exceed 9 positive values. If exceeded, output “Too many inputs”.

Hint: Use an array of size 9. First, read the data into an array. Then, based on the number of items, find the middle item.

8,Arrays: Output values below an amount

Write a program that first reads a list of 5 integers from input. Then, read another value from the input, and output all integers less than or equal to that last value.

Ex: If the input is:

`50 60 140 200 75 100`

the output is:

`50 60 75`

For coding simplicity, follow every output value by a space, including the last one. Then, output a newline.

Such functionality is common on sites like Amazon, where a user can filter results.