565 Posted Topics
Re: [QUOTE=;][/QUOTE] Several worring things here , but I am going to pick on one detail. Your wrote this: [code=c++] while(input[i]==input[j] && i<=j) { i++; j--; } [/code] Now what happens with the simple word "a". Let us see: First i is set to zero and so is j. The while … | |
Re: [QUOTE=;][/QUOTE] I think that you have mis-understood what is going on in your if statements. The condition [icode] if ('A' != 'z') [/icode] is ALWAYS true for the standard ASCII character set. It is the same as writing [icode] if (65 != 122)[/icode] which you can see is ALWAYS true. … | |
Re: [QUOTE=;][/QUOTE] You should get an error with that code [Nathan's] since size_t is normally unsigned. That means is it is always >0. Use a long int and a cast. Alternatively use an iterator. You can also offset by one [code=c++] for(size_t i=input.size();i;i--) temp+=input[i-1]; [/code] But I prefer iterators. | |
Re: [QUOTE=;][/QUOTE] That is a bit field, so I can't really speak for all of it since I don't know. First off there are not that many completely distinct OS , we have windows, unix (several flavours0, linux, BSD/Mac OS-x then stuff like OS-9 [it is in things like microwaves, digital … | |
Re: First off as Fbody points out correctly, you ideally will give us a bit more information particularly on dataStructure. Second, a large number of "memory" errors come about because of memory freed too early. BUT the error only occurs after the memory is used for something else. This may be … | |
Re: The obvious thing do here seems to be to use a typedef. Consider this [code=c++] class SSE2Vector { // Stuff here that makes use of SSE2 and is fast }; class BasicVector { // General version. Work on everything. }; typedef BasicVector Vector; [/code] What you have done there is … | |
Re: Hi, There is a tiny bit of a trick to doing this. Basically, you need to get enough memory for the characters AND enough memory for a pointers to it. This assumes that both the 501 and 41 in your example are effectively variables that are only known at run … | |
Re: This has tow issues : (a) the code (b) your expectations of the output. First the code: Since you are re-shuffling the code each time, you don't need to re-assign the groups, just re-shuffle. It is much quicker to do it this way: [code=c++] for(int i = 0; i < … | |
Re: If you are going to use a C library in C++, you need to compile the headers in C style. That is done like this: [code=c++] #include <iostream> extern "C" { #include <lua.h> #include <lualib.h> #include <lauxlib.h> } int main() { int s=0; lua_State *L = lua_open(); // load the … | |
Re: [QUOTE=;][/QUOTE] The code as posted works find. [assuming that you put a using namespace std; There are two possible reasons [I can think of] that you are not seeing this code working. (a) you haven't actually run THIS code. The error with the lack of std::, includes and using namespace … | |
Re: [QUOTE=;][/QUOTE] You have forgotten to add a [icode]-lgsl[/icode]. That assumes that you want to make a shared library version of your code, and that you have a path to the libgsl.so file [defined in: ld.so.conf]. IF that doesn't work but you have libgsl.so then you can set the path explicitly … | |
Re: If you do write your own Gauss-Jorden elimination procedure, be [B] very[/B] careful to deal with the huge numerical rounding errors. Re- stabalization of matrix after most steps is essential. Also please not the obvious test matrix [1,2,3][4,5,6][7,8,9] has zero determinate so has no inverse. Simple approaches in books like … | |
Re: Ok the problem is one and only one line. The first is that you are compiling -O3 ... until you have got all the compilation errors out and debugged it DONT use -O anything! Second the error is on line 428 [code=c++] << "After Motor voltage: " << d.amv[x] << … | |
Re: Try using a string stream: [code=c++] std::ostringstream cx; for(int i=1;i<10;i++) { cx.str(""); // clear the stringstream cx<<"File"<<i<<".txt"; std::string Filename=cx.str(); } [/code] Obviously you have to do something with Filename but it has the basics. Note that since I use a loop, you have to clear the stringstream on each pass. … | |
Re: Can I add that you have also got a numerator and num, denominator and dem in the class. That is also an area of confusion. | |
Re: First off: Well done of writing your first "big" class. I have quick question, does the prime check work. E.g what if you have 3, which is prime? I think it give false which is wrong. Also you only need to test as far as the sqrt(N). pow does not … | |
Re: There are a few little errors: (a) You have tried to write a factor that somehow works on the whole array. (b) You have forgotten that if you get clever and add two factors because any number N, has factors 1 and N. Not to test them (c) why does … | |
Re: There are several problems: (i) you haven't given us a piece of code that compiles. What is Bp ? (ii) you haven't given us the input you use , e.g. what does coupon equal? (iii) Then you use what I think is the differential equation approximation at each step. Have … | |
Re: Sorry dustreader, but if you are going to be using the STL, do this: [code=c++] // Assign players in any order (not random) shuffle(players,players+10); [/code] The code given is horrific. It has three vectors etc... However Calyso8 is starting, so it is important for him/her to understand the algorithm and … | |
Re: First off: Fix your compiler warnings. They are there for a reason. e.g. [icode]storage[i]="\0";[/icode] should be [icode]storage[i]='\0';[/icode]. Your memory problem is because in both constructors [that are ambiguous by the way] you allocate storage but do not allocate or even set to zero the memory for each location in storage. … | |
Re: The general way to solve N equations of N unknows of ANY powers (e.g. x^5) is the use Bezout's method. See: [URL="http://www.mathpages.com/home/kmath544/kmath544.htm"]http://www.mathpages.com/home/kmath544/kmath544.htm[/URL] (Also there is a lot of literature to improve the speed and numerical stability). The method is highly tractable to code because as matrix is created and used … | |
Re: You could skip the call to the function and just write this [code=c++] std::cout<<"Time taken == "<<static_cast<double(t2-t2)/CLOCKS_PER_SEC; [/code] You could wrap the static part in a funcit | |
Re: As written there is nothing wrong with this (except I think that you would benefit from a typedef. e.g. [code=c++] typedef std::map<std::string,int> mTYPE; mTYPE aMap; //.... aMap.insert(mTYPE::value_type(word,i)); [/code] However, I would guess that you have swapped the declarations of fileMap and freqMap. It that is not the problem, please make … | |
Re: Actually, it is only partially true. For many template classes you know that you are only going to have a few well known types to create. In that case you can use explicit template initialization. e.g. [code=c++] //file test.h template<typename T> class test { T x; public: void addStuff(const T&); … | |
Re: The problem that you have is understanding how the ordering works. (It is like the maths problems of multiply before addition. e.g 6*5+3 is 33 not 48 ) You have written [code=c++] if (s[i] == 'a'||'A'||'e'||'E'||'i'||'I'||'o'||'O'||'u'||'U') [/code] which is the same as [code=c++] if ((s[i]=='a') || true || true || … | |
Re: [QUOTE=callister;1142426]I know how bisection method works..It just that I don't know how that loop can make it work..[/QUOTE] Well the real problem is that the loop does not work. Or more accurately only works for the specific instance when [ICODE]f(a) <0[/ICODE] and [ICODE]f(b) >0[/ICODE] and there is only one root … | |
Re: Why don't you use dynamic_cast. E.g. [code=c++] class Base {}; class A : public Base {}; class B : public Base {}; Base* Aptr=new A(); Base* Bptr=new B(); A* Ax=dynamic_cast<A*>(Aptr); // sets Ax to a Ax pointer. A* Ay=dynamic_cast<A*>(Bptr); // sets Ay to ZERO [/code] | |
Re: If you want help.. ask a question, I certainly have not the slightest idea what your problem is , what you are trying to do and what c++ code you have currently written. Note: When you post code [B]use code tags[/B]. Please read the forum guidelines. | |
Re: Just about everything about interest is wrong. Basics of percentages: If you wish to calculate a percentage (P) from a total (T) you need [icode] T*(P/100.0)[/icode] Your tax is 6.75 % so, you write totalTax = subTotal+tax. Now the mathematicians say there are three numbers 0,1, infinity. BUT the key … | |
Re: Well I have written a system for extracting maths equations from input text which I currently use in several projects. So I just want to ensure you know what you are getting into it is about 2500 lines of code. So structure: you can break the functions down into a … | |
Re: Have a look at the boost::any class. [URL="http://www.boost.org/doc/libs/1_41_0/doc/html/any.html"]http://www.boost.org/doc/libs/1_41_0/doc/html/any.html[/URL] If you combine it with some of the typelist ideas in the boost::mpl then you can basically do what you want. The downside is that this style of programming often looses type safety, which is "a bad thing" as it moves compile … | |
Re: Do note: that for years before 1582, leap years where EVERY 4 years. There was no 400 or 100 year rule. Prior to 325 the system was 1582 is a mess. Since they did not have a 5-14th October completely (10 days). It you are in in Britian/America it wasn't … | |
Re: Ok the ugly cin.ignore is simply unnecessary. I have no idea why you might need it, it ignores the next 10 characters of input (say 10 random letters upto a return. Very strange. The std::endl should flush cout, so it should work. If you are using an old compiler you … | |
Re: Several problems are apparent. (a) Turn is not initialized so will definitely be wrong (b) loop variable j run 0 to 3 inclusive (j<4). so you access undefined memory locations in rot. (c) rot is local only to the function rotate so the whole function is pointless. [make it global … | |
Re: The trouble is that advance does not return an iterator, AND the iterator (i in your code), has to be double derefrenced because the iterator points to a pointer and hence the error message. You need: [code=c++] advance(i,a); if ( (*i)->getName()==someVar ) { } [/code] Note I have added the … | |
Re: Ok you have (a) made a few basic errors (b) confused how map searches work and (c) made it very very difficult to read by using C like layout and constructs. I will address (c) first by writing some c++ like code (others may object but I think it is … | |
Re: I think you have made a mistake in your question. If you have 0..25 in your code your compiler will tell you that it is an error. However, if you are trying to go from a string to a number the simplest way is to use a stringstream. The problem … | |
Re: Hi, I am amazed at this code. The normal way to do a trapezium integral is simply to loop over the value like this const int N(500); const double startTime(0.0); const double endTime(100.0); double area(func(startTime)+func(endTime)); area/=2.0; for(int i=1;i<N-1;i++) { area+=func(i); } area*=(endTime-startTime)/N; What you are doing creating 100000 objects to … | |
Re: Welcome to the real world. There is not free lunch.... sorry. So (a) either attend your classes and actually pay attention. (b) decide that the class is not a good use of time and instead study some books in the library. (c) Fail your degree/course. But because this is the … | |
Re: Ok the original post was about gcc, which has a tolower function that does not return a char, it returns an int and takes an int. That is because it works with internationalized character sets. So you do a functional composite to convert the form both ways or just write … | |
Re: This forum and others see this question a lot and you get answers that range from none - > everything. So I will point out something that I have noticed, I have never studied and understood any maths that immediately looked useful, but I always seem to find that it … | |
Re: I understand for quick solutions you would follow AncientDragon's sage advice. However, that always leaves a sour taste since I am sure your want to know how to do it: So here goes [code=c++] template<typename A> struct printFunctor { static void print(FILE*,const T& Obj) { } }; template<> struct printFunctor<int> … | |
Re: I am sorry I don't like jonsca's solution, it has ugly temporary variables, that aren't necessary. The better way to do this (IMHO) is to add two operators. One is operator<< and the other is operator>> [code=c++] std::istream& operator<<(std::istream& FX,complex& A) { A.read(FX); return FX; } std::ostream& operator>>(std::ostream& FX,const complex& … | |
Re: This code is a mess, and 99% is that it is mixed c and c++ , it has not been set out tidily so neither I nor you know what is going on. Things to fix: Why use malloc to allocate memory use [icode]int* wt=new int[5];[/icode] Then remember to delete … | |
Re: Sorry but the main problem is that you have over complicated the if structure in FindFirst. Several points (i) You pass the value first BUT don't actually use it. Let me elaborate. You use a variable first BUT you don't use the value you pass. So I put the variable … | |
Re: Ok you have a number of problems: So let me (a) post some working code: (b) tell you what each line does: [code=c++] int main() { std::fstream bfile("file.bin", std::ios::in|std::ios::out|std::ios::binary); int a = 2; int var(0); bfile.write(reinterpret_cast<char*>(&a), sizeof(int)); bfile.seekg(0,std::ios::beg); bfile.read(reintepret_cast<char*>(&var), sizeof(int)); std::cout << "Variable:" << var<<std::endl; } [/code] Note that I … | |
Re: As far as I understand the standard, this is not possible [I stand to be corrected]. There are slightly upsetting work-arounds. First: If you don't need the virtual aspect, this is easy use a template return type. Easy. Second: If you require the virtual part you can then return A_1 … | |
Re: Ok I am not going to deal with all of this mess but point out some of the basic errors and then ask how did you manage to get to this point. -- Talk your first effort, [code=c++] double Individual:: SchwefelsProblem(vector <double> schwefel_population) { for (int i=0; i <schwefel_population.size();i++) square_of_sums=square_of_sums+ … | |
Re: [B]First: [/B] [B]USE CODE TAGS[/B] [B]Second: [/B]you don't not have to declare the variable name in definitions e.g. [icode]void resultsfn(int& A);[/icode] is the same as [icode]void resultsfn(int&);[/icode]. This often makes code clearer [B]Three: [/B]Dont use [CODE]system("pause");[/CODE]. [B]Four:[/B] You use references BUT the fail to use references in namefn for example. … | |
Re: First thing that comes to mind is that [icode]a[9][/icode] and [iCODE]c[9][/iCODE] are not initialized. Next you are using variables columns and row. So check that this->rows == u.columns. Then set the data in this. as c[] is just ignored. Other than that it looks ok. |
The End.