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.

48 comments:

  1. http://snipt.net/embed/07d477ae9dde683807a810ab2fabc913
    Name: Wang Liang Class: 2B05

    ReplyDelete
  2. http://snipt.net/embed/8112279aa27930e77101083b09b8ce27

    ReplyDelete
  3. Johnathan Teo DCPE 2A/05October 25, 2010 at 11:35 AM

    http://snipt.net/embed/40dca785e3ae696688bfbf97cb531af5

    ReplyDelete
  4. http://snipt.net/embed/e159fd1ca49c1afb07d8f85122f87810

    ReplyDelete
  5. http://snipt.net/embed/a86bbafeb7206f0726f607408fefd46e

    ReplyDelete
  6. Johnathan Teo DCPE 2A/05October 26, 2010 at 8:43 AM

    http://snipt.net/sgxbuster/wk2-day-2?key=5eecf85df18c3eee67f67cf8b64c026a

    ReplyDelete
  7. Edwin - calculate sum of even numbersOctober 26, 2010 at 8:52 AM

    http://snipt.net/edwinraja/sum-of-even-numbers?key=a86bbafeb7206f0726f607408fefd46e

    ReplyDelete
  8. http://snipt.net/J0k3r/sum-of-even-number-09?key=31018839872a96ea85dda0981e72c489

    ReplyDelete
  9. http://snipt.net/puteraafiq/array-1?key=ba60528fdea2ecd74e973e98ff95be7d

    ReplyDelete
  10. http://snipt.net/embed/e3e621347d131dcc07aa0a7248d218df

    ReplyDelete
  11. http://snipt.net/Soonbengisgougou/sum-of-even-numbers-using-for-loop

    ReplyDelete
  12. alvin then DCPE 2b03October 26, 2010 at 12:13 PM

    http://snipt.net/alvinthen/pointer-usage?key=cc394f1347eade9e648d60c01b081909

    ReplyDelete
  13. http://snipt.net/asdsieg/pointer-9?key=c4685b9cbbef0fcfe46e03066a25af4f

    ReplyDelete
  14. http://snipt.net/bigshow/1st-code/

    ReplyDelete
  15. http://snipt.net/jellybanana/pointer-testing/

    ReplyDelete
  16. http://snipt.net/sa198/by-qi-hao-dcpe2a03?key=45a284d3f31584ea9ea0c9da0d1b6c1e

    ReplyDelete
  17. http://snipt.net/UJuzGtJacked/done-by-felix/?key=a35bb84adfcebabe4c9785f9dc5b277a

    ReplyDelete
  18. I wrote the wrong class, my class is DCPE/2B/03

    ReplyDelete
  19. http://snipt.net/ganesh92/cube-volume-and-area/?key=ae7918331ae181ccb2c8296fe40a235b

    Name : Ganesh Class : 2B03

    ReplyDelete
  20. http://snipt.net/hengnee/class-assignment-i-realised-my-firewall-that-i-downloaded-blocked-my-program-so-i-had-to-uninstall-it-anyways-it-works-now/

    ReplyDelete
  21. li xiaofei DCPE 2A03October 28, 2010 at 8:18 AM

    http://snipt.net/xiaofei/c-2/

    ReplyDelete
  22. http://snipt.net/Stish/area-and-volume?key=c07a202057d4ae4a0a870d44648bcbdf

    ReplyDelete
  23. http://snipt.net/yangershiwo

    ReplyDelete
  24. alvin then DCPE 2b03October 28, 2010 at 9:19 AM

    http://snipt.net/alvinthen/sum-of-even-number-using-array?key=428cadf68b255f327448b74f3143d73c

    ReplyDelete
  25. http://snipt.net/asdsieg/array-2?key=27306d549c408a8a8a866a269ebb292c

    ReplyDelete
  26. http://snipt.net/bigshow/week2

    ReplyDelete
  27. http://snipt.net/Stish/sum-of-even-nums?key=77a9e5fc20f8607fdb20dd7d4ce94b82

    ReplyDelete
  28. li xiaofei DCPE 2B03October 28, 2010 at 9:30 AM

    http://snipt.net/xiaofei/add-the-no-in-array/

    ReplyDelete
  29. http://snipt.net/yangershiwo

    ReplyDelete
  30. http://snipt.net/UJuzGtJacked/array-3?key=05bd295a11f64e9e65e12bc05aa40a52

    ReplyDelete
  31. http://snipt.net/tintundcpe2b03

    ReplyDelete
  32. http://snipt.net/tintundcpe2b03

    cinema booking system

    ReplyDelete
  33. @felix, nice work done.

    @ the rest of the guys that written code, keep the code coming in!~

    ReplyDelete
  34. http://snipt.net/UJuzGtJacked/improved-cinema-booking/?key=e6e8d35b8e46dc9a92bcabc3a9d44db1

    Sorry sir. I think i accidentally copied the wrong link. This is then the right one.

    ReplyDelete
  35. http://snipt.net/asdsieg/cinema-1

    ReplyDelete
  36. Muahammad Ramadhan DCPE 2B/03November 1, 2010 at 9:26 PM

    http://snipt.net/Wizardgenesis/row-and-column-example/

    ReplyDelete
  37. http://snipt.net/hengnee/not-successful-version

    sorry, but it seems like I cannot replace the zero with a one...

    ReplyDelete
  38. alvin then dcpe/ft/2b/03November 1, 2010 at 10:56 PM

    http://snipt.net/alvinthen/fail-version?key=330cb2ea513ecad86fc9f9ed7faebbd6

    cannot manage to detect duplicate seat numbers

    ReplyDelete
  39. @felix, u also posted the wrong link on the post for common mistakes
    @heng gnee, which line is the one that is giving the problem? you need to identify by stepping thru ur program.
    @alvin, checking of the duplicate booking is as simple as comparing the content of the cell in the array.

    eg
    temp = arr[i][j]
    if (temp == 1) //assume 1 is booked
    cout<<"msg"

    ReplyDelete
  40. @all,,

    some of you declarare 3 arrays instead of a 2D array....

    using of a 2D array let us modify the req easier, eg cinema expanded to 100x100 seats instead of 10x10 seats.

    if u use 3 arrays as 3 rows, u gotta declare 100 arrays ==> tired larrrr

    ReplyDelete
  41. Example of 2D array:

    int ar1[2][2];
    ar1[1][2] = 1;
    cout << ar1[1][2];

    ReplyDelete
  42. Li Xiaofei DCPE 2B03November 4, 2010 at 10:19 PM

    http://snipt.net/xiaofei/cinema-booking-3


    why after i book the place ,the output of seat change the whole column not that particular seat?

    ReplyDelete
  43. @Li Xiaofei
    You declared your array as zero.

    ReplyDelete
  44. @Li Xiaofei
    Ah sorry, I realized that my answer wasn't really clear. You declared the size of your array as [0][0].

    ReplyDelete
  45. @HY
    thanks ya now it works

    ReplyDelete
  46. @xiao fei, good 1. keep the code coming in

    ReplyDelete
  47. alvinthen dcpe/ft/2b/03November 10, 2010 at 4:17 PM

    zomg after so long i finally understand what you are talking about mr teo...now my cinema is 100% working
    http://snipt.net/alvinthen/fail-version?key=330cb2ea513ecad86fc9f9ed7faebbd6

    ReplyDelete
  48. @Alvinthen

    You could have just used a 2D array. If you had used a 2D array, the seat checking part would only be 2 lines long.

    ReplyDelete