Previously Assigned Problems from the WCU CS Contest (1990-1998) -------------------------------- P1. Diamonds: Your program should prompt for an integer value which will serve as the number of asterisks across the middle of a figure which has the appearance of a diamond. The program should print the figure. Example: Width of diamond --> 5 * * * * * * * * * * * * * -------------------------------- P2. Matrix: Your program should prompt for two integers, which represent the number of rows and columns in a two dimensional array. Your program should then prompt for the necessary integer values to fill the array. Your program should output the two-dimensional array and the positions and value of the second largest entry in the array. Example: Dimension --> 3 Input--> 1 4 7 2 8 8 1 10 2 OUTPUT: 1 4 7 2 8 8 1 10 2 The maximum element, 10, is at position (3,2) -------------------------------- P3. Primes: Your program should prompt for a positive integer to use as the upper limit for searching for prime numbers. Your program should then print out a list of all prime numbers less than or equal to this upper limit. (Note: A prime number is defined as an integer larger than one which has no positive integer divisors other than itself and one.) Exampe: Limit --> 13 Prime --> 2 3 5 7 11 13 -------------------------------- P4. 3X + 1: construct a sequence of positive integers as follows. Begin with X, an arbitrary positive integer. The next element in the sequence is X/2 if X is even, or (3X + 1)/2 if X is odd. Continue in this fashion, that is, given the kth term, Xk, the (k + 1)th term, Xk + 1 is given by: Xk + 1 = Xk/2 if Xk is even, (3Xk + 1)/2 if Xk is odd. Your program should prompt for a starting value for X and build the sequence until the integer 1 is obtained. Your program should output the starting value and the length of the sequence, (ie. the number of terms in the sequence). Example: X --> 7 Sequence: 7, 11, 17, 26, 13, 20, 10, 5, 8, 4, 2, 1 Length --> 12 -------------------------------- 1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for example, 1980), except that it is not a leap year if it is divisible by 100 (for example, 1900); however it is a leap year if it is divisible by 400 (for example, 2000); and there were no leap years before the introduction of the Gregorian calendar on October 15, 1582. Write a program that asks the user for a year and computes whether that year is a leap year. Example: Input 1980 Output Leap Year Input 1900 Output Not a Leap Year Input 1200 Output Not a Leap Year -------------------------------- 2. Given an integer n > 1 and an integer p > 1 you are to write a program that determines the positive nth root of p. In this problem, given such integers n and p, p will always be of the form k^n for an integer k ( this integer is what your program much find). Example: Input 2 2 (n=2, p=2) Input 5 243 (n=5, p=243) Output no such integer k Output k=3 -------------------------------- 3. When the number is 64,253 is multiplied by 365 the product is 23,452,345. Notice that the first four digits are the same as the last four digits (2345 and 2345). Write a program that will find any and all integers that can be multiplied by 365 to produce an eight-digit product where the first four digits are the same as the last four digits. In the example above, no digits were repeated, but in your program, the product can have repetition. For example, 44,884,488. (Hint: Use mod) -------------------------------- 4. Write a program that converts a given text to "Pig Latin". Pig Latin consists of removing the first letter of each word in a sentence and placing that letter at the end of the word. This is followed by appending the word with letters "ay". Example Input THIS IS A TEST Output HISTAY SIAY AAY ESTTAY -------------------------------- 1. Write a program which takes any positive integer less than or equal to 10. The program outputs a square on the screen with sides of that length. The square is filled in the following way. (a) the outermost square is drawn using a's, (b) the nested square that is adjacent to the outermost is drawn using b's, the next is drawn using c's, etc. Example Input: 5 Output: a a a a a a b b b a a b c b a a b b b a a a a a a Input: 8 Output: a a a a a a a a a b b b b b b a a b c c c c b a a b c d d c b a a b c d d c b a a b c c c c b a a b b b b b b a a a a a a a a a -------------------------------- 2. An integer is perfect if it is the sum of its proper divisor (i.e. not including itself). Write a program that prompts a user to input a positive integer and outputs whether it is perfect or not. The program should let the user repeat this computation for new input values until the user inputs -1 to end the program. Example Input: 28 Output: 28 is a perfect number. Input: 10 Output: 10 is not a perfect number. (Note: 28 is perfect since 1 + 2 + 4 + 7 + 14 = 28 10 is not perfect since 1 + 2 + 5 = 8 (10) -------------------------------- 3. Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter. Example Input: I say Hi. Output: 3 words 1 a 1 h 2 i 1 s 1 y -------------------------------- 4. A company wants to transmit data over the telephone line, but they are concerned that their lines are tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that encrypts their data so that it may be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by ((digit + 7) mod 10). Then, swap the first digit with the third, swap the second digit with the fourth, and print the encrypted integer. Example Input: 6254 Output: 2139 -------------------------------- P1. "Trimmed Mean" In your account you will find a partially completed program named TMEAN. It reads an array of integers and displays the array, then quits. You are to complete the program so it that computes the "trimmed mean" of the array. This means that the largest and smallest numbers are to be excluded from the calculations. The answer should be written to 2 decimal places. Example: Input ---- 5 12 8 3 Output -- "Trimmed Mean = 6.50" (obtained from (5 + 8) divided by 2) Notes: If in making changes, you damage TMEAN, there is a backup copy of the same program named P1, which you can use to restore TMEAN, by the VAX command: COPY P1 TMEAN -------------------------------- P2. "Number of Digits" You are to write a complete program, named DIGITS, which should prompt for a variable of type integer. It can by negative, zero, or positive. The program must then determine how many digits the integer contains. Example: Input --- 3872; Output --- "Integer contains 4 digits" Input --- -402; Output --- "Integer contains 3 digits" Input --- 0; Output --- "Integer contains 1 digits" -------------------------------- P3. "Combining Arrays' In your account you will find a program named COMBINE. The program, as it now stands, reads M distinct integers, in random order, into array A, and similarly reads N distinct integers into array B. M and N can by in the range 0 to 100. Either or both arrays can be empty. The program displays the contents of A and B, then stops. Change the program so that it will also combine the contents of A and B into array C, eliminating all duplicates, then write the contents of C. Examples: Input --- 3 8 1 7 (Array A) 4 8 6 5 3 1 (Array B) Output --- "Array C contains 3 8 1 7 4 6 5" Note: If in making changes, you damage COMBINE, there is a backup copy of the same program named P3, which you can retrieve to restore COMBINE, by giving the VAX command: COPY P3 COMBINE. -------------------------------- P4. "Pascal's Triangle" Your program, named TRIANGLE, is to display N rows of Pascal's Triangle. The program must prompt for N, then print the triangle so that its peak is at top center. Your program must work for N from 1 to 10. Spacing must take into account the possibility of numbers of varying sizes. Leave at least two spaces between the numbers of each row, and double space the rows. Example: Input --- 6 (the number of rows) Output ---(at top center) Pascal's Triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 -------------------------------- P1. Input real numbers A and B. Without performing any arithmetic computations, decide whether the sum of A and B is positive, negative, or zero. OUTPUT SHOULD LOOK LIKE: The Sum of 2.987 and -9.345 is Negative. P2. Given a positive integer as input. If it has exactly three digits, then determine if the integer equals the sum of the cubes of its digits. OUTPUT SHOULD LOOK LIKE: The integer 153 is equal to the sum of the cubes of its digits. or The integer 103 is not equal to the sum of the cubes of its digits. or The integer 56 does not have three digits. --------------------- P3. Read a list of up to 100 numbers. Call this list DATA1. Place the positive numbers from DATA1 into an array POS and the negatives into array NEG. Then print the list DATA1 and the lists POS and NEG. OUTPUT SHOULE LOOK LIKE: Original List 2, -3, -4, -6, 7, 9, -13, 5, -7 The Positives are 2, 7, 9, 5 The Negatives are -3, -4, --6, -13, -7 or Original List 2, 3, 4, 5, 8, 9 The Positive are 2, 3, 4, 5, 8, 9 There are no negatives --------------------- P4. Write a program to calculate and display the first ten rows of a table according to the following rules: (a) The table has four columns: n, a, b, c. (b) The values in the first row are 0, 1, 1, 1. (c) In any row after the first: (i) the value of n is one greater than its value in the preceding row. (ii) the value of a is one greater than its value in the preceding row. (iii) the value of b is one greater than the sum of the values of a to and including the preceding row. (iv) the value of c is one greater than the sum of the values of b to and including the preceding row. --------------------- 1. Write a program that prints a tree like the one shown below. Your program should prompt the user for an integer n so that the number of asterisks in the widest row of the tree is (2n-1). The tree should be centered on a page that you may assume is 79 columns wide. The "trunk" of the tree should be three asterisks wide and three rows tall * *** ***** ******* ********* *** *** *** ---------------------------- 2. Write a program using arrays and the best strategy for sorting a list of numbers in non-decreasing order. Assume that each number is either a 0 or a 1. Your program should prompt the user for the size of the list and then prompt the user to enter the numbers (a list of zeros and ones). ---------------------------- 3. Write a program to evaluate the sums of the form Sn = 1 + 2 + 22 + 23 + ... + 2n where n is the user input. Example: S4 = 1 + 2 + 22 + 23+ 24= 31 ---------------------------- 4. Write a program which reverses an integer n entered from the keyboard. If the input is 23985, the program should output 58932. are concerned that their lines are tapped. All of their data is transmitted as four-digit integers. They have asked you to write a program that encrypts their data so that it may be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by ((digit + 7) modulus 10). Then, swap the first digit with the third, swap the second digit with the fourth, and print the encrypted integer. Example Input: 6254 Output: 2139 B1. TRIANGLE: INPUT A NUMBER AND PRINT A TRIGANGLE WHOSE BASE SIDE CONTAINS THAT NUMBER OF *'S. PLACE THE VERTICAL CENTER LINE OF THE TRIANGLE AT COLUMN 35 (THAT IS: CENTER THE TRIANGLE) EXAMPLE: IF 5 IS INPUT OUTPUT LOOKS LIKE: * * * * * * * * * --------------- B2. ARRAY: INPUT A ONE DIMENSIONAL ARRAY AND CHECK TO SEE IF ALL ELEMENTS ARE THE SAME, IF SO PRINT THE ELEMENT AND EXIT THE PROGRAM. IF ALL THE ELEMENTS ARE NOT THE SAME, THEN PRINT THE MESSAGE "THE SECOND LARGEST ELEMENT IS "FOLLOWED BY THE SECOND LARGEST ELEMENT. (EXAMPLE 1, 2, 5, 7) SHOULD PRINT THE SECOND LARGEST ELEMENT IS 5 WHILE 2,2,2,2,2,2 SHOULD CAUSE THE ELEMENT IS 2 TO BE PRINTED. B3. TABLE: THE TABLE BELOW SHOWS THE NUMBER OF SALES OVER A ONE WEEK PERIOD FOR FOUR SALESPERSONS. MON TUES WED THURS FRI SAT TOTAL SALES PERSON 1 5 8 14 6 9 12 54 SALES PERSON 2 7 2 8 15 3 7 42 SALES PERSON 3 10 8 12 11 12 15 68 SALES PERSON 4 8 10 9 4 11 6 39 USING THE ABOVE VALUES SET UP A TWO DIMENSIONAL ARRAY USING READ AND DATA STATEMENTS. THE FIRST COLUMN WILL CONTAIN THE NUMBER OF THE SALESPERSON. THE LAST COLUMN WILL CONTAIN THE WEEKLY TOTAL SALES FOR EACH SALESPERSON. THE LAST ROW WILL CONTAIN THE TOTAL SALES FOR THAT DAY OF THE WEEK. CALCULATE THE VALUES IN THE LAST ROW AND COLUMN AND PRINT OUT YOUR COMPLETE TABLE. ALSO, PRINT CONGRATULATONS TO YOUR SALESPERSON WITH THE LARGEST NUMBER OF SALES AND THE NUMBER OF THIS PERSON. EXAMPLE OF SALESPERSON PRINTOUT: CONGRATULATIONS NUMBER 3 FOR SALES OF 68 FOR THE WEEK. ---------------------------- 1. Write a program which will find and display all three-digit positive integers that are divisible by the product of their digits. ---------------------------- 2. Consider the following scheme for computing the cost of a direct dial long distance call, based on the time the call originates, the length of the call, and a basic rate of 107 cents for the first minute, and 57 cents/minute for all additional minutes. Time Call Day of Originates Week Discount 7 am - 5 pm weekday none 5 pm- 11 pm any day 20% 11 pm- 7 am any day 40% 7 am - 11 pm Saturday 20% 7 am- 5 pm Sunday 40% Write a program which will allow entry of data for the following: day of the week (1 for Sunday, 2 for Monday, ..., 7 for Saturday); time call originates, in terms of a 24 hour clock (to calculate length of call). Your program should print out the cost of a call in cents. For example, a call that starts at 11:35 pm on Friday night and ends at 12:15 Saturday morning would have entry data of 6 for day of the week, 2335 for starting time, and 0015 for ending time. The program must calculate the elapsed time of 40 minutes, and calculate the cost as 107 cents for the first minute plus 39 times 57 cents for the rest of the minutes, less a 40% discount of the total for the day and time. ---------------------------- 3. "Walking a List": Consider the following two lists and the variable START: x (1) = 94 L (1) = 0 x (2) = 27 L (2) = 4 x (3) = 17 L (3) = 2 START = 3 x (4) = 30 L (4) = 5 x (5) = 35 L (5) = 1 The purpose of START is to indicate the location in X of the first number to be printed, in other words, the 17. Having written the first number, the corresponding number in the L list gives the location of the second number in the X list that is to be printed, i.e., the 27. After the second number is printed, the corresponding number in the L list gives the location of the third number in the X list that is to be printed, and so on, until a zero is encountered in the L list. So the output in this example will be: 17 27 30 35 94 Write a program which will store the above list Z, and a value for START. The user is to be prompted for the L list, containing the location numbers, but not necessarily in the same order as in the example. For instance, if you enter into L the sequence 4, 2, 1, 0, 3, the output would be 17,94, 30, because the 0 would be reached after only three numbers had been written. For any appropriate L list, the program should then display the numbers according to the scheme above. To save time, you may store the data in the arrays at the same time you declare them, using the following special Pascal statements: Var X: array (1...5) of integer: = (35, 27, 94, 30, 17); ---------------------------- 4. You are given a golfer's scores in each of four rounds of six tournaments in which she played. Write a program to accomplish all of the following: a) print her average score for each tournament b) print her average for all twenty-four rounds c) print her lowest and highest scores for the twenty-four rounds d) print her highest and lowest tournament averages Data might look like the following (user should be able to input data for each tournament): TOURNAMENT SCORES Blue Creek 72, 73, 70, 68 Hob Nob 80, 70, 65, 68 Ground Hog 71, 70, 64, 68 Happy Hill 58, 72, 66, 67 Atla Hope 72, 77, 67, 75 Dog Bone 70, 70, 65, 72 ---------------------------- 1. Give two times as illustrated below, the program should return their sum reduced to the lowest possible terms. Example: 3 da 15 hr 45 min Input 3 15 45 + 2 da 12 hr 35 min 2 12 35 6 da 4 hr 20 min Output 6 4 20 ---------------------------- 2. Given a sequence of integers, the program should determine whether or not they are in order and, if they are not, give the last two values which are out of order. Example: Input 1 2 4 3 6 5 7 9 Output 6 5 ---------------------------- 3. Given a positive integer, the program should determine if it is a prime number and give a list of all prime numbers less than the given integer. Example: Input 26 Output Not Prime 2 3 5 7 11 13 17 19 23 ---------------------------- 4. Given a "word," the program should determine whether or not it is a palindrome. (A palindrome is a word which remains unchanged when spelled from left to right or right to left.) Example: Input RADAR Output Palindrome ------------------------------ BOB'S CHARRED BURGER SHOP PRICE LIST Regular Burger $1. 95 Soft Drink: $.90 French Fries: $.75 1. i) Given Bob's ad above wire a program that will calculate the bill for a customer. The input should reflect the customer's order and the output should be the total price of the order. Assume that a 6% sales tax is applicable to all sales. ii) After the total price is displayed, prompt for the amount paid. Your program should then determine the customer's change. ------------------------------ 2. A list of n integers k1, k2,..., kn is in order provided k1< k2 < ...< kn. Write a program which will prompt for the number of integers and will allow input of the integers from the keyboard. Determine if the list is in order or not. If the list is not in order, determine the number of times it is out of order. Example: 2 5 9 13 24 is in order 2 7 5 6 4 3 12 is out of order 3 times since 7 < 5, 6 < 4, and 4 < 3. ------------------------- 4. Ghost is a word-guessing game. The goal is to guess the hidden word using as few guesses as possible. One player enters a word that will be hidden. The program should then report the length of the word. The second player then enters a guess for the hidden word. The guesser is told whether the guess is correct or not. It the guess is incorrect, the guesser is told how many letter in the guessed word are also in the hidden word (ignoring the position of the letters). Create a complete Ghost program. Example: Player 1 enters word "happily" Player 2 sees on the screen "Word has 7 letters" When Player 2 guesses "healthy," the program returns "There are four matches" ------------------------- 1. Write two programs which reverse an integer n entered from the keyboard. The first program should include a function (function Reverse (n : integer) : integer) which reverses n, so that Reverse (4723) = 3274. Example: Input 3295 Output 5923 ------------------------- 2. Write a program to calculate sum of the form Sn = 1 + 2 + 2^2 + 2^3 + .... + 2^n where n is the user input and ^ denotes exponentiation Example: Input 4 Output 31