C++ :: Recursive Function - Creating Vector Of Every Unique Combination Of Commands
Mar 24, 2013
I'm trying to write a recursive function that takes in a vector of strings that contains
"1 forward", "2 forward", "rotate left", "2 backwards" ... etc
how can I write recursive function that creates a vector of every unique combination of commands?
View 8 Replies
ADVERTISEMENT
Mar 26, 2013
Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>
How do I make a function that creates vector of vector of every different integers?
<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>
and so on...
View 2 Replies
View Related
Apr 9, 2013
I am trying to create a recursive function that i can call on in order to take a user inputed base and exponent and give final answer this is what i have but im completely lost after this i dont even know how to continue. What i have so far
#include <iostream>
using namespace std;
int Exp(int x,int y){
if(base <= 1 || exp == 0) return 1;
if(exp == 1) return base;
int main(){
int number, exp;
[Code] .....
After i set the base situations im not sure how to get the function to make the function take the base to the exponent recursively.
View 3 Replies
View Related
Aug 10, 2013
For example let say there is vector [1,2,3,4,5,6,7,8,9] and let say there is int n
if i say n=3
I want to find all possible 3 combination
[1,2,3][1,2,4].....such and such
if i say n=4
[1,2,3,4][1,2,3,5]......
I want to know how you write the code depend on the input n. Is it call dynamic programming?
View 4 Replies
View Related
Jul 15, 2013
I have N vectors which look like this:
[1→m] [m+1→2m] [2m+1→3m] [3m+1→4m] [4m+1→5m]..... [{(N-1)m}+1→Nm]
I want to select 1 element from each vector without duplication of any combinations. Essentially only when all combinations are done with 1st element in first vector ,only then it should move to next element in first vector.
Say i have elements :[123] [456] [789]
my combinations should be like
147
148
149
157
158
159
167
168
169
247....
Also, I cant have any repetitions and only after all combinations of 1 are done only then the loop has to move to next combination ie 247 combination and so on.
I tried NCK (n choose k) command but it gave me random combinations. How should i go about it with using minimal for loops?
View 2 Replies
View Related
Dec 16, 2012
I'm using CStatic inherited class in my code and creating static text boxes dynamically. Now for assigning unique control ID I'm creating a static control from resource editor and destroying it before calling CStatic::Create() and using its control ID say ID_STATIC_SAMPLE.
If I do not use this parameter of Create(), the static control is also being created, so what is the use of this unique ID. And any other better way to assign a ID for dynamically created static controls.
View 3 Replies
View Related
Dec 29, 2013
I am trying to seed a vector with 52 values ranging from 1 to 52. I already have the code written to seed the vector bu how to keep from repeating the same values from being seeded. This is for a five card stud game.
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<vector>
#include<ctime>
using namespace std;
[Code] ....
View 3 Replies
View Related
Dec 6, 2013
if i can use a function to use unique for my class and also i want to count how much each of them is duplicated. i mean i have (420,250,420,66,444,777,250) in my list i would like to know that 420 is duplicated 2 times and also 250. Is there a way to get this result ?
list<Patient *> ListePatient
ListePatient.unique(g_nas()); // nas is a attribute of the class patient
View 2 Replies
View Related
Nov 15, 2014
I was trying to implement own vector class and wrote some code. Below is the code.
Code: #include<iostream>
#include<vector>
using namespace std;
template <typename T> class MyVector
{
T *mem;
int m_size,final_size;
public:
MyVector() : final_size(1),m_size(4)
{
[code].....
I have question on this function.
Code: myVecPush_back(T t){}
Here I was able to put elements upto any number of time, but I have allocated memory to only 4 elements in T *mem.
My question, why the program is not crashing when I tried to put elements more that 4?
Is since the variable is of type Template? Any specific reason for this?
View 2 Replies
View Related
Sep 10, 2014
Is it possible to create a temporary
std::list of pointers
I would like to pass a temporary
std::list
to the constructor of a class to initialize its own one.
For example, using a
std::vector
:
#include <iostream>
#include <vector>
void func(const std::vector<int*>& myVec) {
for(int i=0; i<myVec.size(); ++i){
[code]....
Can we do this? What are other possible problems in addition the ones I have just mentioned above?
View 14 Replies
View Related
Nov 27, 2014
How do I change the following recursive function into a iterator function and basically what should it look like?
int f(int i) {
if(i<2)
return i;
return f(i-2)+f(i-1);
}
View 1 Replies
View Related
Nov 29, 2014
I had the following question in my exam and received 3 out of a possible 4 marks
Here is the question:
Assume the following recursive function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}
Translate this function into an iterative version:
Here is my solution:
int f(int i)
{
int prev,next,final;
prev = 0;
[Code]....
View 9 Replies
View Related
Nov 30, 2014
I am trying to translate the following recursive function into its iterative function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}
This is what I tried to do:
int f(int i)
{
int prev,next,final;
prev = 0;
next = 1;
if(i==1)
final = i;
[Code] .....
View 1 Replies
View Related
Nov 30, 2014
I am trying to translate the following recursive function into its iterative function:
int f(int i) {
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}
This is what I tried to do:
int f(int i) {
int prev,next,final;
prev = 0;
next = 1;
[Code] ....
View 1 Replies
View Related
Apr 23, 2013
Creating a Vector Array + Reading a file ....
View 1 Replies
View Related
Jan 3, 2013
In a numerically intensive code, I have a Cartesian vector class Vector3d which has the normal operator overloading and basic functions such as dot product, magnitude, etc. For simplicity, assume that it is not a templated class and its components are of type double.
I frequently need large 1-d arrays (e.g. stl vectors) of Vector3d. Two use-cases must be satisfied:
1) The trivial case in which the data are stored as stl vectors of Vector3d;
2) The more difficult case where the individual components are stored as stl vectors of double, and are not guaranteed to be contiguous in memory (so one cannot rely on "stride").
Assuming the array lengths are all identical, I'd like to be able to access both types in a single loop. The straightforward way for case 2) is to construct a temporary Vector3d from the three components (indexed with the loop index). However, I would prefer not to incur the overhead of the constructor.
Is it possible using template metaprogramming. Ideally I'd like a CompositeVector3d that inherits from Vector3d and is constructed with the component vectors, but can be dereferenced using the loop index in the same way as one would do with case 1.
I am not looking for the typical template metaprogramming utility of being able to operate on the entire array without explicit loops. I just want the syntactic "sugar" whereby CompositeVector3d and Vector3d act the same, plus the avoidance of the cost of the constructor. I am not averse to using an additional templated class (perhaps a Field or a View class) to access the basic storage in both case.
Is it possible to do this without using a full template metaprogramming utility?
View 1 Replies
View Related
Nov 12, 2014
In my problem I am to create a base class to represent a four vector (a concept in physics involving a four dimensional vector) then create a derived class specifically to represent the four momentum of a particle which inherits from the base class. I have been supplied a small piece of code to use to generate a 'random' x y and z component of the momentum magnitude. The code is as follows
#include <cstdlib>
double triangular(double momentum){
double x, y;
do{
x = momentum*rand()/RAND_MAX;
y = x/momentum;
} while (1.0*rand()/RAND_MAX > y);
return x;
}
It is said in my problem that this code is supposed to generate the magnitude, and then randomly split into x, y and z components. This code returns a single value and so I cannot see how it is doing what it says in the problem.
View 2 Replies
View Related
Apr 12, 2014
I'm currently writing a chunk of code that will take inputs from the user and push them into a vector until 0 is entered, at which point it will break the loop and continue on with the rest of the program. This is nothing I haven't done before, but I have never encountered this error.
The code chunk looks like this:
typedef vector <int> ivec;
int main() {
ivec nums;
int input;
while (true) {
cout << "Enter a positive integer, or 0 to quit" << endl;
[Code] ....
My standard testing input has been 3 5 6 3 8 (then 0 to quit), so one would expect my sequence to be 3 5 6 3 8...but instead after the 8 I get a random number value that is usually quite large and I cannot figure out where it comes from (ex. 3 5 6 3 8 201338847).
View 9 Replies
View Related
Nov 16, 2013
The recursive function is bolded, i got feedback and was told that the static variable made the function seem a lot like a iterative function but he did not say why.
Code:
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char string[MAX]);
int checkRecPalindrome(char string[MAX]);
[Code] .....
View 7 Replies
View Related
May 5, 2014
I'm writing a program that starts at a given number n and adds all the way to 2:
n + (n-2) + (n-4) + (n-6) + .....
The following is my code, it compiles but after I enter an integer the program crashes.
Code:
#include<stdio.h>
#include<stdlib.h>
int sum_even(int n);
int sum_even(int n){
if(n==1){
[Code] ....
View 11 Replies
View Related
Nov 9, 2013
the functions checks if the word is a palindrome like"level" "madam" etc. but with input "dfdfdfdffdfd" my recursive function fails.
Code:
/* main.c - Created on: Nov 9, 2013 - Author: Kaj P. Madsen*/
#define MAX 100
#include <string.h>
#include <stdio.h>
int checkPalindrome(char checkString[MAX]);
int checkRecPalindrome(char checkString[MAX], int strLgt, int a);
}
[code]....
results from "dfdfdfdffdfd" added some print to see that its the variables a and strLgt not functioning properly
Code:
dfdfdfdffdfd.
The word is not a palindrome(iterative)
strLgt: 11 a: 0
a: d strLgt: dstrLgt: 10 a: 1
a: f strLgt: fstrLgt: 9 a: 2
a: d strLgt: dstrLgt: 8 a: 3
a: f strLgt: fstrLgt: 7 a: 4
The word is palindrome (recursive)
View 4 Replies
View Related
Jan 5, 2015
I am trying to make from f_rec (recursive function) to f_iter (iterative function) but I can't.
(My logic was to create a loop to calculate the results of f_rec(n-1), another loop for 2*f_rec(n-2) and one loop for f_rec(n-3);
But I'm wrong)
int f_rec(int n) {
if(n>=3)
return f_rec(n-1)+2*f_rec(n-2)+f_rec(n-3);
[Code] .....
I also think that my run time for the f_rec is 3^n ...
View 2 Replies
View Related
Feb 9, 2013
I am just practicing some recursion and I am having trouble with printing out a recursive function in main. Here is what I have:
Code:
// This function adds the squares 4, 5 = 4*4 + 5*5 recursiveley
int recursive_sumSquares(int m, int n) {
if (m < n) {
return m*m + recursive_SumSquares(m+1, n);
}
else {
return m*m;
[Code]...
I am getting an error that says undefined reference to 'recursive_SumSquares'
View 2 Replies
View Related
Jun 10, 2013
I am working on a problem that requires a nest for loop to be converted to a recursive function. I am going to provide just the code instead of the entire program.
Code:
for (R1=1; R1 <+3, R1++){ //for loop
printf (something);
}
// the recursive function
void loopR1 (int R1, int max){
if (R1 <= max){
printf (something);
[Code]...
when calling the recursive function in main i am using the following statement...
loop r1(1,3)
View 4 Replies
View Related
Jan 23, 2013
this function has to check if an array is not descending it has to be recursive and it's space complexity has to be O(log(n))
Code:
int is_sorted(int a[ ], int n) {
int check=n-1;
if(n==1 || n==0) return 1;
if(a[check]<a[check-1])return 0;
else return(is_sorted(a,n-1));
}
this is what i came up with. i know it's space complexity is O(n) how do i make it O(log(n))?
View 2 Replies
View Related
Oct 31, 2014
I am trying to learn so much C as possible by my own. I have learned a bit already and made my first game. I made a tictactoe with a 3x3 board that works great. But now i want to make it a NxN-board. But the problem right now is my checkwinner-function. I really don't know how I should check the diagonal for winner in the for loop. Earlier I have checked the diagonal manually like you can see down there.
Code:
for (Row = 0; Row < BOARDSIZE; Row++) {
if ((game[Row][0] == 'X' && game[Row][1] == 'X' && game[Row][2] == 'X') ||
(game[0][Row] == 'X' && game[1][Row] == 'X' && game[2][Row] == 'X') ||
(game[0][0] == 'X' && game[1][1] == 'X' && game[2][2] == 'X') ||
(game[0][2] == 'X' && game[1][1] == 'X' && game[2][0] == 'X'))
return 1;
I think I need two foor-loops in each other too and need to check the spots in the board like 0,0, 0+1,1, 0+2,2. Is that even possible?
View 3 Replies
View Related