1,247 Posted Topics
Re: Consider using a named constant to replace the 'magic number' 26 whic appears at several places in your code.What would you do if you need to modify your program to deal with 100-digit integers tomorrow? [code]enum { NDIGITS = 26, LAST_DIGIT = NDIGITS-1 } ;[/code] [QUOTE=;]I know that I can … | |
Re: [QUOTE=;]which is usually preffered and why? [/QUOTE] Both [icode]book::book( int, string, double);[/icode] and [icode]book::book( int data1, string name1, double data2);[/icode] are indicative of poor programming style; one could argue that the second version is the worse one because it introduces meaningless noise words. Well written code is self-documenting; the basic … | |
Re: [QUOTE=;]will c++ let you open a .exe or any file for that matter in binary mode?[/QUOTE] Yes. [quote]what would be the most efficient way of scanning thru a file on disk[/quote] Don't do any conversion to hex or any other text format; just compare the raw bytes. [code]#include <fstream> #include … | |
Re: [quote]If there was a "return", I could do it that way, but there isn't.[/quote] Create a class [icode]ElectionResults_test_driver[/icode]. Add a friend declaration [icode]friend class ElectionResults_test_driver ;[/icode] to [icode]ElectionResults[/icode]. Let the test driver examine the innards of [icode]ElectionResults[/icode] after each test case. | |
Re: First, a couple of suggestions regarding your coding style. I'm taking [icode]int GetNumber(std::string number)[/icode] as an exmple. The comments are inline, and some of them would apply to the rest of your code as well. [code=C++]// for user defined types, prefer passing by reference-to-const over passing by value int GetNumber( … | |
Re: [quote]I find the greatest common divisor of numerator and denominator.[/quote] You are right; there is no need to find individual factors at all. Just find the gcd and divide both the numerator and denominator by it. This is slightly faster: [code]inline unsigned int gcd( unsigned int a, unsigned int b … | |
Re: [icode]void(*fnctn)(void(*)(int *,void **),int(*)(void**,int*));[/icode] is equivalent to: [code] typedef void first_arg_fn_type( int*, void** ) ; typedef int second_arg_fn_type( void**, int* ) ; typedef void function_type( first_arg_fn_type*, second_arg_fn_type* ) ; function_type* fnctn ;[/code] | |
Re: [QUOTE=;][/QUOTE] [quote]This is not beyond his level. There is no reason to learn arrays before vectors. Novices should learn vectors because they convey the same concept but are "easier"[/quote] Agreed, vectors are far easier to learn, with fewer chances to make errors. | |
Re: A generalized (any uni-modal or multi-modal input sequence) version of essentially the same algorithm: [code=C++0x]#include <map> #include <iterator> #include <algorithm> #include <iostream> // C++1x template< typename INPUT_ITERATOR, typename OUTPUT_ITERATOR > void copy_modal_values( INPUT_ITERATOR begin, INPUT_ITERATOR end, // input sequence OUTPUT_ITERATOR result ) // modes to be placed in this sequence … | |
Re: You would need to search for chars in strings, extract substrings from strings, convert numbers to strings and strings to numbers. In general, most programming problems are easy to tackle if we reduce complexity by breaking up the problem into a number of smaller problems, each of which are easy … | |
Re: This would suffice for [b]char[/b] and [b]wchar_t[/b]. Also for any user defined CHAR_TYPE that has defined a [b]ctype[/b] facet. [CODE=C++]#include <string> #include <locale> template< typename CHAR_TYPE, typename TRAITS_TYPE, typename ALLOCATOR_TYPE > void trim_r( std::basic_string<CHAR_TYPE,TRAITS_TYPE,ALLOCATOR_TYPE>& str ) { std::locale loc( std::locale(), new std::ctype<CHAR_TYPE>() ) ; auto iter = str.rbegin() ; // … | |
Re: [quote]My understanding is that in C++ there are much more elegant ways of handling this.[/quote] There are. For instance, using a polymorphic call wrapper. [b]std::function[/b] (C++1x), [b]std::tr1::function[/b] (C++98 with tr1 extensions) or [b]boost::function[/b] (vanilla C++98). For example C++98 with boost: [code]#include <boost/function.hpp> #include <boost/bind.hpp> #include <iostream> struct NewtonRaphson { typedef … | |
Re: Use [b]std::bitset<>[/b], perhaps? [code=C++]#include <bitset> #include <limits> #include <string> #include <iostream> int main() { unsigned char x = 0x02 ; unsigned char y = 0x01 ; unsigned char z = 0x03 ; typedef std::bitset< std::numeric_limits< unsigned char >::digits > bits ; std::cout << bits(x).to_string() + bits(y).to_string() + bits(z).to_string() << '\n' … | |
Re: [code]#include <iostream> [b]// declare the template friend function first[/b] template< typename T > struct matrice ; template < typename T > std::ostream& operator<< ( std::ostream& stm, const matrice<T>& m ) ; template< typename T > struct matrice { //... [b]// add <> to the friend declaration in the class // … | |
Re: [quote]but would it be possible for you to modify your program to do a search for the Ps before replacing them instead?[/quote] If you had really understood the program, you would have been able to make such a trivial modification yourself. [quote]What its lacking, and perhaps the reason it executes … | |
Re: [QUOTE]don't say how to do this using usarname and password.[/QUOTE] One way is to put the username and password in the URL [B][url]ftp://username:password@hostname/[/url][/B] see: RFC 1738 [url]http://www.ietf.org/rfc/rfc1738.txt[/url] In the example cited: [CODE]//#define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS #define REMOTE_URL "ftp://username:password@example.com/" UPLOAD_FILE_AS[/CODE] Passing the username and password in plaintext is usually inadvisable; cyphertext … | |
Re: Do you mean something like this: [code]struct shape { virtual ~shape() {} virtual void print() const = 0 ; virtual double delta() const { return 1.0 ; } // ... }; struct sphere : shape { virtual void print() const { std::cout << "sphere\n" ; } // ... }; struct … | |
Re: [QUOTE]A teacher of mine told me that when an object of a class is created then six things are created. ... 1)Copy constructor ...[/QUOTE] Copy constructors etc. are synthesized (if required) by the compiler at compile-time. Objects are created at runtime. | |
Re: [QUOTE]What is the integer or double equivalent of the NULL...The computer checks the particular index in the array to know if it contains a value or not.[/QUOTE] What we are talking about here is the null in the relational database world; the SQL NULL, which essentially says that "this information … | |
Re: Since x, y are integers limited to the inteval [ -100, +100 ] there are just 200*200 == 40000 possible tuples (x,y). Just use brute force. | |
Re: If the requirement is that each derived class should have no knowledge of the relative ordering of objects of that class with respect to objects of the other derived classes (any such knowledge should be localized to that part of the program where one needs to sort these objects), a … | |
Re: Change [ICODE]s2=s2+s1[i];[/ICODE] to [ICODE]s2 += s1[i];[/ICODE] and you would get the behaviour you expect. (why?) | |
Re: [Font=large][b]Simple Random Selection Without Replacement From a Finite Population[/b][/Font] [icode]1. Let [b]P[/b] be the fraction of lines to be written into the first file ( [b]P[/b] == 0.63 => 63% ) 2. Read all the 300 or so lines from the input file into a [b]std::vector< std::string >[/b] 3. Let … | |
Re: [QUOTE]Btw we can't change main().[/QUOTE] Unless you change main, you do not have a well-formed C++ program - main must return an [B]int[/B] [CODE]class x { int a ; protected: int& cheat() { return a ; } } ; class y : public x { public: y() : a( cheat() … ![]() | |
Re: [QUOTE]To do this, I need to be able to identify the maximum value of the element in the array and its associated index. In addition, I also want to be able to label the problems with strings as opposed to the array index number. What can I do?[/QUOTE] Obviously you … | |
Re: [quote]So far, with c++, all I know how to do is create a super class called Object and make both the Sphere and Plane classes inherit from the Object class. The problem is, when I loop through my scene objects and call the findIntersection method, I get an error saying … | |
Re: [quote]But I need to separate the each digit of a double variable how I can do this?[/quote] A floating-point representations is inexact; so very often you can compute a more accurate result of an operation on real numbers in your head than your computer can do with floating-point numbers. When … | |
Re: > Can i use realloc to deallocate the memory allocated using new operator you should not. see [URL="http://www.research.att.com/%7Ebs/bs_faq2.html#realloc"]http://www.research.att.com/~bs/bs_faq2.html#realloc[/URL] as suggested, using a vector is the simplest solution. hamrick's idea has some potential problems: [code]T *dst = new T[newSize];[/code] will only work if T has accessible default initialization (a trivial constructor … | |
Re: [QUOTE]The problem as I saw it was how to return the unknown 'Object'[/QUOTE] The Java programmer's [B]Object[/B] is the C or C++ programmers [B]void*[/B]. If a Java method returns an [B]Object[/B], you need to cast it to the correct type before you can do something useful with it. The closest … | |
Re: [quote]but a bit less practical cause i dont know the exact number of arguments[/quote] One option is to use the boost preprocessor metaprogramming library. [url]http://www.boost.org/doc/libs/1_45_0/libs/preprocessor/doc/index.html[/url] For example, [b]file /tmp/pp.h[/b] [code]#include <boost/preprocessor.hpp> #include <boost/any.hpp> #include <vector> #define repeat_this( place_holder, N, CNTR ) CNTR.push_back( boost::any( Argument ## N ) ) ; #ifndef … | |
Re: [QUOTE]I need to store each path in a tree as a row in a two dimensional array of size #of paths x N. I couldn't find the appropriate indexing scheme for the array.[/QUOTE] Could you clarify your question? Is it a binary tree or an n-ary tree? What do you … | |
Re: [QUOTE]I have a text file with characters. I must able to read the length of characters in the file by every line and count the frequency of each characters for each line. I am still new for c++.[/QUOTE] Simplify the problem by breaking it up into two smaller sub-problems: [B]a.[/B] … | |
Re: You might want to have a look at Kernighan and Pike's 'The Practice of Programming'. [url]http://www.pearsonhighered.com/educator/product/Practice-of-Programming-The/9780201615869.page#takeacloserlook[/url] | |
Re: Send a large file across the network and measure the average Round Trip Time. TCP Throughput = TCP Recieve Window Size / Round Trip Time eg. if TCP Recieve Window Size is 64K, RTT is 100 ms, TCP Throughput = 64K * 8 / 0.1 = 5242880 bps = 5.2 … | |
Re: Please post the code for [ICODE]void MainWindow::on_btnStart_clicked()[/ICODE]. It is around line 31 of [B]mainwindow.cpp[/B] From the error message, you are trying to copy an object of type [B]Producer[/B] somewhere in that function and a copy constructor for [B]Producer[/B] can not be synthesized (because the base class copy constructor is private). | |
Re: First create a user defined type to represent 23 zero bits and 1 one bit. [CODE]#include <cstdint> // C++1X (or <stdint.h> for C99) struct pattern { enum { NBYTES = 3 } ; std::uint8_t bytes[NBYTES] ; // 3 x 8 bits == 24 bits };[/CODE] Then provide mechanisms to read … | |
Re: [QUOTE]Is there any way to use something like get/getline without providing an argument for storage variable?[/QUOTE] If the type involved has default or trivial initialization, you could write such a function yourself. For example: [CODE]template< typename T > inline T get( std::istream& stm ) throw (std::runtime_error) { T temp ; … | |
Re: [QUOTE]I think we must creat every operator for this class like: +, -, *, /, %, pow(x,y),...[/QUOTE] Just two would suffice. [CODE]struct INT { INT() {} INT( int value ) : builtin_int(value) {} operator int& () { return builtin_int ; } operator int () const { return builtin_int ; } … | |
Re: [QUOTE]> Considering quadratics of the form: > n² + an + b, where |a| < 1000 and |b| < 1000 > Find the product of the coefficients, a and b, for the quadratic expression that > produces the maximum number of primes for consecutive values of n, starting with n … | |
Re: [QUOTE]that shuffles the array and is straight-forward and pretty easy to implement, so that's a good solution.[/QUOTE] It is straight-forward and pretty easy to implement, but it is not a correct solution. There are [B]N![/B] ways of arranging a sequence of [B]N[/B] elements. A random shuffle should yield uniformly distributed … | |
Re: [QUOTE]Where they are getting information for USER , %CPU , %MEM & TIME+ in Linux[/QUOTE] From [B]procfs[/B] mounted at [B]/proc[/B] See: [url]http://en.wikipedia.org/wiki/Procfs[/url] Source for procfs utilities: [url]http://procps.sourceforge.net/[/url] | |
Re: [QUOTE]but I am not sure the Input/Output chapter covers everything I will need to know.[/QUOTE]Any reasonable book should cover all that you need about file i/o for this task. Basically, you just need to be know how to read and write bytes (or sequences of bytes). [ICODE]Dictionary<int, int>[/ICODE] would be … | |
Re: Essentially you just need to implement a 2D polygon clipping algorithm which can handle polygons that are not necessarily convex. Greiner-Horman algorithm: [url]http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.3266&rep=rep1&type=pdf[/url] Vatti clipping algorithm: [url]http://books.google.com/books?id=fGX8yC-4vXUC&pg=PA98[/url] | |
Re: [QUOTE]can I happily assume that the C++ language will handle that detail correctly without my intervention?[/QUOTE] Yes. The implementation is responsible for setting the vtable pointer correctly during construction. Just make sure that you do not treat C++ objects as 'raw bits'. For example: [CODE]struct A { virtual ~A() {} … | |
Re: [QUOTE]//This doesn`t workbecause of const problem...[/QUOTE] [CODE]template <int p> bool FComapare (Node *lId, Node* rId) ; [/CODE] Here, [B]p[/B] must be a [B]constant known at compile-time[/B]. [CODE]for (int [COLOR="Red"]i[/COLOR] = 0;i < partNum; ++i) { //This doesn`t workbecause of const problem... set<Node *, bool (*)(Node*,Node*) > f(&FComapare<[COLOR="Red"]i[/COLOR]>); m_F.push_back(f); }[/CODE]Because [B]i[/B] … | |
Re: When the loop [CODE]while(getline(ErrorFile,line)) { }[/CODE] exits, the stream is in a failed/eof state. When a stream is in an error state, all operations on the stream will fail. You need to first clear the error state before you do anything more with the stream. For example, in the following … | |
Re: A vector of pointers? [CODE]struct student ; struct subject { // ... std::vector< student* > list_of_students ; // ... }; struct student { // ... std::vector< subject* > list_of_subjects ; // ... };[/CODE] | |
Re: the xor swap algorithm fails if you try to swap a variable with itself. other swap algorithms using arithmetic operators fail if overflow or underflow occurs. the correct way to swap two variables a and b of [B]any[/B] type is std::swap(a,b). we would expect std::swap to be correct and to … | |
Re: [code=cplusplus]#include <iostream> #include <locale> #include <iomanip> using namespace std ; struct comma_facet : public std::numpunct<char> { protected: string do_grouping() const { return "\003" ; } }; int main() { cout.imbue( locale( cout.getloc(), new comma_facet ) ) ; cout << fixed << setprecision(2) << showpoint << 123.0 << '\t' << 12345.6789 … |
The End.