Always two there are, a master and an apprentice. -yoda
Monday, October 25, 2010
[DSA] pointers stuff
Well done to those that have completed the drill question (well, at least you did the “EASY” one) last week. To those that did my STUN question, KUDOS!!!! By doing this exercise, you have examine the buggy code and correctly identified the syntax errors, logic erros and some went further of discovering the differences of using “cin” and “getline”. The application and synthesis skills, such as isolating the buggy portion of the code, using the debugger, and reading error message from the IDE are particularly important especially in programming. These are the skills that I hope you can exhibit or demonstrate at the future assessments to come.
For those that have yet to attempt, please give it a try. And for those have done, try out the “HERO” question. Everybody loves to be a hero and get "worshiped" (I assumed that is).
Pointer is used for example when sorting a data structure. It is “expensive” to move the data physically, hence, changing the value of the pointer that points to the data is considered “cheaper”.
Below is a chunk of code that demonstrates pointers, the differences when pointers are applied to “char array” and “int array”. YOU are MOST WELCOMED to add more examples of using a pointer in c++.
Pointers come a long history in C and C++, some hate it so much but some really love it. Take it like a durian. Pointers enable the manipulation of the memory directly. PLEASE be EXTRA meticulous when it comes to programming pointers. Commented code really helps a lot for others to look, learn, debug another programmer’s code.
Now Comes the drill question for pointer. Write a program that calculates the volume of a cube (A x A x A), which need to satisfy some "requirement" identified from the "problem analysis" (which is done by me for you)
1. Read in 2 inputs from the user
2. Code a function that takes in this 2 parameter passed by reference and the result is stored in another variable, which is also accessible by the pointer ptrArea. Print out the area for the user.
3. Code another function, which takes in the ptrArea and calculate the volume of the cube.
Now come to the drill question array. Arrays are “containers” that can be used to store data in a structural manner. There are some disadvantages when it comes to using array. First, memory need to be allocated first (static). What happends when there are many copies of the same programme are running? Since memory is allocated, and used up but there are more data to come, array do not have the flexibility to increase like the vector. What are the other cons of using array compare to any other data structure?
Consider using “arrays”. Code a simple 100 seat cinema booking system that will do the following
1. Given (x,y) coordinate set the seat to be occupied.
2. Suggest a scheme/algorithm that detects duplicate seat entry.