The deck of cards so it is populated with all 52 cards. Once you have populated the deck, you need to shuffle the deck. Here is a simple algorithm you might consider for this purpose:
For each card up to the middle of the deck generate a random number modulus the size of the deck, swap the current card with the card at that location done
To implement this requirement you need to implement and use the following functions:
I have written a program, in which I create a NxN-matrix. As I need some different samples (some different such NxN-matrices), I try to create a new matrix in each recall of a while-loop. The first recall of the while-loop works without any problems, but the second recall crashes, but I get any errors from the compiler. Here is my quell code. The problem should be with line 44:
imagine you have a world class. Than you have a tile class. Now, in the world class is a array with a lot of tiles and I want to initialize them with my imagination(For example I want a grass floor). But how I can do this, the array can only be create with the standard constructor. But it would be stupid and not very fast, that the tiles are first initialized with the standard constructor and than overridden to build the world. Whats the best way to initialize such a array ?
I would like to write a program that prompts the user to enter a series of client names which the program will then store in an array of structures. I'm thinking something along the lines of:
struct sInfo { vector<string> vName; float fClientHoldings; }; sInfo sClientData[100]; //create an array of structures for up to 100 client's sClientData[0].vName="Acme"; //these will be inputted by the user in my program, but I've attempted to initialize them here for simplification sClientData[1].vName="Enron";
I can't declare a structure array after I declare it. Is this true? If so, is there an alternate approach that I should be using?
I've made a code to check whether or not a save file has been created correctly, but for some reason it always returns this line: readdata[qa]=='1' as true. in which qa is the counter I use in a for loop and readdata is a character array consisting of 50 characters that are either 0, 1 or 2.
this is the entire code:
#include <iostream> #include <fstream> #include <string> using namespace std;
[Code]....
at first is also went wrong at line 22 and also returned that as true, but then I added brackets and it worked.
I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.
/* this is my terminal readout joe bob <- nothing is put into (*playerPtr).name, why not? joe bob joe bob seg fault*/ /****************************************************************/ //This is here to show my struct/playerInit
I've been messing around with loops/functions and basic logic and come up with a small maths program. Here it is:
Code: #include <iostream> #include <string> float divide (float x, float y) //function to divide numbers { return x / y;
[Code] ....
Would initializing the string 'anotherGo' to a value that makes the loop run at least once be a suitable way of doing this rather than using a do/while loop? I read that a do/while loop is a black sheep but I have come across a number of uses for it. Maybe it is just preference which one you should/could use?
In C how can I initialize a variable that is not a letter or number? For example with a number I can :
Code:
int i = 5; for ( i = 0; i <=5; i++ ); printf( "%d", i ) This would display a row of 5's
but what if I wanted to display a row of -----? What I am trying to do is read in a simple txt file, around the file I want ----1----2-----3 ect ect on the top ----a----b-----c down the side Then I want to be able to change the file at lets say position c2 and save it. This is the early stages of my attempt to set up a editable table.
I'm not finished with this code, but I am stuck on this one glitch. This is what I have so far, and it's not working.
Code: int main() { /*Please input an n value greater than zero. Otherwise, exit the program by entering a carriage return*/ printf("Please input an n value greater than zero. Otherwise, exit the program by entering a carriage return "); int summation = 0, x, y; scanf("%d", y); for (y == x; summation <= M_E && x <= 34; x++)
I am creating a class that has a private array on the heap with a constructor that takes the size of the array and initializes it on the heap. Later I have to make a deconstructor delete the space and print out free after.In my code, I was able to heap a private array and make a deconstructor, but I don't know how to take the size of the array and initialize it on the heap. My guess is this:
int* size = new int();
Also when you initialize size on the heap, don't you also have to delete it too? If so, where, in the code, do you do that? Here is my code so far.
Class Student { private: int size; int* array = new int[size]; public: Student(); // Constructor ~Student(); // Deconstructor
[code]....
How do you make a constructor that takes the size of the array and initializes it on the heap
I am currently practicing designing classes. In one exercise, I am trying to store 15 words in an array, and randomly print one (using the rand() functions and seeding it with crime. I have a header file, a respective .cpp file, and a main .cpp file. When I try to compile the code using g++ GuessWord.cpp UseGuessWord.cpp -o UseGuessWord, I get the following error in my constructor: expected primary-expression before ‘{’ token
nDataPoints and numCol are globally defined integers which get the values 120 and 13 (just about the size of those matrices).
So, A is not a problem but as soon as it gets to z and b there is a dialog "Out of Memory" and it stops at the line return HeapAlloc(_crtheap, 0, size ? size : 1); (in malloc.c).
This program debugged without any problem with same code - now I just edited the size of the matrices. And I tried the same with another library called "Eigen" and I get the same problem - so I guess there is a problem with the heap and I have to do some kind of memory allocation...
As it turns out, I cannot use method (1) when I try to initialize a private instance inside another class. But I can use it in the main() code. I hope the code below will explain what I mean. Why only method (2) will work inside another class? Or is there another fundamental mistake I am making?
(BTW: I used CodeBlocks 13.12 with GNU GCC compiler for this example)
#include <iostream> using namespace std; class CLASS1{
I'm a newbie to C++ and I am writing a code that searches strings and checks their value. I'm mostly working with if statements.
My code tends crash on Dev C++, even when I haven't recently saved any changes or compiled it, citing "memory access errors".
I recompiled in visual studio, and visual studio told me I have string out of bound errors.
My code is very long... but most of it is copy and pasted with slightly different conditions.
I believe the problem lies in how I initialize arr[4] ??
#include <cstdlib> #include <iostream> #include <string> #include <sstream> using namespace std; int main() { cout << "Hall symbol -- generator matrix program" << endl; // Setting up a data structure bc a normal array cannot handle the data for the spacegroups
Need a C++ constructor to initialize each members of an array. how to give value for for each elements of an array declared as a class object according to the users input.
I tried in internet but am unable to know the differences. I want to know the difference between those two in terms of run time and compile time. Explanation required also for below one. Is this run time or compile time? How does we actually decide which is run time and which is compile time!