2,712 Posted Topics
Re: Whats the general form? From that example, you can do something like this : [code] unsigned int var1 = 11; long var2 = 5; long var3 = 3; unsigned int final_var = var1 &0x3; final_var <<= 2; final_var |= (var2 & 0x01); final_var <<= 1; final_var |= (var3&0x1); [/code] | |
| |
Re: Can you gives us a picture or something that shows how the output should be? | |
Re: Virutal inheritance comes into play when you inherit from two classes and both of those class inherit from the same base class. Its called the diamond shape inheritance. Here is an example : [code] struct Base{ }; struct A : Base{ }; struct B : Base{ } struct C : … | |
Re: I don't think thats possible. Obviously, you will have to iterate over all the elements to reverse the order. I think the best you can do is : O(log(n)) | |
Re: Do something like this : [code] int nearSqrt(int num){ int n = sqrt(num); return n*n; } [/code] That returns the nearest square number. For it to be greater than num,you need to return (n+1)*(n+1); | |
Re: put the "cout << endl; " at the end of the first for loop. So do this : [code] for(i=1;i<5;i++) { for( j=1;j<6; j++) { infile>>random[i][j]; cout<<random[i][j]<<endl; } cout<<endl; } [/code] | |
Re: [URL="http://www.cplusplus.com/reference/clibrary/cmath/pow/"]This [/URL]link shows the function prototype that pow function takes. Make sure you match one of the function prototype. So for example this code : [code] pow(2,2); [/code] will not work since pow does not overload pow(int,int) . But this any of these will work : [code] pow(2.0,2.0); pow(2.0,2); pow(2.0f,2.0f); … | |
Re: First correct me in this if I'm wrong. For sum = 0 , return 0; For sum = 1 , return 1; For sum = 2 , return 2; For sum = 3 , return 3; For sum = 4 , return 5; For sum = 5 , return 8; … | |
Re: use vectors. Here is an example : [code] #include <iostream> #include <fstream> using namespace std; int main(){ vector<float> inputValues; ifstream file("num.txt"); while(!file.eof() || !file.fail()){ float num = 0.0f; file >> num; inputValues.push_back(num); } } [/code] | |
Re: [QUOTE=myz068u;1184716]Please forgive me, but I don't seem to follow... Could you care to explain more?[/QUOTE] 1) start a loop 2) ask for input 3) use the switch 4) continue with loop until exit command entered >>start a loop [code] while( true ){...} [/code] >>ask for input ask for input inside … | |
Re: This code : [code] cout << "Enter 1 to create an account : "; int nEntered; cin >> nEntered; for(;;) { if( nEntered == 1) { BankAccount s; cout << " Bank account created\n If you would like to make a deposit press 1"; break; } else { cout << … | |
Re: You access each individual numbers and multiply them : [code] int Array[2] = {1,1}; int Array2[2] = {5,5}; int result[2] = {}; result[0] = Array[0]*Array2[0]; result[1] = Array[1]*Array2[1]; [/code] Of course there is a lot more to this, but you get the basic idea. | |
Re: This : [code] char array2[arraySize2] ={'A','B','E','F','G','H','J','K','N','O','P','Q','R','S','T','U','W','Y','Z'}; for (int i = 0 ; i < str.length() ; i ++) int b = str.find(array2[i]); [/code] can be replaced with this : [code] if( str.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == string::npos){ letter not found. } [/code] but it seems that you are trying to find the number … | |
Re: There are more difference. For instance, when an object gets destroyed, its destructor is called. Whereas when an object gets destroyed, its member function might not be called. Also a destructor has no return type where as an member function could. Looks like what your confused about is polymorphism. Take … | |
Re: Also don't use magic numbers. Use constants. For example : [code] enum FuncReturn{ ALL_DIGITS , INVALID_INPUT }; int myCheck(const string& str){ if(str.find_first_of("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz") != string::npos) return INVALID_INPUT; else if(str.find_first_of("!@#$%^&*()-_+=';[]{}:<>?/.,`~") != string::npos) return INVALID_INPUT; else return ALL_DIGITS; } int main(){ FuncReturn f; f = myCheck("123123143431a"); //.... } [/code] | |
Re: Look at it in our eyes. You talk a little and dump a whole *lot of code on us. Make it simpler for us to help you by shows only the relevant code and stating the problem correctly. I have a hunch, that you are not getting the theory behind … | |
Re: Its pretty close. You had the right idea to use a for loop and use isdigit function. You just didn't do it correctly. Here are basic steps you can try : 1) Get input into a string 2) Create a for loop from i = 0 to string.length 3) use … | |
Re: what do you think ? Don't you have a compiler ? Whats confusing you ? So many questions( including yours ) but no answers? | |
Re: [QUOTE=some;1177975]In my c++ book it shows how to do it differently. I'll just post that up to so the person who asked can have more options in choosing what he wants to do. I'll use an example to explain it. Suppose you want to display num which stands for 4.91877 … | |
Re: Nope. What you did makes no sense. A char can only contain 1 char. Use std::string instead. | |
Re: You didn't define a operator+, that takes an int. You only defined a operator+ that takes another Polynomial. | |
Re: If your function is write, and it writes to the screen, then just templatized it. | |
Re: Just for emphasis, an automatic variable is deleted with it reaches the end of its block. So for example : [code] int main(){ { int x = 0; } //x is deleted when it reaches this bracket int x = 3, y = 3 } //x and y is deleted … | |
Re: >>[B]I don't believe you can call class functions without an object.[/B] It can be done. [CODE] #include <iostream> #include <string> using namespace std; struct Print { static void print(string str) { cout << str << endl; } }; int main(){ Print::print("string"); } [/CODE] But from the looks of it, using … | |
Re: >>[B]info[cols_*row + col];[/B] I don't think this conversion is right. Assuming you are using a 1D array. Then our 1D array indices is like so : 0 1 2 3 4 5 6 7 8 9 10 11 Your conversion equation is " row*col+col ". Say we call matrix(1,1); That … | |
Re: You need exactly what you said here "std::cout << "Haha, this is a loop";" You need a loop. Here is an example of for loop. [code] #include <iostream> using namespace std; int main(){ const int MAXLOOP = 100; for(int i = 0; i < MAXLOOP; ++i){ //loop 100 times cout … | |
Re: Nope. That's the limitation of the linked list. You need to transverse the list to get to a node. Whereas with arrays, you can access the data by indexing. But using linked list, you can delete and insert element very fast, O(1) to be exact. | |
Re: Close but you still did not need to program for this problem. We have this wonderful formula : [I][U] Fib(n) = round( Phi^n/√5)[/U][/I] We can manipulate the above equation to get the number of digit we are looking for by using logarithm. Fib(n) = log( phi^n / phi(√5) ) Lets … | |
Re: >>[B]can someone show me an example of how you would initiate a variable. [/B] [code] int variable = 0; //declare and initialize the variables [/code] >>[B]how you would fix a undeclaired identifiers.[/B] an undeclaried identifier means that you used a variables that has not been declared. For example : [code] … | |
Re: Yep, I think you need to include sstream as well. stringstream is defined in the sstream header file. | |
| |
Re: [code] void turn_it_is(int play1, int play2) { cout << (((turn_it_is%2)==1)?(play1:play2)); cout << " it's your turn "; cin >> turn_it_is; } [/code] What are you doing? "turn_it_is" is a function. Not a variable!. Why are you trying to use cin >> turn_it_is ? and also why are you trying to … | |
Re: Most likely, you did not initialize your variables. Also you probably might be writing into memory that you are not supposed to. Because, in debug, the compiler pads the memory more than in release. | |
Re: Do something like this : [code] #include <iostream> #include <ctime> //needed for time int main(){ srand( time(0) ); //seed random number generator string name[3] = { "luke" , "tony", "robert" }; string randomName = rand() % 3; //return a number from [0,3) } [/code] | |
Re: I like the second one, if it would compile :) And why are u incrementing m two times per a loop. | |
Re: The function prototype : [code]int operator +(Polynomial &Poly1, Polynomial &ConstValue);[/code] The function definition header: [code]int operator +(Polynomial Polynomial1, Polynomial ConstValue)[/code] | |
Re: If i'm not mistaken, you should be able to return the vector iterator. Maybe an example will help. [code] #include <iostream> #include <vector> #include <string> #include "print.h" using namespace std; class Items{ public: typedef std::vector<string> ItemType; typedef std::vector<string>::iterator Iterator; typedef std::vector<string>::const_iterator constIterator; private: ItemType _weapons; ItemType _potions; public: Items(){ _weapons.push_back("sword"); … | |
Re: There are a couple of ways you can do this. First you need to create a BigInteger class. It basically adds and subtracts arbitrary long digits. One way to do this is to use a string to hold the number, internally. Then create an add function, that adds one BigInteger … | |
Re: If you need optimization that badly, then make a look up table with cosine and sinus table. | |
Re: Not quite sure what you mean, but maybe this will help you : [code] switch(option){ case 1 : doCase1(); break; case 2 : doCase2(); break; case 3 : case 4 : doCase3And4(); break; default : break; } [/code] The idea is that if the user picks 3 or 4 it … ![]() | |
Re: string concatenation. If you're saying "...default***/test.txt" is the path, where the user picks what goes in ***, then you can achieve this with string concatenation. For example : [code] string startPath = "...//default"; string endPath = "//test.txt"; string pathNumber = ""; string finalPath = ""; cin >> pathNumber; if(pathNumber != … | |
Re: >>[B] error C2662: 'Number::operator -' : cannot convert 'this' pointer from 'const Number' to 'Number &'[/B] That means you are passing a const object into a non-const function. Const-correct your functions, that is do something like this if you can : [code] Numbers operator+(const Number& num)const;[/code] | |
Re: your idea is fine. Just don't use stl containers. use regular arrays if you wan't speed since this is all about speed. | |
Re: 1) use code tags. `/* code goes here */` 2) Whats your problem. We aren't compilers. We don't just compiler the code in our head and find errors, although we might be able to. 3) `for( int n = 0; n!= 100; n++) re_read_Steps1And2();` 4) Fix those things and post … | |
Re: This would be just like adding regular numbers, but instead of carrying when the addition of 2 digits gets 10 or bigger, you will carry when the addition of 2 numbers get 2 or bigger. First now how to add binary numbers in paper. When you try to add binary … | |
Re: sure : [code] class A{ public: const static string ClassName; //declare }; const string A::ClassName = "A";//initialize [/code] | |
Re: *(A+0) == A[0]; *(A+1) == A[1]; *(A+2) == A[2]; //... *(A+n) == A[n] | |
Re: [URL="http://www.play-hookey.com/digital/converting_ff_inputs.html"]Here, [/URL] less work for me to explain. |
The End.