|
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. |
On February 16 2013 14:38 mcc wrote:Just a small idea, maybe change the representation to prefix or postfix notation if allowed by the assignment  So something like : 5(5AB)(1(4AB)(4BA)). Infix notation is terrible, I have no idea why it is used everywhere, it is so unfriendly for us programmers  But of course you can parse the infix notation also to create the expression tree recursively or without recursion. Just realize what are the invariants at each step. When you are examining (sub)formula there are 3 possibilities 1) it starts with 0 so 0X - just put the 0 at the root of the expression tree and parse X and put it as a subtree 2) it is in the form of (X) - well parse X and you have a tree 3) it is in the form of X{1/2/3/4/5}Y - put the number as the root of the expression tree and parse X and Y and put it as its two subtrees Note that I assumed that every subformula is in brackets. If it is not the case, you have to make it more complex and possibly include operator priorities.
Thanks for the suggestions! I figured out how to recursively parse the function and create the expression tree.. after writing the string manipulation implementation that I had mentioned. So yeah, I pretty much had to rewrite everything. Oh well
|
Yes, java has the same ternary operator common in most languages, that works just fine.
Unless you properly define what "num" is for us, then fabiano's code my fail, but if "num" is an array of int, then it's spot on.
|
On February 17 2013 03:35 BADBADNOTGOOD wrote:Show nested quote +On February 17 2013 03:10 Kambing wrote:On February 17 2013 02:54 BADBADNOTGOOD wrote: I'm looking to start learning the .NET Framework ( C++ and/or C# ), starting out with building basic console applications. For a while now I've been trawling around the MSDN site looking for ANYTHING about include libraries, DLL Pre-Build commands ect but I can't find any info on using a .NET class in VC++ 2010 / VC# 2010.
I have no idea how to even get these .NET classes recognised by Visual Studios.
It's probably me being stupid but I am clearly missing something, maybe I'm not understanding how development with .NET works?
Any suggestions/ input would be appreciated.
If you are learning the .NET framework, then you should be restricting yourself to C#. What do you mean by getting VS to recognize your classes? The library-and-build model for C# (to a first-order approximation) is much saner and cleaner than native code (which is sounds like where you are coming from). Can you give a concrete example of what you'd like to do as your initial statement is rather vague. Lets say I literally only want access to the System namespace (http://msdn.microsoft.com/en-gb/library/system.aspx) What headers do I need to use? What libraries do I need to use? How do you set up a VS project to recognise this .NET Framework namespace? I'm sure I'm just completely overlooking something or missed the page on Getting Started where it mentions this but everyone who talks about using .NET seems to skip mentioning any of this. Hope that's clearer.
Within the .NET framework and the .NET languages (e.g., C#, VB, V#, and IronPython), the fundamental code unit is the assembly which takes the form of either a .NET executable (traditionally with a .exe extension) or a .NET library (traditionally with a .dll extension). Assemblies contain code (in the CIL format) and metadata which are the types that the assembly exposes to the outside world. In contrast, native libraries only contain code but no metadata regarding their contains. Thus in the native code (specifically C/C++) world, you use header files that specify what types and functions are exposed by a given library. The difference is that there is no strong connection between the types defined in a header file and the code of a native library whereas with an assembly, the two are (rightfully) strongly tied together.
That being said, to use an assembly in your own code, you reference it during the compilation process which exposes the types in the assembly to the compiler. Your resulting program then has a corresponding dependence on that assembly at run time. In the MSDN documentation, each class tells you the assembly it comes from, e.g., the generic List<T> class
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
comes from mscorlib.dll which is the .NET framework core library. It is implicitly referenced in every project.
Note that the documentation also mentions the namespace in which List<T> exists (the System.Collections.Generic namespace). In the .NET framework, namespaces have no special relationship to assemblies. Namespaces merely provide a scope to names. Furthermore, multiple assemblies may contribute to the same namespace. For example, the XmlElement class:
http://msdn.microsoft.com/en-us/library/system.xml.xmlelement.aspx
Is declared in the System.Xml namespace but exists within the System.Xml assembly. If you needed to use this class, you would need to add a reference to System.Xml.dll in your project (right-click References -> Add References and look under Framework).
Thus, the fully qualified name that you would use to reference a List<T> is really System.Collections.Generic.List<T>. However, this is a pain to use, so languages offer different ways of eliding fully-qualified names when there are no ambiguities. In C#, this is accomplished via the using declaration:
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication2 { class Program { static void Main(string[] args) { // This declaration is possible because we (implicitly) // reference mscorlib.dll where List<T> exists. System.Collections.Generic.List<int> list1 = new System.Collections.Generic.List<int>(); // This declaration is possible because we use the using // declaration above. List<int> list2 = new List<int>(); } } }
So the likely short answer to your question is that you need (1) make sure that you are referencing the appropriate assembly that contains the classes you want to use in your project and (2) use using declarations to reference those classes.
The long answer is appropriate because how code is organized in the .NET framework is one of its key features is and very easy to misunderstand if you are coming from a different background.
|
Okay, this is JavaScript code for long division which I found here - http://weblog.bocoup.com/long-division-in-javascript/
+ Show Spoiler + function longDivision(n,d){ var num = n + "", numLength = num.length, remainder = 0, answer = '', i = 0;
while( i < numLength + 3){ var digit = i < numLength ? parseInt(num[i]) : 0;
if (i == numLength){ answer = answer + "."; }
answer = answer + Math.floor((digit + (remainder * 10))/d); remainder = (digit + (remainder * 10))%d; i++; } return parseFloat(answer); }
longDivision(356,8);
I've tried to make it work in Java, and this is what I have so far:
+ Show Spoiler + public class test { public static double longDivision(int d, int n) { String[] num = new String[1000]; num[0] = "" + n; int numLength = num.length; int remainder = 0; String answer = ""; int i = 0; while (i < numLength + 3) { int digit = i < numLength ? Integer.parseInt(num[i]) : 0; if (i == numLength){ answer = answer + "."; } answer = answer + Math.floor((digit + (remainder * 10))/d); remainder = (digit + (remainder * 10))%d; i++; } return Double.parseDouble(answer); }
public static void main (String[] args) { System.out.printf("%f\n", longDivision(356,8)); } }
I have 2 problems though: 1. 'var' in JavaScript is so ambigious, I'm left to guess what data type to use in Java. 2. I'm still not sure what to do with the num array which probably causes the whole program not to work as it should.
Edit: Nevermind, ternary isn't touched here, but the num array looks rather messy.
And yeah, I get this error:
+ Show Spoiler +
Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at test.longDivision(test.java:11) at test.main(test.java:26)
NumberFormatException: null at the line of declaring 'digit'.
|
Write more documentation detailing what you're trying to do at each step. I think you would be able to catch many of the problems if you just tried to understand what each line of code is doing.
Some problems with your code:
1) You switched the order of the numerator and denominator 2) The algorithm you are copying from returns a quotient and remainder. The problem you described several pages back explicitly needs high decimal precision. You fail to account for this at all. 3) You declare an array of strings, assign the first value, never assign the ith value, and then try to repeatedly access the ith value. 4) You stick the entire numerator (or what would be the numerator once you fix the issue in #1) into a single string, when I think you're trying to separate out the digits. It's not clear.
Those are the main quick 4 things I see wrong, there are probably more but it's not worth debugging because those 4 will completely fuck up all the rest of the function.
|
More detail on the problems I described above:
+ Show Spoiler +2) You are just using the wrong long division algorithm. See if you can't work through the steps of long division from wikipedia yourself and write out the pseudocode. Use an algorithm that actually generates decimal points instead of a remainder. You will also need to include code for catching infinite loops, because many fractions repeat for infinity. 3) What I mean is you are doing this: String[] num = new String[1000]; num[0] = "12345"; // ... some more stuff happens, but nothing using num Integer.parseInt(num[0]) // ok.
// ... Some more stuff happens, but nothing using num Integer.parseInt(num[1]) // this blows up because num[1] was never assigned. // you only assigned num[0] and then tried to access num[1].
int foo[] = new int[10]; foo[0] = 5; int bar = foo[1]; // This also blows up. If you still don't understand what I mean with #3, you need to crack open a basic tutorial on arrays. This is a good start, but there are probably more things you can learn: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.htmlBy the way what happens when you get more than 1000 digits? You need to handle that case as well. 4) You need to get the individual characters of the string and use each digit. Look at String.charAt() or something. Also you should probably use an int[] (or List really, just stop using arrays altogether) instead of string[].
|
Hey guys, I need some help/clarification on an assignment I'm doing.
One of the problems require me to write a member function that compares two strings.
A member function “CompareStrings” that would compare two strings (similar to strcmp “C” library function but do not use strcmp function) that returns 1 when the first string is grater (in lexicographical order) than the second one, 0 if the two strings are equal and -1 otherwise.
My issue is, what is lexicographical order? I've been trying to google it up but I have no clue what it exactly means.
If I compare two string like "abcdefg" and "abcdegh" or "Hello" and "Bye", how do I compare them in lexicographical order?
|
http://en.wikipedia.org/wiki/Lexicographical_order
basically means just alpahbetical
They probably want you to compare the ascii char values of each individual point in the string. Keep going until you find a non-match, use that result (careful about running off the end of the string).
|
On February 17 2013 14:49 phar wrote:http://en.wikipedia.org/wiki/Lexicographical_orderbasically means just alpahbetical They probably want you to compare the ascii char values of each individual point in the string. Keep going until you find a non-match, use that result (careful about running off the end of the string).
I'm not quite sure how to go about it. Do I add the ascii char values of each point and compare the sum of one string versus another?
also, I also need some clarification of the second function:
A member function that returns the number of occurrences of a character in an input string (both passed as formal parameters).
I'm just not sure what it means.
2 of the 4 functions I need to write I'm perfectly fine with. The lexicographical and this one is the two member functions I need help with.
|
It means like
aa comes before aaa comes before aab
aa aaa aab b bb bba bbb
etc
you need to step through both strings at the same time and compare each individual character. so look at the first character in both strings, compare them, make a decision to return bigger, less than, or go on. Put that in a loop, handle the cases of one or both strings running out, and you're done.
|
For the second one, you just need a single loop over the input string.
Do you know how char works in C?
char x = 'a'; char y = 'b';
x == y -> false x > y -> false x < y -> true
|
|
|
I don't know this course, but rather than an introduction to Python it seems to be an introduction to computer sciene and programming. So it doesn't matter what programming language is used there. It is about the fundamentals and to understand how to do stuff. If you're done you can apply the principles in any programming language you like to learn.
|
Hey guys, I'm working on a program for my c++ uni class, and am trying to figure out how to initialize n sets of arrays. For example if the input n = k, I need to initialize "k" arrays. What I've got right now is this: + Show Spoiler + while (i<n) { int arrayName+i; // I want this to return arrayName1 on the first run, arrayName2 on the second, etc. i++; }
Where i is initialized to 1 and n is inputted earlier in the program. Any ideas to make it work? Much appreciated.
|
On February 18 2013 11:24 JKholodnov wrote: Hey guys, I'm working on a program for my c++ uni class, and am trying to figure out how to initialize n sets of arrays. For example if the input n = k, I need to initialize "k" arrays. What I've got right now is this: You mean an array of arrays? Or a std::set?
Have no idea what you mean by n = k. But if you just want to initialize an array of arrays:
// For an array of k arrays of n ints void foo(int[][] bar, int k, int n) { for (int i = 0; i < k; ++i) { bar[i] = new int[n]; } }
|
That isn't exactly it. I meant the program is inputted an int n earlier in the program, and then must make that many arrays. If n = 2 then I need 2 arrays created. If n =3 then 3 etc.
|
On February 18 2013 12:43 JKholodnov wrote: That isn't exactly it. I meant the program is inputted an int n earlier in the program, and then must make that many arrays. If n = 2 then I need 2 arrays created. If n =3 then 3 etc.
I'm a bit rusty on my C++,
#include <vector>
int main () { std::vector<int> outside_vector; for (int i=1; i<=n; i++) { std::vector<int> inside_vector; outside_vector.push_back(inside_vector); }
...
return 0; }
Why do you need to use an array? As far as I know, arrays can't be dynamically allocated so what you're describing is impossible. From what I can tell as well, your solution to this problem doesn't seem to be optimal, so if you want to describe it in another way some of us might be able to suggest a better alternative. Vectors inside vectors are most of the time a big no no.
|
--NUKED-- I don't know what I'm talking about.
|
Yea you're right, my C++ is equally rusty, my code may not compile. Or maybe it would, I can't remember how the new operator works. Essentially what you need is the C++ equivalent of
foo[i] = (int *) malloc(k * sizeof(thing)), which I think the 'new' operator gets you for free.
At least I think that's what new int[n] is supposed to do for you.
|
Does anyone else have trouble learning programming when they're near a computer? Like I keep getting distracted. What helps?
|
|
|
|
|
|