1 ENGR 490: Software Tools for Engineering Laboratory Lab 5: Flowchart and Pseudocode Demo due Friday, September 30 th, 2011 (by 4PM) Objective We will discuss the software design using Flowchart and Pseudocode. In the assignment, students need to make the flowchart or pseudocode before writing the program. Part 0: Admin stuffs 1. Let s build the folder where you will save your project files. I suggest c:\projects\yourname\engr490\lab5. All files should be organized and put in dropbox. Part 1: Shuffle the array 1. If we want to make 1 random number, it will be very simple by calling rand() command. 2. How about if we want to make N random number that each number is not repeated. This application is suitable for making games, multiple choices. 3. There are many ways to do but Dr. Somsak will show you 2 methods using flowchart first. a. Create a new project called ExampleShuffleArray1 b. The first idea is to create an array with size N, and initialize with number from 0 to N-1 in order. c. Then we will pick 2 numbers from 0 to N-1 representing the indexes and swap the content inside that 2 locations. Repeat this process many times, eventually the content will be random and never repeat. See the flowchart.
4. If you agree that the flowchart is correct and covers everything that you need, we can start writing the code now. Run and you will see the results. At this point, I would recommend to include a bunch of header files since you don t remember which command is belong to which one. 5. Notice that way that I use #define instead of hard code number. Because we can change the size of array to anything easy by changing one place, instead of doing many times. 2 6. Run the program and see results. Try to remember the number for a few samples. Run again, see if it is random and never repeat. 7. You may be able to shorten the loop by doing just 1000 times, instead of 1 million. It should yield the fine result since it is too small array. 8. Try to make a random for array size 52. You can simple by changing the N in define.
Part 2: Shuffle the array part 2 1. The method in part 1 for shuffling array or generating nonrepeat numbers is simple. It has a disadvantage that we create the array size N ahead of time. If the N is big, 5000, it can use a lot of memory and time to shuffle. 2. Sometimes in application that we want to make a few array but the content is nonrepeat and big numbers. We should use different method. 3. The second method that we will use based on making one random number at the time. Each time that we make, we will check if it is already created, if yes we will repeat until it is never created. 4. As you can see, we know that we need to make N random number, use for-loop. 5. But we don t know how many times we need to check with the duplicated numbers. In this case, we will involve using while. 6. Following is the flowchart, see that is more complicate than the first one but it is faster to get the outputs. a. Variable i is for index of X, keep track the new random number b. Variable k is for index that checks duplicate with anything that X has so far. c. This method can do any range, does not need to be 0 to N-1, can be 1 million to 2 millions. 3
7. Make a new project, write this code. Run. You will see it makes 10 numbers from 100 to 1000. You can change any number in the define. 8. BUG: what happen if you define N is 10, and random max and min are 0 and 5 respectively. Try and run. Why nothing shows up? 9. In the future, feel free to use either one of this method to make shuffled array. 4
5 Part 3: Sorting Array 1. If you have the array size N and would like to sort from minimum to maximum (or max to min), there are several methods we can do such as bubble sort. 2. The idea of bubble sort is to compare 2 numbers at the time and switch them if it is not in order. The comparison will continue until there is nothing changed or swapped. 3. I recommend you to look at this link for visualization. http://en.wikipedia.org/wiki/bubble_sort 4. The pseudo code of this bubble sort can be written as 5. Copy part 2 code folder to a new one. Name the folder ExampleSort. 6. Add sorting once you have the random array, before printing out. Don t forget to declare variables for swapped and temp.
6 C code implementation //assume you have an unsorted array size N //use results from part 2 //see how I translate pseudocode to C code swapped = 1; while (swapped){ swapped = 0; for(i=0;i<n-1;i++){ if (X[i] > X[i+1]){ //swap 2 elements temp = X[i]; X[i] = X[i+1]; X[i+1] = temp; swapped = 1; } } } //show X 7. You should see that the outputs show array of random data that also sorted from min to max. 8. Question 1: can you simply modify a little, to sort from max to min. It should be easy. 9. Question 2: based on pseudo code and C code, can you write the flowchart? The solution is on the next page, don t look at it until you practice to write the flowchart. 10. If you want the program for drawing flowchart for report or website, Google Doc provides the flowchart program called drawing. You can login google account and find google doc. Create a new drawing file. Once finish, you export to a picture file.
7
8 ENGR 490: Software Tools for Engineering Lab Assignment 5: Flowchart and Pseudocode Source code and demo due by Friday, September 30 th, 2011 (by 4PM) 1. You should assign each person to take one question independently and discuss at the end. 2. For the hard question, you should work as a team, starting with design flowchart. Competition Rules and Award In this competition, Dr. Somsak will measure how effective and concise that you write the code. He will use the number of lines that you wrote on 2 problems, 1) salesman travel and 2) histrogram to measure. He will count the number of lines in the code, except { line, white space such as blank line. Then he will take log base 10 for the number of lines for each problem and sum. The team with lowest number is the winner. The award is 1 question off for the oral midterm. Each problem needs to start with flowchart before writing the code. Dr. Somsak will ask to see your flowchart each time you ask his help and demo time. Project 1: Roll roll the dice.. Crap Game (appleapple) Design a crap game. You will roll 2 dices. Use spacebar to simulate rolling the dice. You can do it by system( pause ); command. You will need to include stdlib.h. Use %c with the appropriate number to make sound. In this problem, you simply make 2 random numbers independently. I can repeat. Show the message of points, money. Start money with 200 dollars and you can bet each round that is less than the money that you have. If you bet more than the money that you have, the program should repeat ask you again. If you win, you get money back + same amount that you bet. You you lose, you will lose the bet. If the money reaches zero lose, if the point is over 999 win the game. Make the flowchart first.
9 Project 2: Histogram (appleappleapple) Competition problem Given that you have an array size 1000 containing salary of 1000 people. You can do this task with random data between 5000 and 25000. We want to find the histogram of data, basically the range of data and it will tell how many counts in there. The range is divided into 20 spacing equally, e.g. 5000-5999, 6000-6999 Count and assign each array elements into the histogram. The result should show o Bin 1: salary from??? to??? has xxxx persons o Bin 2: salary from??? to??? has yyyy persons Example: if I have 6 numbers 1 4 3 4 5 4. I want to break into 2 bins, it will be from bin#1 from 1 to 3 [1,3] and bin#2 from 3 to 6 (3,5]. The results will be Bin 1: from 1 to 3 has 2 populations. Bin 2: from 3 to 5 has 4 populations. Hint: if you are writing something that looks same same and tedious, try to make it shorter using C command such as for-loop. You need to find the pattern in it. Project 3: Statistics Part 2 (appleapple) Copy the code from previous lab to this week folder. We will modify the code to do more thing. 30 25 18 12 23 55 100 12 23 89 67 5 12 23 16 90 23 90 100 34 23 Print out on the screen to show the following statistics o Old stuffs: Min, Max.. Variance o Median (need to sort array first) o Mode (need to do histogram first, see project 2) Project 4: Salesman Travel (appleappleappleappleappleappleappleappleappleappleappleappleappleappleapple) 1. Competition problem 2. Imagine that you have the location of 10 cities on x-y plan. Each one has 2 numbers representing the location in x and y coordinates. To find the distance from one point to another point it will be d = ( x 1 x 2 ) 2 + ( y 1 y 2 ) 2.
10 3. The question is, if you start to travel from the city #0 and want to go to all cities and come back to city #0 again, what will be the good order in term of minimum total distance? To solve this, we can end up with so many possibilities and pick the one the have minimum distance. That will be about 10! = 3 million choices. 4. If the number of cities increase such as becomes 30, you will end up with trillion possibilities, take a year to get the answer. In this assignment, we will try to solve the minimum path for 10 cities. If you start at city#x and back at city#x again, e.g. city#5 and back to city#5 eventually. We will randomly generate sequence. o Start with random one sequence that start with 0, such as 0 5 3 1.. o Find the distance between cities and don t forget to add distance of the last one back to city#0 and use this one as the first result of distance and city. o Random the number again but it needs to start with 0 and the rest are 1 to 9 only never repeat. Find the distance + back to 0 of the last one, compare with the minimum that you have so far. o If it is lower, you will copy the data and print out sequence and result on screen. o Do it 3,000,000 random sequences, and the final answer will be somewhat minimum distance path that we try to search. o Print out the result of the minimum path including city sequences and total distance. o Use the following data. Use the graph to check if it makes sense to you. City 0 1 2 3 4 5 6 7 8 9 x 0.5-3 -2.5-3.2-0.1 1.9-0.3-0.2 2.4 1.5 y 3 0.2-0.4 2.5-2.7 1.2 3.2 2.4 0.8-1.5