my current solution:
struct SnakeDot{
int positionX, positionY;
SnakeDot * tail;
}I dont wana store it in a sized array, no...
| Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
|
NB
Netherlands12045 Posts
my current solution: struct SnakeDot{I dont wana store it in a sized array, no... | ||
|
sluggaslamoo
Australia4494 Posts
On October 16 2013 23:38 Rannasha wrote: Since question 1 was already answered... Show nested quote + On October 16 2013 22:30 heroyi wrote: two: simple program but I am running into a roadblock. No, this is not a hw assignment. Besides, I just help on the control flow. The program question is you are making a program to reserve seats on a small airplane of 10 seats. If user types 1, then that is a first class seat (seats 1-5) while 2 is economy(seats 6-10). If someone reserves a seat than it is taken and if all the seats are taken for a class then you ask whether they want an empty seat in the other class. I don't understand how one could check if the seat is taken and how would you check if all the seats are taken to prompt the user if they want the other class. Also, how would you check if all the seats are taken. I am stuck on how to initalize the array so that I can just check if seat is taken and if it isn't go reserve it and break from there. Keep in mind we have not gone over pointers yet. An example (of just the first half of loop as the other half would be the exact same copy other than it being 2 as for the other class): printf("type one or two: ") scanf("%d",&choice) if(choice==1) {??? if (array[i]!=1) { printf("first class seat number: %d", array[i]) array[i]=1 } } ... First, initialize and array and set all elements to 0 (we'll let 0 mean free and 1 mean reserved)
Now, when someone picks first class, we need to check whether any of seats 1 through 5 (so 0 to 4 in the array as array-indices are zero-based in C) are free. If we find a free seat, we'll set it to reserved. Similarly for second class. If a class is full, we want to check the other class for free seats. Easiest is to make a function:
This function returns the seat-index of the first free seat in the class or -1 if nothing is free. So now we use this function:
Ewwww where's the functional decomposition? ![]() The best way to start with any sort of program is think about the structure first, think about you need then think about how to write it. Your constants should be something like, MAX_SEATS=10 FIRST_CLASS_START=0 ECONOMY_CLASS_START=6 FIRST_CLASS=1 ECONOMY_CLASS=2 NO_SEAT_AVAILABLE=-1 Your functions should be something like, reserve_first_class() reserve_economy_class() find_available_seat(type) reserve_seat_by_index(index) reserve_seat_by_class(type) All functions return seat index or -1 if no seats available. Some of them will be helpers for the more abstracted functions so you Don't Repeat Yourself. Implement those functions and make sure they work on their own (test them). Then see if you can fill in the gaps and write the whole program, you'll probably find it a lot easier. Then bask in the glory of clean code | ||
|
Fiel
United States587 Posts
On October 17 2013 12:48 NB wrote: hmmm what is a good way to store a snake in a Snake game? my current solution: struct SnakeDot{I dont wana store it in a sized array, no... A linked list would be a poor implementation. This is because checking for collision detection would require O(n) lookup time. Ideally you want O(1) lookup time because it's much, much faster. There are several options to doing this that does not use a linked list or a two dimensional array. One example would be a bitboard. You designate an array of uint64_t. Then you use each bit in the 64-bit integer to designate where the snake currently is. This means you can have a board up to 16 wide and infinitely long. You could do infinitely wide and infinitely long but that would take a little more complicated (but not much more complicated) math.
Even better is that you can use the same implementation for both the fruit board and the snake board. This makes collision detection super easy. Do you have to use C? Why not use an std::vector? And why be against sized arrays? You could go for variable arrays - both those and linked lists require malloc calls. The only difference is that a variable array only requires one malloc call and a linked list requires a whole lot of them. | ||
|
NB
Netherlands12045 Posts
On October 17 2013 13:24 Fiel wrote: Show nested quote + On October 17 2013 12:48 NB wrote: hmmm what is a good way to store a snake in a Snake game? my current solution: struct SnakeDot{I dont wana store it in a sized array, no... A linked list would be a poor implementation. This is because checking for collision detection would require O(n) lookup time. Ideally you want O(1) lookup time because it's much, much faster. There are several options to doing this that does not use a linked list or a two dimensional array. One example would be a bitboard. You designate an array of uint64_t. Then you use each bit in the 64-bit integer to designate where the snake currently is. This means you can have a board up to 16 wide and infinitely long. You could do infinitely wide and infinitely long but that would take a little more complicated (but not much more complicated) math.
Even better is that you can use the same implementation for both the fruit board and the snake board. This makes collision detection super easy. Do you have to use C? Why not use an std::vector? And why be against sized arrays? You could go for variable arrays - both those and linked lists require malloc calls. The only difference is that a variable array only requires one malloc call and a linked list requires a whole lot of them. I was messing around with a bunch of recursions so I thought my way was convenient but i guess yours is better. Still learning stuff so ima have to look up things about vector | ||
|
Nesserev
Belgium2760 Posts
| ||
|
Tobberoth
Sweden6375 Posts
As for collision detection (with walls, the snake and apples), I would have a World class which keeps track of such things. The world would keep track of the position of the snake by applying the Snakes 2d array to its own 2d array of the whole playing field. You could then, if you wanted, simply implement Drawable on the world which would make your main loop nicer. Create world, affect it, draw it. | ||
|
Arevall
Sweden1133 Posts
I'm loading some large datafiles with the LOAD DATA command. All works fine except for my last column. It should be an integer, if it's not empty. Therefore I use an nullif statement. expected_days_logging= nullif(@expected_days_logging ,'') It seems the field isn't '' but '(newline)'. In the value viewer it is 0d. How do I modify my nullif-statement for this? | ||
|
Tobberoth
Sweden6375 Posts
On October 17 2013 19:14 Arevall wrote: Need some SQL-help. I'm loading some large datafiles with the LOAD DATA command. All works fine except for my last column. It should be an integer, if it's not empty. Therefore I use an nullif statement. expected_days_logging= nullif(@expected_days_logging ,'') It seems the field isn't '' but '(newline)'. In the value viewer it is 0d. How do I modify my nullif-statement for this? 0d is undefined. I would assume it wants an integer and you're giving it '', which makes no sense, you should give it NULL or 0. '' is an empty varchar, not an integer. | ||
|
Arevall
Sweden1133 Posts
On October 17 2013 19:20 Tobberoth wrote: Show nested quote + On October 17 2013 19:14 Arevall wrote: Need some SQL-help. I'm loading some large datafiles with the LOAD DATA command. All works fine except for my last column. It should be an integer, if it's not empty. Therefore I use an nullif statement. expected_days_logging= nullif(@expected_days_logging ,'') It seems the field isn't '' but '(newline)'. In the value viewer it is 0d. How do I modify my nullif-statement for this? 0d is undefined. I would assume it wants an integer and you're giving it '', which makes no sense, you should give it NULL or 0. '' is an empty varchar, not an integer. The nullif sets an empty field to NULL, so that I don't try to give it '' but an integer or NULL. But the problem was that the end of a row in my .csv file is \r, so the field @expected_days_logging wasn't empty. 0d seems to be \r and 0a \n, so changing my nullif statement to nullif(@expected_days_logging ,'\r') did the trick. Thanks for the input, now I can finally go eat something! Then I can correct the 1551 faulty entries in the data I received, yay -__- | ||
|
Tobberoth
Sweden6375 Posts
On October 17 2013 19:25 Arevall wrote: Show nested quote + On October 17 2013 19:20 Tobberoth wrote: On October 17 2013 19:14 Arevall wrote: Need some SQL-help. I'm loading some large datafiles with the LOAD DATA command. All works fine except for my last column. It should be an integer, if it's not empty. Therefore I use an nullif statement. expected_days_logging= nullif(@expected_days_logging ,'') It seems the field isn't '' but '(newline)'. In the value viewer it is 0d. How do I modify my nullif-statement for this? 0d is undefined. I would assume it wants an integer and you're giving it '', which makes no sense, you should give it NULL or 0. '' is an empty varchar, not an integer. The nullif sets an empty field to NULL, so that I don't try to give it '' but an integer or NULL. But the problem was that the end of a row in my .csv file is \r, so the field @expected_days_logging wasn't empty. 0d seems to be \r and 0a \n, so changing my nullif statement to nullif(@expected_days_logging ,'\r') did the trick. Ah, I see, I misunderstood your problem. If you want a "cleaner" or rather safer way to do that, I think nullif(TRIM(@expected_days_logging) ,'') would work, and should work regardless of whether it's \r, \r\n etc. | ||
|
AnotherRandom
Canada81 Posts
| ||
|
spinesheath
Germany8679 Posts
Visual Basic isn't a program, it's a programming language. It supposedly has been around since 2002. | ||
|
Tobberoth
Sweden6375 Posts
On October 17 2013 22:59 AnotherRandom wrote: This is an embarrassingly simple question but I've looked online and I don't understand all the options. When I was in grade 9 we used Visual Basic. I've come up with a program I want to make and I think the simplest way for me to do it is to simply do it with Visual Basic (I have very basic experience with C++, C#, and Python). But I can't find "Visual Basic". Is it really a 1998 program? MVisual Studios has a bunch of different versions now but I'm not sure if they have VB. I remember I used MVS 2005 for C++ but I don't recall if that had VB packaged with it. Visual Basic is not used anymore. You either use VB.NET (get Visual Studio) or VBA, which you use as a script inside programs like Excel. If you have experience with C#, I would recommend it over VB.NET. Both work with .NET and compile to CIL, but VB.NET is such an ugly language. | ||
|
tofucake
Hyrule19167 Posts
| ||
|
TimKim0713
Korea (South)221 Posts
I am also suppose to make this in order, (like lowest beepers to highest) with visually showing, but I am like stuck T_T (We started doing ArrayLists, and using it to do SearchAndSort: Find max/min. Sort them in order.) + Show Spoiler + int left = 0; int right =1; for(int x=0; x<input; x++){ //input = the number of robots in the array (user input) if(botList.get(left).compareTo(botList.get(left+right))>=0 && left+right<=input*2-2){//compareTo compares # //of beepers right++; } if(botList.get(right).compareTo(botList.get(left+right))>=0 && left+right<=input*2-2){ left++; } } botList.get(left).dropBeepers(); //dropBeepers() makes robot all of its beepers in a line Thanks! | ||
|
Nesserev
Belgium2760 Posts
| ||
|
Zocat
Germany2229 Posts
On October 18 2013 10:48 TimKim0713 wrote: Hey I'm kinda new to coding, but I made this part, (Karel The Robot), but it keeps getting me out of bounds exception. I heard about it and I think its when the index goes over, but I can't fix it somehow... could you help? or does this code just not work? I am also suppose to make this in order, (like lowest beepers to highest) with visually showing, but I am like stuck T_T (We started doing ArrayLists, and using it to do SearchAndSort: Find max/min. Sort them in order.) + Show Spoiler + int left = 0; int right =1; for(int x=0; x<input; x++){ //input = the number of robots in the array (user input) if(botList.get(left).compareTo(botList.get(left+right))>=0 && left+right<=input*2-2){//compareTo compares # //of beepers right++; } if(botList.get(right).compareTo(botList.get(left+right))>=0 && left+right<=input*2-2){ left++; } } botList.get(left).dropBeepers(); //dropBeepers() makes robot all of its beepers in a line Thanks! I'm assuming botList is an ArrayList (since you mentioned those). Overall something like Nesserev mentioned is good practice. Write output statements "before 1st if", "before 2nd if", ... to pinpoint the exact location where your error occurs. Another method is to "mentally" go through your code. For extreme cases (high/low values at the border of allowed stuff). What happens if your botList has 0 elements? "x < input" is true, so it ignores the loop & jumps to the end. Then you say botList.get(0).dropBeepers and try to access the 1st element even though no elements exist. Here you can get an out of bound error. Now what happens if your botList has exactly 1 element? The program will enter the for loop (since x = 0 is < 1) with left = 0 and right = 1. So left + right = 1. So now you use botList.get(0) and botList.get(1). But there's only one element! So the get(1) is out of bounds, since it tries to access the 2nd element in an array. You correctly already thought about that situation, because you check "left+right<=input*2-2". But sadly it's the second part of an AND evaluation and (depending on compiler, language, ...) the left part of an AND evaluation is checked first. So your program crashes before you reach the check which should prevent the crash. Also keep in mind your program basically has the structure: + Show Spoiler +
There might be other problems with your code, since I have no clue what it's intended behavior is. | ||
|
NB
Netherlands12045 Posts
| ||
|
HumpingHydra
Canada97 Posts
| ||
|
ObviousOne
United States3704 Posts
On October 18 2013 10:48 TimKim0713 wrote: Hey I'm kinda new to coding, but I made this part, (Karel The Robot), but it keeps getting me out of bounds exception. I heard about it and I think its when the index goes over, but I can't fix it somehow... could you help? or does this code just not work? I am also suppose to make this in order, (like lowest beepers to highest) with visually showing, but I am like stuck T_T (We started doing ArrayLists, and using it to do SearchAndSort: Find max/min. Sort them in order.) + Show Spoiler + int left = 0; int right =1; for(int x=0; x<input; x++){ //input = the number of robots in the array (user input) if(botList.get(left).compareTo(botList.get(left+right))>=0 && left+right<=input*2-2){//compareTo compares # //of beepers right++; } if(botList.get(right).compareTo(botList.get(left+right))>=0 && left+right<=input*2-2){ left++; } } botList.get(left).dropBeepers(); //dropBeepers() makes robot all of its beepers in a line Thanks! x<(input-1) The last element is sorted by default by the time you get there. | ||
| ||
StarCraft 2 StarCraft: Brood War League of Legends Counter-Strike Other Games Organizations
StarCraft 2 • Hupsaiya StarCraft: Brood War• AfreecaTV YouTube • intothetv • Kozan • IndyKCrew • LaughNgamezSOOP • Migwel • sooper7s Other Games |
|
Wardi Open
StarCraft2.fi
Replay Cast
The PondCast
OSC
Demi vs Mixu
Nicoract vs TBD
Babymarine vs MindelVK
ForJumy vs TBD
Shameless vs Percival
Replay Cast
Korean StarCraft League
CranKy Ducklings
SC Evo League
BSL 21
Sziky vs OyAji
Gypsy vs eOnzErG
[ Show More ] OSC
Solar vs Creator
ByuN vs Gerald
Percival vs Babymarine
Moja vs Krystianer
EnDerr vs ForJumy
sebesdes vs Nicoract
Sparkling Tuna Cup
OSC
BSL 21
Bonyth vs StRyKeR
Tarson vs Dandy
Replay Cast
Wardi Open
StarCraft2.fi
Replay Cast
StarCraft2.fi
PiGosaur Monday
|
|
|