565 Posted Topics
Re: The problem is that you are passing a particular structure to your printstudent function BUT you are not actually using that data. Let me explain with an example: [code=c++] int main() { Student FirstStudent; // Make a student Student SecondStudent; // Make a DIFFERENT student // Now populate the student … | |
Re: It is another one of these typename instances. First off the solution is this: [code=c++] template<class t> typename Openfile<t>::reference Openfile<t>::rout() { return fin; } [/code] Ok that is the simple part, now I am going to make a complete mess of trying to explain WHY you need a typename. [sorry … | |
Re: What doesn't work with this: int main() { VStack<double> A; A.push(10.0); std::cout<<"A =="<<A.size()<<std::endl; } You remember that you have to use a template type in declaring VStack and that you might need to explicitly initialize the template if VStack and Stack are in separate files e.g. Add template class Stack<double>; … | |
Re: This seems a lot like you have coded before you have figured out what you are going to do. So let me point out a number of strange things that you may have overlooked. First you initialize the board to 1,2,3, etc... e.g. [code=c++] for(int y=0; y<n; y++) for(int x=0; … | |
Re: I think you have almost expressed your own solution, :). If the race is ALWAYS going to be less than 12 hours, or in some way constrained to be in some range which is less than 12 hours, then you simple calculate the time, and if the time goes negative, … | |
Re: You main problem is the algorithm that you calculate a continuous average with: you do this: [code=c++] for(int i = 0; i < Student::getCount(); i++) for(int j = 0; j < 5; j++) avg[j] = (avg[j] + (userData + i)->getScores(j)) / i; [/code] So what that does is effectively divide … | |
Re: The obvious [once you have seen it] trick to make your output easy to read is this: [code=c++] char first=' '; for(int i=0;i<num_var;i++) { if (equ_1[i]<0) cout<<equ_1[i]<<variable_array[i]; else if (equ_1[i]>0) std::cout<<first<<equ_1[i]<<variable_array[i]; if (equ_1[i]) first='+'; } [/code] You can note that you don't need a separate loop index for variable_array, and … | |
Re: This is a classic case of ignoring the compile warnings! It is 99.999% essential to turn on compiler warnings and fix the ALL. [Yes sometimes that means turning a particular warning off, but at least you are aware of exactly what caused it] In your case you have a constructor … | |
Re: I would like to see all of this class, well in particular the copy and assignment operator, as well as the default constructor. The correct constructor should have: [code=c++] arrayInstance = new T*[rows]; for (int i = 0; i < rows; i++) arrayInstance[i] = new T[cols]; [/code] the destructor should … | |
Re: As arkoenig said, as you have written it, it doesn't compile. If you are using the construct operator new, you need to do it like this: [code=c++] class X { X() { std::cout<<"Constructor called "<<std::endl; } ~X() { std::cout<<"Destructor called "<<std::endl; } }; int main() { X *x=new X; X … | |
Re: First off I would like to second SasseMan's comment, for very high precision you need the GMP. Second your algorithms is going to kill you, you have 27111, copies of a function call on the stack, that is a block of memory for each function call. If you wish to … | |
Re: Almost any algorithm that calculates Pi, [or anything else] is almost completely unlikely to need true-random numbers. What you normally need is certain properties of the pseudo-random number generator. For example, if you decide to calculate pi by simulating a 2x2 square (area 4) and a circle in that square … | |
Re: Unfortunately, the a pile of little but important numerical/coding errors. In arrMean, you don't actually get the wrong answer (I think), but you don't need to calculate avr until after the loop which adds up sum. arrMedium: [icode]if (middle % 2)[/icode] implies that the remainder of middle / 2 is … | |
Re: Looking at your code you are almost 100% creating the array incorrectly. And that may cause your problem. By the looks of it you want an 2d array of dimension d and delta. E.g. as if you had known d and delta at compile time you would have written [icode]int … | |
Re: Can I just add, the code the OP has given will go horribly wrong at some point, since [icode]int random_card = (rand() % cards.size()) - 1;[/icode] can result in -1. That will be a very strange output, and some memory violation later, most likely resulting in a core dump. The … | |
Re: First off there is not enough code provided to actually compile this. However, if the only error was that you are getting array overrun I am absolutely surprised. The likely error is to do with deletion, and the use of bare-pointers in std::vectors. E.g. Consider struct Population. Now that takes … | |
Re: Very sorry to tell you but the operator ^, which you use e.g. [icode]a2=a^2[/icode] is actually the XOR operator. ie. it compares the bits in the value and sets them according to the rule: [code] A B Out 0 0 0 1 0 1 0 1 1 1 1 0 … | |
Re: Why don't you capture the salient information before the decision tree. e.g. [code=c++] struct TRes { double pTime; // time in seconds double yVal; // y positions A(const double Y) : pTime(time()),yVal(Y) {} }; // Note this becomes the fixed point TRes A(position); if (A.pTime<0.08) y1=A.yVal; else if (A.pTime<0.15) // … | |
Re: Well first off, ALWAYS write a constructor, a copy constructor, an assignment operator and a destructor unless you are 100% absolutely certain you don't have to. If you have to think about it for even a second, write them all. Therefore, what happens, well size is not initialized. [nor is … | |
Re: Ok, first off, I REALLY hope this doesn't compile on VC++ because if it does then VC++ is adding a bracket In addition to not compiling to the standard. So what did you do wrong: You missed a } at the end of insertAfter in SinglyList.h. [The clue is that … | |
Re: [QUOTE=;][/QUOTE] Yes you should post life.h, well how can WE run the program, if you don't! -- and you want help with a runtime error!! You seem to have made a mistake in iterateGeneration(). You create an initial line text that has zero characters in it. Then you call findNeighbours, … | |
Re: Well first off, you can choose to use the abs function, or not as you wish. If you want to use the abs function, then do this [code=c++] int a(93); int b(102); std::cout<<"difference == "<<abs(b-a)<<std::endl; [/code] Note, that you will need to add [icode]#include <cmath>[/icode] to your header. You could … | |
Re: You have made the classic error, that always seems to trip up beginners: That is if you use integers, then you get integer arithmetic : Consider this code: [code=c++] std::cout<<"1/4 == "<<1/4<<std::endl; [/code] That will print the unexpected value: 0. That is because 1 and 4 are both integers and … | |
Re: [QUOTE=;][/QUOTE] First off, yes you need to add a semi-colon. BUT in addition you need to note that GCD is a member function of Mixed and you have extracted the operator to an external friend function [more on that later]. Since GCD does not actually touch the class, or use … | |
Re: [QUOTE=;][/QUOTE] Fundarmentaly you have two problems , (a) that you are using the wrong algorithm, (b) that you have put the multiply operator as a friend method, and not a standard method of the class. [code=c++] class Polynominal { // stuff Polynominal operator*(const Polynomial&,const Polynominal&) const; }; [/code] is a … | |
Re: Well a little more information about the actual error message and the compile sequence would help. However, my guess is that you forgot to create an instance of your templated class. If you combine the code into one file, it works. [Not something to do other than for testing !!!!] … | |
Re: Your problem looks like this line: [icode]scanf("%s/n", &ans);[/icode], since ans is a char. But you are reading it as a string. It is not. You perhaps want to do this [icode]scanf("%c",ans);[/icode]. The reason you get a crash, is that your text entry on the command line, is always at least … | |
Re: [QUOTE=;][/QUOTE] Ok in the first instance, using power is plain ugly and inefficient. The typical route for a polynomial in the positive sense, is to do something like this: [code=c++] int total(0); int xpow(1); for(int i=0;i<=degree;i++) { total+=coef[i]*xpow; xpow*=x; } [/code] You can also do it via an internal bracket … | |
Re: [QUOTE=;][/QUOTE] Firstly, obviously you can see that you don't need any template specialization for this example. But assuming that this is a example, let us see what you did wrong. It is just that sloppy practices that are allowed in non-template code are not allowed when dealing with templates. (a) … | |
Re: [QUOTE=;][/QUOTE] I think the issue here is first to define "big". What does that mean to you, size of an OS kernel (2million lines) , size of a big application (500,000 lines) etc. That gives you an idea of how much time a rewrite will take. Next define the problem, … | |
Re: Your biggest problem here is that you (a) are trying to do evergthing at once, (b) cell broad.... You seem to have overlooked that if you have a cell board[SIZE][SIZE]; in your class definition, then you don't put cell in front of broad to used it. That is because your … | |
Re: Sorry, you haven't given us enough code here... We need to know what w, w_temp, h_temp get assigned to and how. If you can, post the fragment of code that they are set in as well please. I guess the you almost certainly have made a memory error with the … | |
Re: Your problem is the assignment beyond the end of the array. You wrote this [icode]for (int i=0,j=0; i<row,j<5; i++,j++)[/icode] And that means : do i<row, ignore the result and test j<5. Therefore when you get to this [icode]m[i][j]=2;[/icode] you will definitely run m[4][4]=2; regardless of row. That means you assign … | |
Re: You have made mistakes in the code to do the largest AND smallest. First off, consider the test case of a user inputting ONE number only. Let us say that number is 10. Then your code will report that correctly for the largest BUT report 0 for the smallest. The … | |
Re: Not surprising because of a tiny bug in your loop: You wrote: [icode]for( int inte = 1; inte = 1; ) { } [/icode] Now a for loop has four parts, first an initializer, that goes from the first open bracket to the semi-colon. Second a conditional, that goes between … | |
Re: Just coming back to the assignment of a value. I think this is a little verbose and confusing: [code=c++] if(rand() % 2 == 0) {value = -1;} else {value = 1;} [/code] Can we use the mathematical property that you can linearly map any number set to any other smaller/equally … | |
Re: Well done!! I don't think I have seen this error before!! What you have done is this: [icode]calc.Data[SIZE]=(24,21,43,24,43,31,41,42,34,41,45);[/icode] That is your error... You have use round bracket e.g. ( ). That is not what you wanted. So let us see what happens. You first evaluate the contents of the bracket. … | |
Re: Well I don't think this is going to be that welcome, so I sorry. First off I am not 100% you understand why you want to encapsulate something in a class. In this case, you want a fraction type, that behaves as if it is a standard type, e.g. a … | |
Re: So you can't add a virtual function to the base class?? Then your only decision is to make BaseClass completely abstract, i.e a standalone instance is disallowed, or not. If you don't then think about adding the functionality to say class absFunc, which is inherited by BaseClass, then you get … | |
Re: Well first off, this is good. Insert functions correctly, i.e. does indeed double the memory. It also copies (as far as I have tested) and sorts everything out. However, there is a subtle bug in erase that may hurt. You can get a memory violation. In your shift of moving … | |
Re: Ok first off, welcome to daniweb, and thank you for writing a clear post with code blocks and not-too much and not too little code! Ok to your problem, you have unfortunately made a couple of logic errors. First, consider your loop, [icode]for(int c=2;c<= num;c++)[/icode]. This will test each possible … | |
Re: Your problem is the equals sign (=) in your sort function. You cannot e.g. this allows [code=c++] Edge1 A(0,0,1); Edge1 B(0,0,1); // This should not be true to use the algorithm sort. sortFunction(&A,&B) == sortFunction(&B,&A) [/code] Also please use a managed point with vectors, e.g. boost::shared_ptr. | |
Re: The error is because this is a definition of a class. You have then put initialization code in it which is not going to work. You could do something like this: [code=c++] class box { public: int x; int y; XMMATRIX world; ConstantBuffer cb; // constructor box(int a, int b) … | |
Re: First off please use code tags. Second, actually, this is quite a common and slightly difficult problem, and is 100% down to how numbers are represented on a computer. First off float is an inaccurate number, it cannot represent even simple number, like 0.2 without some rounding error, that is … | |
Re: My guess is that you have just landed on the chapter about arrays. I could tell you about the standard library, stuff like vector etc BUT I hope that is a later chapter. In fact, I hope that you do this exercise, three times or something like that, using different … | |
Re: First off C++ has evolved in the last 10 years, hence some changes. These changes were because of unforeseen problems that occurred. Let us start with why [icode]using namespace std;[/icode] and why you might not want to do that anyway. What happens in that there are a large number of … | |
Re: Two quick comments, AD is correct that you should have deleted line 4 in Fraction.h, [and that you have misspelled fractions in main()] (what it does is define the symbol hbg_cmpsc122 as and empty value. Therefore when you write[icode]namespace hbg_cmpsc122 {[/icode] you have effectively writen [icode]namespace {[/icode]. This is why … | |
Re: I am answering this simple because I can't believe that anyone teaches a class without giving some feel for why. Your code introduces that basic concept of a 2D array, ie. having two coordinates. [The idea can be extended up to many dimensions e.g. 3 or 4 etc.] So when … | |
Re: Well first of if you need random numbers to actually compute something else, don't use rand() for anything, it simple isn't random enough. [Basically, rand() has very poor lower bits.] e.g. see [URL="http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx"]http://www.eternallyconfuzzled.com/arts/jsw_art_rand.aspx[/URL] So that leads us to looking for a good [enough] random number generator. The current defacto standard … | |
Re: Nearly working but you have a small tiny problem with your code. In C (and C++) your simple char strings need to be terminated with the value 0. [That is 0x0 and not the character '0']. You didn't do that, and you don't need it for the second loop that … |
The End.