Jump to content

MikeD

Member
  • Posts

    216
  • Joined

  • Last visited

Everything posted by MikeD

  1. I found this, but never tried it: https://stackoverflow.com/questions/5171957/access-file-in-jar-file
  2. To loop the char you could do something like char currChar = 'A';at the beginning and then cout << "| " << currChar++ << " ";or cout << "| " << currChar + i << " ";
  3. During my OOP class in uni (3 years ago) we learned a subset of those design patterns and had to apply them to a project as we saw fit. And by that I mean 2 things: 1) Those patterns are as relevant now as they were in '94; 2) A design pattern is supposed to be a generic solution to a type of problems. Nobody is going to be any more specific about when to use a given pattern/set of patterns. This was what I was going to say before you said this was for your master's thesis. Now I will add that design patterns are dangerous if you try to use them all at once, but the rest still stands!
  4. The time function expects a pointer to a time_t struct where it would store the returned value. I prefer NULL to zero because, even though 0 is null, it's clearer.
  5. #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; int main() { int yourGuess, randomNumber; string playAgain; srand(time(NULL)); cout << "Let's play a game. I will come up with a number and you will have to guess it." << endl; do { randomNumber = (rand() % 20) + 1; do { cout << "Guess a number: "; cin >> yourGuess; if(yourGuess > randomNumber) cout << "Nope! You were too high." << endl; else if(yourGuess < randomNumber) cout << "Nope! You were too low." << endl; } while(yourGuess != randomNumber); cout << "Winner Winner!!! You got my number!" << endl; cout << "Play again?[y/n]: "; cin >> playAgain; } while(playAgain == "y"); return 0; }
  6. You don't need to convert every time. int main() { int **screen = ... ... f1(screen); ...}void f1(int **arg) { arg[3][25] = 10;} If this is what you mean.
  7. Yeah, I was being stupid, sorry! And I was about to post some code that contradicted what I had just said, but I backed out!
  8. Sorry for the lack of follow-up, I had to leave. I am glad you sorted your problem out. Shouldn't this be *(screen + 12 * height + 23) = 1 ? After all, you are allocating 'height' rows of 'base' columns (screen[height][row]). *(screen + 12*height+23) would be the 23rd column of the 12th row and *(screen + 12 * base + 23) would be the 23rd column of the 24th row (base = 80 = 2*40 = 2*height => 12*2*height)
  9. 1st of all, use the code tags in the editor ('<>' symbol) to be easier to read the code. 2nd, I have run into that issue a few times myself and the answer usually lies in the fact that there are some funny businesses in the code (unless you are using multiple threads, in which case it could be some problem with memory consistency since printfs and other IO operations usually impose memory barriers (and cache coherence) if I am not mistaken). In your case, however, it would probably do you good to correct the mistakes that you have made and see if the "printf effect" resolves by itself (gcc in linux spits out compilation warnings and even with the printf the program just crashes in runtime). The main problem lies in the line int *screen = (int*)malloc((height)*(base)); which affects pretty much everything that follows. You are allocating an array of size height*base but you forgot to multiply this by the sizeof(int). Right now it is allocating height*base bytes instead of height*base integers. Obviously, since you are assuming that every position is an int, pointer arithmetic will soon break. Also, when you free screen at the end of main, you forgot that screen is no longer a pointer to the start of the array, so you are trying to deallocate memory that was not allocated in the first place. You should always keep a pointer to the beginning of any allocated memory.
  10. You didn't interpret step 2 correctly. It's asking you to return the sum of calling the 3 functions and you are returning the sum of the 2 arguments. The problem that is being reported, however, seems to be that, in the function hotel_cost, you are always returning 140 instead of 140 * the argument (nights).
  11. Sorry, I meant the tutorials that are on oracle's pages. http://docs.oracle.com/javase/tutorial/java/
  12. What's wrong with the OFFICIAL Oracle Java documentation?
  13. The problem does not seem to be the driver 'pcspkr' but instead nouveau's 'init table command not found'. One of the first google answers for that search reveals this: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1129511 (but be careful about changing the bios boot mode since windows might not boot afterwards, just try what the title says) Maybe you could give it a try and, if you can boot, fix the issues more permanently (look into nvidia optimus in linux and bumblebee).
  14. I don't like those things either! But I do drink more than my fair share of coke! That was something I purposefully kept out of my previous comment because it's more high-level. The core of programming is writing code but that code has to originate from the need to do something and has to be structured. Before sitting down and writing code endlessly throughout the night you need to develop the algorithms that you need and you need to find the right architecture for your system. If you just start writing right away you will end with what we call 'spaghetti code' and eventually not even you will understand what you're doing. As for the problem of writing 1000 words on the subject, well... If you knew half the things someone knows after 5 (or even just 3) years of Computer Engineering studies you wouldn't have a problem! You can talk about what a program is, the different kinds of languages and paradigms and then their applications: operating systems and their purpose, software architecture, games and what you need to make one, human-computer interaction (graphical user interfaces), the Web (from small websites with just HTML and CSS to web based applications and web services such as those provided by google), distributed systems, virtual machines, artificial intelligence, ... And you can also talk about how to develop software: using version control systems, employing a development process (waterfall vs agile (xp or scrum)), best practices, the journey from requirements elicitation to maintenance, ... *all the memories*
  15. A programmer is a machine that transforms coffee into code. No, but seriously... it can be any other type of energetic beverage! (EDIT: Damn, @Ciccioo you were faster!) On a more technical level a programmer writes code to create applications. Apps can range, as others have already said, from a basic HTML page (this would be better described as scripting) to a distributed computing platform and from command line applications to apps with graphical user interfaces or even games. At the most basic level it's just writing lines of code with logic (conditionals, loops, ...) and statements that perform calculations (arithmetic) or that change a system's state.
  16. I think doing it like @Naty722 suggested (nums = (int*) malloc(sizeof(int) * 10) could still be considered using arrays. Usually the other way to do it is with lists. A struct for each number with a pointer to the next struct. If the question says to use files then @Ciccioo 's answer looks fine. However, saving inputs in a file to treat them later is not very usual, considering the overheads.
  17. Not in the stone age, but learning. And it was not mandatory, just encouraged. I personally think that not using an IDE while learning was a good thing. After that class I had others with Java and we used Eclipse. While it is a great tool and saves a lot of time it also attempts to fix mistakes for you and if you don't know what you're doing it may make things more confusing to someone. We did not have to implement anything in the project with all the diagrams. Like I said, we were the architecture company and our final product were the diagrams (and most of them were sequence diagrams, use cases, SysML and BPMN). Yes, the structure of a system will most definitely change over time (especially with agile development methods), so the UML diagrams should be kept up to date, not ignored. I agree with what you said about automatically generated code, if you know what you are doing. But for a beginner it might not be the best thing. One could trust the generator too much and not double check if everything is correct. And, if you're a teacher, you'll probably be a little suspicious and pay more attention to mistakes if you're grading a project with artifacts (e.g. comments) from an automatic generator! The level of the UML diagram will depend on the stakeholder it was intended to. The systems administrator will probably only need a deployment diagram, the project manager a UML with only the classes/packages and relationships and the actual developer of a certain module will probably need a more detailed diagram of that specific module. A single diagram will not give information about the whole thing and will have many holes, but the set of all diagrams should be mostly complete. Of course it need not have every single implementation detail or you'll sacrifice readability.
  18. The more formal it is, the less ambiguity there is. If you had to hand your project to someone else it'd be better than if it were the kind of sketches we do on a piece of paper. Every time someone walks into a room in uni with some diagram on a whiteboard they have a different interpretation of what it represents. I have taken part in one too many hour+-long discussions about the architecture of some project because we didn't understand each others drawings! (Mainly because someone else would always walk in and say we were all wrong, but still!) But of course UML is not intended as a tool for school projects. Apart from OOP class we only used UML and such in a Modelling class where we had to respond to a "request for proposals for the specification of requirements and the analysis and design of a system of systems, conceptually made of one logical system and one physical system.". We were the company that was going to design the system and our results would be handed off to two other companies that would do the actual systems. We had to do requirements elicitation from the document that we were given and use different kinds of UML diagrams (sequence, class, deployment, etc.) and even BPMN and SysML to model the systems. In this context UML makes sense, but for school projects, sketches on paper were just fine (if everyone was on the same page about their meaning!). By the way, i assume that by "only 6-8 'standards' proposed and done differently" you were talking about the different UML diagrams? Because UML is a well-defined standard (ISO/IEC 19505). The thing is, you shouldn't need to generate UML diagrams from code because they are supposed to be (mostly) done before writing any code at all. In fact, our OOP teacher did not want to see any automatically generated diagrams or code (and he actually encouraged us to use a text based editor for coding with java as opposed to an IDE for that class, so that we wouldn't get any bad habits!). In the example I used above - the Modelling class - our "company" was only responsible for designing the architecture and the other 2 companies would have to actually build the systems based on our diagrams. In total we had 48 diagrams and 69 tables. Of course in that case we were talking about a public contract, which means there were stricter rules, the diagrams had to be near perfect and we had to provide complete traceability; in a normal company there would be space for UML improvement as the project went on (and if we were talking about an agile process instead of waterfall there would probably be no diagrams at all!!).
  19. I (strongly) disagree. UML is not supposed to be used to generate code. Not automatically, at least. It is also not supposed to be generated from a finished program either for documentation or other purposes. When I was taking my Object Oriented Programming class at uni our project had as its first deliverable, even before starting coding, a UML diagram. The aim was to translate the specification into a high-level scheme of how the classes would be related to each other and what fields and methods would be necessary so it would be easier to reason about the solution. And since the teachers graded the UML before we started coding we could correct mistakes and find alternative solutions that saved a lot of work later when things were already too hard to modify. Since we implemented some design patterns that we learned during the 2nd half of the semester in the project, the UML diagrams also helped understand where some pattern made sense and made the adaptations to the code easier because we had the "big picture" right there.
  20. Because you are trying to search for a string in a list of tuples. What you want is to search for that string inside the tuples in the list. To clarify a bit, the line ans = [True if 'com' in x else False for x in listy] is syntactic sugar (a simpler syntax to do the same thing) for a map with a lambda. You can find some more information here: http://docs.python.org/2/tutorial/datastructures.html Basically, ans will be a list that results from applying the if statement "True if 'com' in x else False" to every element x of listy. That means that it will go through the elements of listy and append to the result (list ans) True if the element you are looking for (uInput in your case) is in that tuple or false otherwise. Then, it's just a matter of finding out if there was a match (True is in the resulting list) and where it is (it's position inside the list will be the same as the position of the tuple that contained the string in the original list). Hope that (somewhat) clears it up a bit.
  21. You keep using readlines(). I don't think it means what you think it means! readlines will read the file and advance the cursor, so eventually you will reach the end of the file and it will stop returning lines. I use it without arguments, but apparently the optional argument is the least amount of bytes that should be read. It will always read until and end of line or file. What you want to do is read the whole file to a variable just once with conn.readilnes() and there you'll have the entire contents. As for the multiple options that represent the same country I just learned something new in python in just 5 minutes. If you have, after reading the file, a list of tuples or lists, you can use the function map to check if an element is in one of the tuples of the list. Here is the example I just made: >>> listy = [('a'), ('b', 'ba', 'bc'), ('c', 'com')]>>> ans = map(lambda x: True if 'com' in x else False, listy)>>> True if True in ans else FalseTrue>>> ans = map(lambda x: True if 'banana' in x else False, listy)>>> True if True in ans else FalseFalse It's not exactly the best code ever, but shows that it works. The >>> are from the interpreter. If you want to know which index the answer is in you can do >>> ans = [True if 'com' in x else False for x in listy]>>> if True in ans:... print(ans.index(True))... else:... print('nope')...2>>>
  22. https://gist.github.com/migueldferreira/9485468 Just a simple python 3 script with some functions to create, populate and query a database of transactions. Helps me keep track of mah monay! The goal of this script was to create a stepping stone to move from excel (libre office calc, actually) to mysql and php and be able to log my transactions from the web without having to store the files in dropbox or having to ssh. But that's probably going to take a looooooong time! I'm still trying to figure out some functionality to be able to categorize the transactions (that's why there is an extra 'type' column in there) to make searching and "data mining" easier.
  23. This is a more "critical section" oriented solution, but since you have many tools I would opt for a data oriented synchronization scheme. If you use a lock to control access to that function you restrict potential parallelism since every thread has to wait their turn even if they end up picking different tools. Also, when I think about tools, craftsmen or even threads I think about objects (structs in C) that can hold individual locks and other information for each entity, making storing and sharing information easier than having multiple separate structures. Just a thought, though. And I agree that the solution with the array is better than with lists since with a list you could only have one lock and with an array you can have a semaphore for each tool (object).
×