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:

Wang Liang said...

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

Douglas said...

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

Johnathan Teo DCPE 2A/05 said...

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

Afiq DCPE 2A05 said...

http://snipt.net/embed/e159fd1ca49c1afb07d8f85122f87810

Edwin said...

http://snipt.net/embed/a86bbafeb7206f0726f607408fefd46e

Johnathan Teo DCPE 2A/05 said...

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

Edwin - calculate sum of even numbers said...

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

Wang Liang said...

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

Afiq DCPE 2A05 said...

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

Douglas said...

http://snipt.net/embed/e3e621347d131dcc07aa0a7248d218df

Soon Beng said...

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

alvin then DCPE 2b03 said...

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

yao de DCPE 2B03 said...

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

XuZhou said...

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

Yi Wei said...

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

Qi Hao (DCPE/2A/03) said...

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

Felix (DCPE 2a03) said...

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

Qi Hao(DCPE/2B/03) said...

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

Nash said...

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

Name : Ganesh Class : 2B03

heng gnee dcpe/2b03 said...

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/

li xiaofei DCPE 2A03 said...

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

Sathis 2B03 said...

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

Yang jiashu/2B03 said...

http://snipt.net/yangershiwo

alvin then DCPE 2b03 said...

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

yao de DCPE 2B03 said...

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

xuzhou said...

http://snipt.net/bigshow/week2

Sathis 2B03 said...

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

li xiaofei DCPE 2B03 said...

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

Yang jiashu/2B03 said...

http://snipt.net/yangershiwo

Felix (DCPE 2b03) said...

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

tintundcpe2b03 said...

http://snipt.net/tintundcpe2b03

Felix (2b03) said...

http://snipt.net/tintundcpe2b03

cinema booking system

sjteo said...

@felix, nice work done.

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

Felix (2b03) said...

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.

yao de DCPE 2B03 said...

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

Muahammad Ramadhan DCPE 2B/03 said...

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

heng gnee dcpe/2b03 said...

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

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

alvin then dcpe/ft/2b/03 said...

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

cannot manage to detect duplicate seat numbers

sjteo said...

@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"

sjteo said...

@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

HY said...

Example of 2D array:

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

Li Xiaofei DCPE 2B03 said...

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?

HY said...

@Li Xiaofei
You declared your array as zero.

HY said...

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

xiaofei said...

@HY
thanks ya now it works

sjteo said...

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

alvinthen dcpe/ft/2b/03 said...

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

HY said...

@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.