The Big Programming Thread - Page 156
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. | ||
Dephy
Lithuania163 Posts
| ||
EscPlan9
United States2777 Posts
The pertinent section is below: sub scanDir { I'm trying to think of ways to make sure it runs through every subdirectory without any repetitions. The problem right now is once it finds a directory, it jumps into it and scans all the files and directories there, and it keeps doing that until no more directories are found. This means that in a directory with multiple subdirectories, only the first subdirectory is traversed through. It never returns through the other subdirectories. From google searches I always see that people recommend using modules for this. My teacher wants us to tackle this problem without using modules though. One possible method I can think of is creating a hash structure of Absolute-path-of-dir : Boolean (traversed or not). I would create new keys when new directories are found and update the values for the directories that I have traversed until all the keys -> values evaluate to True. While I think this might work, it'll be tricky and I feel like there must be a much simpler method I am missing. Any ideas? | ||
supereddie
Netherlands151 Posts
| ||
EscPlan9
United States2777 Posts
I'm not sure what you mean by $_ is a global here. If I understand Perl's scope correctly, $_ scope is only within the for loop and gets assigned the value of the current element iterated through within the @contents array (listing of the directory). | ||
EscPlan9
United States2777 Posts
The changed portion:
And if curious on the other subroutines and how it all comes together, full script in spoiler + Show Spoiler +
| ||
supereddie
Netherlands151 Posts
I feel like it would be easier to maintain/expand later on if needed. | ||
EscPlan9
United States2777 Posts
| ||
SiPa
Germany129 Posts
I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing. | ||
Deleted User 101379
4849 Posts
On August 13 2012 20:45 SiPa wrote: Quick Question: I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing. There is no equivalent. You can make your own implementation of a variable size array, though depending on the actual use, it might actually be easier to go with a linked list instead. | ||
heishe
Germany2284 Posts
On August 13 2012 20:45 SiPa wrote: Quick Question: I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing. There's not, but things without inheritance are convertible to C in a pretty straightforward manner (and inheritance is easy, too, just a bit bothersome), with some handicaps. But templates just won't work, you flat out won't be able to get that in C. So if you want multiple types to be handled by that vector, you either have to implement it multiple times each time for one certain type, or you have to store void* in the pointer and cast ("with certainty") around to get the types into and out of that you want. The rest works like this: If you have some class with some method, e.g.
it can be done by creating a struct vector and a free method size(vector* vec);
Similarly, methods with parameters would transform from return_type class::method(a,b,c) to return_type method(class*,a,b,c) (this is actually exactly like it looks in assembly code, without optimizations). Constructors can be implemented by simply making a free method Constructor[yourstructname](struct *str) or something. Of course, usage changes too, from classobject.method(params); to method(&classobject,params); Converting the vector class like this should be trivial, since you can look up its source code of whatever compiler/IDE you're using. However, you can not recreate other built-in features to C++ like RAII, unfortunately. | ||
waxypants
United States479 Posts
On August 13 2012 20:45 SiPa wrote: Quick Question: I have a few lines of code, written in C++. I have to write code, that does the same stuff in C. the C++-code contains vectors (std::vector<>) It uses the push_back-function and clear-function of the vector. Is there an equivalent to the C++-vector in C? (Array with variable size?) Google pretty much tells me to make my own thing. Not in standard C. I like to use glib for generic data structures among other things. http://developer.gnome.org/glib/stable/glib-data-types.html http://developer.gnome.org/glib/stable/ | ||
Olsson
Sweden931 Posts
| ||
bo1b
Australia12814 Posts
| ||
IreScath
Canada521 Posts
| ||
![]()
tofucake
Hyrule18982 Posts
...yeah | ||
IreScath
Canada521 Posts
On August 16 2012 05:40 tofucake wrote: I'm sure this doesn't really help you at all but personally I hate handbrake. ...yeah open scource + opencl = yay. but I think I go it now. ![]() | ||
SiPa
Germany129 Posts
Now some questions regarding GLib: I get tons of "expression must be an lvalue or a function designator" and "a nonstatic member reference must be relative to a specific object" errors. I get, that this isnt necessarily connected to GLib, so i will just give you the code: struct trajectory{
iPos and iDir are pointers to GArrays. trajectory calcTrajectory( _Fragment iFragment, GArray *iPos, GArray *iDir ){} Errors are at g_array_append_val, velocity2, position2 I hope some1 could provide help, thx in advance! Edit: I also would love to know how to get the size of a GArray, meaning how many Elements it contains. | ||
waxypants
United States479 Posts
1) position1 = g_array_new(FALSE, FALSE, sizeof (double)); In C, you can't have member initialization inside the struct definition. 2) trajectory.velocity2 In C, you can't have static struct members. | ||
waxypants
United States479 Posts
You can build it yourself (enormous pain), or get it from here: http://www.gtk.org/download/win32.php Not the newest version, but fairly recent. You may be able to find a newer version elsewhere but this one has worked well enough for me. I'm assuming you are using Visual Studio. In that case you will need to do the following: To build your program: 1) Download the Glib "Dev". 2) Extract it to something like C:\glib-dev_2.28.8-1_win32 (or whatever directory you want) 3) In your project properties, go to Configuration Properties->C/C++->General and add "C:\glib-dev_2.28.8-1_win32\lib\glib-2.0\include" and "C:\glib-dev_2.28.8-1_win32\include\glib-2.0" to the list of Additional Include Directories. 4) In your project properties, go to Configuration Properties->Linker->General and add "C:\glib-dev_2.28.8-1_win32\lib" to your list of Additional Library Directories. 5) In your project properties, go to Configuration Properties->Linker->Input and add "glib-2.0.lib" to your list of Additional Dependencies. For steps 3 and 5 you may need to add other directories and/or other lib files to the list, but these things have been enough for what I have done. You will know what you need to add whenever it complains that it can't find "blahblah.h" or "blahblah.lib". To run your program: 1) Download the Glib "Run-time" 2) Extract whatever files you need from the bin directory into the same directory as your program's exe file. You can add one at a time until your program is able to run, or you can just copy all of them if you don't care about having extra unnecessary files. In theory, I think could also put them in C:\Windows\System32 if you want, and then you won't have to copy them to all of your projects, but if somebody else wants to run your program they will need to download the necessary dll's. | ||
waxypants
United States479 Posts
#define g_array_append_val(a,v) g_array_append_vals (a, &(v), 1) The &(v) is what is throwing the error. Anyway it sucks, but you can do something like: double speed; Or even: double speeds[3]; | ||
| ||