565 Posted Topics
Re: This kind of question is designed to show you that a loop for all the numbers N=1 to N=LOTS [because the answer is big!], is not the way! Even if you got your div function you work, you could easily be spending a long time waiting for it to work. … | |
Re: I am going to ask about firstPerson's code here. So first some initial thoughts, first I always like to separate output from function, i.e. don't do logic/maths in the same place as you are telling the user what happens or what is wanted. Especially, as often initially you develop going … | |
Re: The obvious approach most Vector3D classes use it to represent the data in one form and convert from input in another form into it (e.g. leave the vector in cartesian for and convert from spherical and cylindrical input). Depending on the problem that is a viable solution [not always]. Then … | |
Re: The issue isn't really the coefficient value that you have with the polynomial, it is that you have to realize, that the size of your polynomial will change dramatically, on addition, subtraction etc. Once you have figured out how to handle that, [in particular to remove zeros e.g. (x^2+3x) + … | |
Re: Sorry to say, it is the usual problems. Basically, if you get a problem like this put some std::cout statements in the inner loops that are doing stuff. .e.g in reverseArray. You would find: [icode]for(count=5;count = 0; count--)[/icode] is wrong since I think you intended to write [icode]for(count=5;count != 0; … | |
Re: First of all, welcome to Daniweb. Thank-you for writing a coherent, well constructed post, with code tags. Your problem in this code [which actually causes the core dump] is that both of the findCoord functions can return an undefined number. Consider your code: [code=c++] int findCoord(std::vector<MazeCoord> &maze, int xarg, int … | |
Re: First off before you actually code this, you need to think about the maths that is involved. Let us consider it. Using "pen and paper" first, is often the quickest way to write a computer program (and it also gives you all the test cases quickly). Consider the cat is … | |
Re: It is not strange that it runs, however, the problem is that exitMinutes is uninitialized. That means that when the value in exitMinutes is read, it could be ANYTHING. To see how that came about, consider your program. In the main program you write [icode]int exitMinutes;[/icode] which creates a variable … | |
Re: The question is really do you know how to draw stuff in a c++ program and want help with the maths for making a 36 pointed star shape [10 degrees]. Or do you want help writing a C++ program to draw stuff. If it is the maths, then the obvious … | |
Re: Well appart from the strange layout, it works. That is always good. Do note that a*a is the same as pow(a,2). It is also a lot more normal to do the calculations in the double form. E.g. [icode]pow(a,2.0);[/icode]. It is often quicker as well, since the floating point unit is … | |
Re: You almost have the code for that... You have written: int winnerIndex(int votes[]) { int i; int maximum; maximum = votes[0]; for(i=0; i<5; i++) { if(votes[i] > maximum) maximum = votes[i]; } return maximum; } Now if you add something like this: int winnerIndex(int votes[]) // This function now returns … | |
Re: In addition to gerard143's solution, which is fine. You may want to make getitsvalue virtual. This is problem is a specialization of the common construction: [code=c++] class Base { public: // Stuff... virtual void write(std::ostream&) const; }; class Derived : public Base { public: virtual void write(std::ostream&) const; }; std::ostream& … | |
Re: There is a third part the equation here: To illustrate consider this: [code] class AClass { public: AClass(const int,const int) { std::cout<<"Constructor"<<std::endl;} AClass(const AClass&) { std::cout<<"Copy Constructor called"<<std::endl; } AClass& operator=(const AClass&) { std::cout<<"Assignment operator called"<<std::endl; return *this; } }; int main() { AClass Object(1,1); // Object constructed with constructor … | |
Re: Firstly all variables are scoped. That means that if a variable is within a set of brackets [icode]{ }[/icode] then the variable will exist only within that region. Each time the execution leaves that region, the variable is said to be out of scope. [code=c++] int myFunc() { int A(10); … | |
Re: First welcome to Daniweb, second please use code tags! It make life easier for all of us. The problem is possible with the use of [icode]srand(time(NULL));[/icode]. What you are doing is re-initializing the random number generator with a number and that number is from the time. HOWEVER, time(NULL) normally only … | |
Re: You seem to think that intermediates are attempts that is not the case, I don't understand how you can skip that test of 875, it is a guess, and too high a guess. e.g. [code] computer value: 789 minValue=1; maxValue=1000; for(int i=0;i<10;i++) { guess=(maxValue+minValue)/2; // set maxValue minValue based on … | |
Re: There is always the ostream methods e.g. [code=c++] void print_list_four(const std::list<cProg3>& lst) { copy(lst.begin(),lst.end(),std::ostream_iterator<cProg3>(std:cout,"")); } [/code] However, you will want two things, (1) to change your [icode]void Print_f(fstream &Outfile);[/icode] to something slightly more general.[icode]void Print_f(std::ostream&) const;[/icode] and (2) to add this functions [code=c++] std::ostream& operator<<(std::ostream& OutStream,const cProg3& A) const { … | |
Re: It sounds like you are looking for an interpreted language which can interact with you c++ code. If that is the case, then look at languages like lua, e.g. [URL="http://www.lua.com"]http://www.lua.org[/URL]. Used with swig it makes a really quick interface, [url="http://www.swig.org"]http://www.swig.org[/url] There are others, but I found these two worked well … | |
Re: Several things come to mind. [and in no particular order]. You will have to judge what you think the importance is. (a) You test a double precision number to be exactly equal to zero, that very often doesn't happen, e.g. your get 1e-314 or something very close to zero but … | |
Re: Without all the code to test this, we are just guessing. BUT: the for loop in the copy constructor looks horribly suspicious. [code=c++] for (DLNode *sourcePtr = source.head->next->next; sourcePtr != tail; sourcePtr = sourcePtr->next) { // ... } [/code] You see that tail is set to [icode]tail(new DLNode::DLNode(head, 0, NULL))[/icode] … | |
Re: First off, absolute brute force isn't going to work in this case as the input is in floating point, and you can easily get a function that has maxima at non-integer values. Next, the finding of minima/maxima of a function in a range, is still an active research topic. Even … | |
Re: The code that you wrote correctly works in both cases because you didn't actually do anything with obj2 or obj1. Note you wrote this: [icode]cout << "ob2.tab[0] = {" << obj1.getTab()[0] << "}" << endl;[/icode] That should have been [icode]obj2.getTab()[/icode] not [icode]obj1.getTab()[/icode] Additionally you have incorrectly written the constructor: This … | |
Re: First off, well done for thinking about contributing to OSS [Open source software]. Personally, I think the best way to get a project is to actually look at your secondary skills and interests. Almost everyone involved in OSS is actual able to program. The skill level is surprisingly high. The … | |
Re: Well you really are going to have to give us a little more code. There are no obvious things that you are doing wrong: [code=c++] #include <iostream> #include <iomanip> #include <sstream> #include <string> #include <vector> int main() { std::ostringstream cx; typedef std::pair<int,std::string> PT; std::vector<PT> XX; for(int i=0;i<10;i++) { cx<<"a"; XX.push_back(PT(i,cx.str())); … | |
Re: I expect that you wrote somthing like this: [icode]std::cout<<"result == "<<a<<std::endl;[/icode]. The problem with that is the default precision, [B]for output[/B], is 6. The number itself is of a higher precision. If you write this [code=c++] #include <iostream> #include <iomanip> int main() { double a=1.2345678901234567890; std::cout<<std::setprecision(20); std::cout<<"results ="<<a<<std::endl; // Not … | |
Re: Sorry the solution given is just too inefficient. You simple don't need to copy the whole array each time. Taking ravenous's code base: [code=c++] const unsigned int range = 100; const unsigned int numberToSelect = 10; int* remainingNumbers = new int[range]; int* chosenNumbers = new int[numberToSelect]; // Initialise the list … | |
Re: Something that avoids all the graphical stuff, [Well use an external program - not part of the project] is a simulation. That can go from easy, to [B]extremely[/B] complex. There are a number of classes of simulation, but it is easier to give an example, what about a set of … | |
Re: Obviously, it would be a lot easier to look at this if you had posted all the code. [B]However: [/B] Find_Trav_Subtree looks very strange [to me]. You see it calls the right branch [icode]if (current->Right)[/icode] AND then [B]always[/B] returns Find_Trav_Subtree from the right branch REGARDSLESS of it returning a NULL. … | |
Re: There are several mistakes in the above code. The main one is that you wrote the code in one long block without debugging each section. It is almost always easier to write the code in little bits, keeping a running program that does a little more each time. Let us … | |
Re: On reading this I thought that the obvious solution is to use a functional. The functional can be defined as a member of your class, or not. Additionally, I wasn't actually sure what algorithm you were implementing, in this example I consider that you have an vector of values, and … | |
Re: [QUOTE=;][/QUOTE] Ok the class is a policy class. So let us understand what you can do with the original code and your code. First consider two classes: [code=c++] class SimpleObj { // Stuff.. public: SimpleObj(); // Simple default constructor }; class ComplexState { public: ComplexState(const int,const std::string&); // THE ONLY … | |
Re: There are two issues here (a) code given doesn't do the assignment. (b) you don't understand the recursion. First let us deal with (a). Code given returns false if stones are greater than 5. That is not true. Consider the case that there are exactly 6 stones. If I take … | |
Re: Sorry I am not impressed with your teacher. Most compilers of c++ and a lot of other compiled language split the program up into individual files. Each of those files are normally compiled into object files. These normally have the .o extension. They are the machine code of each block … | |
Re: Your problem is that you have masked length. You wrote this: [code=c++] # void file(char* Filename, int length) { int length; // UNINITIALIZED VARIABLE: for(int i=0;i<length;i++) { } } [/code] Note that length is NOT the same length as the one passed to the function, you have masked it with … | |
Re: You problems are in your fuction changeGridStorage. You do this [code=c++] x.resize(n); // this is fine y.resize(m); for(int k=0;k<=m;k++) // I REALLY don't think that you need <= BUT this isn't your // main problem v.resize(n,vector<double>(k,0)); // Ok talk about packing a lot into one line: // I will discuss … | |
Re: Well done! I haven't seen this bug/error before. Anyway it made me smile. What you have done, is simple use istringstream in a very odd way, the effect is to create a local variable [code=c++] std::string str; while(getline(input,str)) { istringstream(str); // This line is the proble: // str is a … | |
Re: Ancient Dragon is nearly perfectly correct in what he said, but can I say that you have to think about the deletion of the memory as well. First off, he has made a couple of typos, however, I think that is caused by fiolet. The AD code should be: [code=c++] … | |
Re: you have two problems with your code: (a) missing typename on the in the template file. [code=c++] std::vector<RecordType>::const_iterator it; // THIS IS WRONG // This is correct: typename std::vector<RecordType>::const_iterator it; [/code] There are many explanations around for this, it basically is because since depending on what std::vector<RecordType> is specialized as, … | |
Re: The problem that you seem to have with this code is understanding the pathway of the if / else construction and the way to define it. There is a "formal" way to define it, but I think that first time it is easier with an example: So here goes: [code=c++] … | |
Re: I don't like the example given. I thisk that it misses the opportunity to be a useful learning tool: I would like it modified like this to allow the user to actually see what went on: [code=c++] // bad_alloc example [+ modification] #include <iostream> #include <new> int main () { … | |
Re: It is a bit more complex than that. You have made a whole set of basic errors, and I think you have to fix all of them to get yourself out of jail here. Let me start: First off you have TWO constructors, (a) the default constructor and (b) a … | |
Re: I am not surprised this doesn't run! You have a head node called root. However, rather than add a new copy of the memory for it, you do this: [code=c++] if(root == NULL) { // THIS IS A RUNTIME ERROR: root->data = Person(name, number); root->left = NULL; root->right = NULL; … | |
Re: This is just a simple multi-file program. The big advantage of this [Particularly for big projects] is that you don't have to keep recompiling all the program in one go, just the bit that you are changing. Although it doesn't do anything you can compile it like this: (as individual … | |
Re: You unfortunately have several problems: So i am going to give you a quick summary of the problem and why I think you have made the mistake. (i) In the constructor fraction::fraction(), you have written [icode]integral, numerator, denominator = 0;[/icode] This DOES NOT do what you think, it does NOT … | |
Re: Sorry but I REALLY don't understand what you are trying to do. I would appreciate an example or two. However, that leaves the code, which is simply horrible. It cannot possible be necessary to write so much code to do this, and it cannot be necessary to using strings to … | |
Re: Absolutely fine, (IMHO), obviously it depends on your needs, but what about classes that return some kind of evaluation, e.g. a class that calculates the global density from all the objects in a simulation. It knows the materials, object locations and orientations. It has only one public function that returns … | |
Re: As Vernon says, you are very lucky that line 40 and 46 compile. HOWEVER: if they do, the (which in your case they obviously do). The actual error is this: [icode]cout << letterlist [3][0];[/icode]. That is because possible is an integer an actually over flows!! [code=c++] for (int n = … | |
Re: Ok the first and most important problem is simple that you have gone about this task in the wrong way. Start with the assumption that the compiler is your friend not your enemy, it is there to help, but if you write your whole program and then try to compile … | |
Re: The problem is basically simplified to this: [code=c++] double **array = new double* [size]; array[i]=3.0; [/code] What you have done is declare a array as a pointer to a pointer of arrays. That is effectively a double array like dynamic version of [icode]double A[10][20];[/icode]. However, you did not initialize the … | |
Re: One of your problems is the error in CheckTitle: [code=c++] bool CheckTitle(VideoGame*& first, string title) { VideoGame *current; current=first; while(current!=NULL) { if(current->Name==title) { return true; } else { return false; } // You can't get to here : current=current->link; } // If you get here what do you return ??? … |
The End.