資料結構與演算法設計

未來人類必需學會程式語言來與機器人溝通

Program A: Output to the console screen (25%)
        Write a program that output a text drawing to the console screen as follows. You may also design your own drawing.
    | \_/ |
  / @ @ \
(  >  º  <  )
  `>>x<<´
  /    O    \


Program B: Simple calculation (25%)
        Study section 2.5 in our textbook and study the Program 2.7. Modify Program 2.7 to calculate the average of three numbers. Compile and run your program.


Program C: Simple calculation (25%)
        Study section 2.6 in our textbook and study the Program 2.11. Modify Program 2.11 to calculate the speed of a car whose received radar frequency is 2.00000035  1010 sec-1. Compile and run your program.


Program D: Programming a mathematical formula (25%)
        a. The increase in length of a rectangular slab of metal that’s fixed at one end and pulled by a force at its other end is given by this formula:
I = F X k X l / ( w X d X E )
I is the increase in length (mm).
F is the applied force (N = kg-m/s2).
k is 1000 (conversion of F to millimeter units).
l is the slab’s length (mm).
w is the slab’s width (mm).
d is the slab’s depth (mm).
E is the metal’s modulus of elasticity (N/mm2).
        Given this information, design, write, compile, and run a C++ program to calsulate the increase in length when a slab of aluminum that is 3 meters long, 4 cm wide, and 2 mm deep is subjected to a force of 4 Newtons. Aluminum’s modulus of elasticity is 68,950 N/mm2. (Hint: Make sure to convert the length and width to mm dimensions.) Verify the result produced by your program with a hand calculation.

        b. After verifying that your program is working correctly, use it to determine the increase in length for a slab of copper having the same dimensions as the aluminum slab described above. Copper’s modulus of elasticity is 110,000 N/mm2.




回到最上層

Program A: Simple arithmetic calculation (25%)
        Write a program that inputs three integers from the keyboard and show the sum, average, product, smallest and largest of these numbers. The screen dialog should appear as following example:
Input three different integers: 13 27 17
Sum is 57
Average is 19
Product is 5967
Smallest number is 13
Largest number is 27


Program B: Simple arithmetic calculation (25%)
        Write a program that estimates and displays the temperature in a freezer (in C) given the elapsed time (hours) since a power failure. Assume this temperature (T) is given by
T= (4t^2)/(t+2)-20
where t is the time since the power failure. Your program should prompt the user to enter how long it has been since the start of the power failure in whole hours and minutes. Note that you will need to convert the elapsed time into hours. For example, if the user entered 2 30 (2 hours 30 minutes), you would need to convert this to 2.5 hours.


Program C: Basal metabolic rate calculation (25%)
        The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate, or BMR. The formula for the calories needed for a woman to maintain her weight is:
BMR=655+(9.563 × weight in kg)+(1.850 × height in cm)-(4.676 ×age in years)
The formula for the calories needed for a man to maintain his weight is
BMR=66.5+(13.75 × weight in kg)+(5.003 × height in cm)-(6.755 ×age in years)
        A typical chocolate bar will contain around 230 calories. Write a program that allows the user to input his or her weight in kilograms, height in centimeters, age in years, and the character 'M' for male and 'F' for female. The program should then output the number of chocolate bars that should be consumed to maintain one's weight for the appropriate sex of the specified weight, height, and age. Check the results from your program with any BMR calculators on the internet.


Program D:Game of blackjack (25%)
        In the game of blackjack, the cards 2 through 10 are counted at their face values, regardless of suit; all face cards (jack, queen, and king) are counted as 10; and an ace is counted as a 1 or 11, depending on the total count of all cards in a player’s hand. The ace is counted as 11 only if the resulting total value of all cards in a player’s hand doesn’t exceed 21; otherwise, it’s counted as 1. Using this information, write a C++ program that accepts three card values as inputs (a 1 corresponding to an ace, a 2 corresponding to a two, and so on), calculates and display the total value of the hand, and the value of the three cards.




回到最上層

        
Program A: Calculation of π(Pi) (25%)
        Write a program to calculate the value of the ratio of a circle's circumference to its diameter, π(Pi), using Leibniz series.
π/4=∑_(k=0 to ∞){[(-1)^k]/(2k+1)}
        Calculate the π values for k = 1000, 10000, 100000, and estimate the errors by comparing a constant variable π equals 3.141592653589793238463 in your program. What is the most accurate value of Pi you can obtain?


Program B: Calculation of prime numbers (25%)
        A prime integer number is one that has exactly two different divisors, namely 1 and the number itself. Write, run, and test a C++ program that finds and displays all the prime numbers less than 1000. (Hint: For each number from 2 to 1000, find Remainder = Number % n, where n ranges from 2 to sqrt(Number). If n is greater than sqrt(Number), the number is not equally divisible by n. Why? If any Remainder equals 0, the number is not a prime number.)


Bonus points (25%): (This is an optional problem)
        A pair of positive integer numbers are called twin primes if they are both prime numbers and the difference between them is 2, i.e., they are consecutive odd numbers and they are prime numbers. (3, 5), (5, 7) and (11, 13) are three examples of such pair of twin prime numbers. Write a program to display all the pairs of twin prime numbers that are less than 1000. What is the greatest twin primes you can obtain?


Program C: Use of switch statement and repetition structure (25%)
        The international standard letter/number mapping found on the telephone is shown below:
1                2  ABC       3  DEF
4  GHI        5  JKL        6  MNO    
7  PQRS     8  TUV      9  WXYZ
                   0
        Write a program that is capable of inputing a text string using the ten numeric keys (0~9). For example, by defining ‘1’ as ‘SPACE’ and ‘0’ as “END OF SINGLE CHARACTER INPUT”, a text string “HELLO WORLD” can be input by pressing a series of numeric keys “4403305550555066601090666077705550300”; a text string “GOOD MORNING” can be input by a series of numeric keys “4066606660301060666077706604440660400”. A consecutive input of ‘0’ means the termination of the text string input process.


Program D:Use of function and nested loops (25%)
        The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a function gcd that returns the greatest common divisor of x and y. Also write a C++ program that calls the gcd function repetitively to create a table of greatest common divisor of paired integers from 1 to 20, as the following figure shows.




回到最上層

        
Program A: Perfect number (25%)
        A positive integer is a perfect number if it is equal to the sum of all of its factors, including one but excluding itself. For example, 6 is a perfect number, since 6 = 1 + 2 + 3, and 1, 2, and 3 are factors of 6. Design a PerfectNumber(long int Num) function that determines whether the supplied number Num is a perfect number. Write a program to find all perfect numbers between 1 and 10000 by calling the function PerfectNumber(long int Num). What is the greatest perfect number you can find?


Program B: Statistical calculation using array (25%)
        In many statistical analysis programs, data values considerably outside the range of the majority of values are simply dropped from consideration. Using this information, write a C++ program that accepts up to 10 floating-point values from a user and determines and displays the average and standard deviation of the input values. All values more than two standard deviations away from the computed average are to be displayed and dropped from any further calculation, and a new average and standard deviation should be computed and displayed.


Program C: Data processing and sorting (25%)
        (a). Create a two-dimensional list of integer part numbers and quantities of each part in stock, and write a function that displays data in the array in decreasing quantity order. No more than 100 different parts are being kept track of. Test your program with the following data:
        (b). Modify the function written in part (a) to display the data in part number order.
Part No.        Quantity
1001            62
0949            85
1050            33
0867            125
0346            59
1025            105


Program D: Dice rolling simulation (25%)
        Write a program that simulates the rolling of two dice. The program should use random number generator (C++ rand() function) to roll the first die and should use rand() function again to roll the second die. The sum of two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Your program should roll the two dice 1000, 10000, and 100000 times separately. Use a one-dimensional array to tally the numbers of times each possible sum appears. Display the results in a tabular format. Also, determine if the totals are reasonable by comparing the theoretical probability.




回到最上層

        
Program A: Game of life (50%)
        The game of life is a computer simulation that was created by a Cambridge Mathematician named John Conway. The idea is that in each generation life will populate, survive, or die based on certain rules. Read the Wikipedia article ( http://en.wikipedia.org/wiki/Conway's_Game_of_Life ) to learn more about this famous simulation.
The universe of the Game of Life is a two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbors, the immediately adjacent cells in orthogonal and diagonal direction.
Cells on the border have less than eight neighbors. At each generation, the following transitions occur:
1. Any live cell with fewer than two live neighbors dies, as if by loneliness.
2. Any live cell with more than three live neighbors dies, as if by overcrowding.
3. Any live cell with two or three live neighbors lives, unchanged, to the next generation.
4. Any dead cell with exactly three live neighbors comes to life.
        The following figures show the grid examples for the first generation, second generation, and third generation, respectively.

        Your task is to write a program to simulate the Game of Life for a 20x20 world. The 20x20 world needs to be initialized by reading in which cells are occupied from the user. Your program will generate and display the next generation iteratively based on the set of rules described above. The number of generation for the simulation is also input by the user.

Bonus Points (10%~30%):
        You may obtain bonus points by implementing a better display of simulation results using the Console class for the standard input, output, and error streams for console applications. You may check the MSDN website for more detailed description of the usage of Console class (http://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx).
        An example program ConsoleOutputExample.cpp is also provided in the CEIBA course website for your reference on the commands you need for the control of screen output. The major commands you may use are as follows:
        Console::SetCursorPosition(x, y);        // Sets the position of the cursor
        Console::Write(System::Char);        // Writes the Unicode character to the screen
        Console::Clear();        // Clear the console screen

Note:
1. You need to create a “CLR Console Application” project (not an Win32 Console Application) for your program in order to use the Console class.
2. You need to add the “using namespace System;” before main().


Program B: Magic square (50%)
        A magic square is a square of numbers with N rows and N columns, in which each integer value from 1 to (NXN) appears exactly once, and the sum of each column, each row, and each diagonal is the same value. For example, the following figure shows a magic square in which N = 3, and the sum of the rows, columns, and diagonal is 15. Write a program that constructs and displays a magic square for any given odd number N. This is the algorithm:

Insert the value 1 in the middle of the first row (element [0][N%2]).
After a value, x, has been placed, move up one row and to the right one column.
Place the next number, x+1, there, unless:
        (1) You move off the top (row = -1) in any column. Then move to the bottom row and place the next number, x+1, in the bottom row of that column.
        (2) You move off the right end (column = N) of a row. Then place the next number, x+1, in the first column of that row.
        (3) You move to a position that is already filled or out of the upper-right corner. Then place the next number, x+1, immediately below x.
Stop when you have placed as many elements as there are in the array.




回到最上層

        
Program A: Bridge card game (50%)
        In a typical card game, each player gets a hand of cards. The deck is shuffled and cards are dealt one at a time from the deck and added to the players' hands. Bridge uses a pack of 52 playing cards. There are 4 suits (spades, hearts, diamonds, clubs) each of 13 cards: 1 (the Ace) to 10 and Jack, Queen, King. The Ace is the highest card, followed by the King, Queen, down to the 2.
        You are requested to design a program to simulate dealing cards to 4 players. The program needs to simulate at least three times of card shuffling and dealing and saves the results into a file as well as displaying the results on the screen. Your program should also calculate and show the total high-card points of each player. High-card points are counted as follows: A = 4 points, K = 3 points, Q = 2 points, and J = 1 point.
Bonus Points (10%~30%):
        You may obtain bonus points by implementing a better display of simulation results using the Console class for the standard input, output, and error streams for console applications. You may check the MSDN website for more detailed description of the usage of Console class (http://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx).
Note:
        1. You need to create a “CLR Console Application” project (not an Win32 Console Application) for your program in order to use the Console class.
        2. You need to add the “using namespace System;” before main().


Program B: Morse code (50%)
        Perhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit and a few special characters (such as period, comma, colon and semicolon). In sound-oriented systems, the dot represents a short sound, and the dash represents a long sound. Other representations of dots and dashes are used with light-oriented systems and signal-flag systems. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented system, a space is indicated by a short period of time during which no sound is transmitted. The international version of the Morse code is shown in the following table.

        Write a program that reads a file named MorseCode.txt and convert it into Morse code. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word. Display the result on the screen and save the converted Morse code into a file named MorseCode.dat. You may also play the sound of Morse code with your program. The file MorseCode.txt has the content as below :

        Morse code is one of several adapted computer access methods and alternative communication techniques that can be effective in helping enhance the lives of persons with special needs. Those without the ability to speak, sign, or use keyboards may benefit from this proven system of communication.

        Some information about Morse code can be found in the following link:
        http://morsecode.scphillips.com/




回到最上層

        
Program A: Streams and File I/O (50%)
        The text file words.txt, which is provided on our CEIBA course website, contains an alphabetically sorted list of English words. Note that the words are in mixed upper and lowercase.
        Write a program that reads this file and finds the longest word that reverses to a different word. For example, “stun” reverses to make the word “nuts” but is only four letters long. Find the longest such word.


Program B: Classes (50%)         Create a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initialized num and denom to 1 if there is no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object’s data value and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as follows:

Addition:                a/b + c/d = (a * d + b * c) / (b * d)
Subtraction:           a/b - c/d = (a * d - b * c) / (b * d)
Multiplication:        a/b * c/d = (a * c) / (b * d)
Division:                (a/b) / (c/d) = (a * d ) / (b * c)

        Finally, your class should have a member function that reduces each fraction to its terms as well as input and output function for entering and displaying a fraction. Implement a C++ main program that tests each member function.


Challenge Program: Sodoku (Bonus Points 50%)
        Solve a partially filled-in normal 9X9 Sudoku grid and display the result in a human-readable format. Backtracking algorithm, but not the only algorithm, is usually used to solve the Sudoku that iterates all the possible solutions for the given sudoku. If the solutions assigned do not lead to the solution of Sudoku, the algorithm discards the solutions and rollbacks to the original solutions and retries again and hence the name backtracking.
Below is the general pseudocode of backtracking algorithm for standard sudoku template (9X9):

_________________________________________________________________________________
Initialize 2D array with 81 empty grids (Row=9, Col= 9)
Fill in some empty grid with the known values
        Make an original copy of the array
        Start from top left grid (nx = 0, ny = 0), check if grid is empty
        if (grid is empty) {
                assign the empty grid with values (i)
        if (no numbers exists in same rows & same columns same as (i) & 3x3 square (i) is currently in)
                        fill in the number
if (numbers exists in same rows & same columns same as (i) & 3x3 square (i) is currently in)
                discard (i) and repick other values (i++)
}
else {
while (nx < 9) {
                Proceed to next row grid(nx++, ny)
                if (nx equals 9) {
                        reset nx = 1
                proceed to next column grid(nx,ny++)
                if (ny equals 9) {
                        print solution
                        }
                }
        }
}
_________________________________________________________________________________
        Your program should allow the user to input a Sodoku game to be solved via a text file and then save the solution in an output file as well as displaying it on the screen. You also need to record and show the computation time of the solution process.
        Test your program with the following puzzles representing three different difficulty levels. Compare the computation time of these three puzzles.




回到最上層

Recent Comments

施工中

進度完成百分比:

20%