C++ :: How To Turn Recursive Into Iterative

Jul 10, 2014

map< int, int > cache;
int count( int n ){
if( cache[n] != 0 ){
return cache[n];

[Code] ....

I don't know how to turn this recursive function into an iterative...

View 7 Replies


ADVERTISEMENT

C++ ::  From Recursive To Iterative Function

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

C++ :: Turning Recursive To Iterative Form?

Jul 21, 2013

So I have a code like this one below :

void get_sum( INNER_ID id, vector<INNER_ID>& dont_check ) {
vector<INNER_ID> below = get_below( id );
vector<INNER_ID>::iterator second;

[Code]....

I use this algorithm for my "crappy" physic engine, so the point of this algorithm is to get the sum of mass below an object. get_below( id ) function can get the ids of what object is below them.

But before I need ids of the object below them to apply impulse, force, and some other physic stuff.

One object doesn't neccesarrly rest on top of one object, it can rest on 2 object or more.

when I look at it, it resemble a tree, maybe it's not. I just don't really know very much about tree algorithm

I cannot optimize a recursive code so I think, I better turn this into an iterative but I cannot seem to find a way to do that

View 4 Replies View Related

C/C++ :: How To Programmatically Find If The Code Is Recursive Or Iterative

Apr 8, 2014

Is there any way to programatically find if the given code is taking recursive approach or iterative apporaoch using concept of files in C programming.

View 3 Replies View Related

C++ :: Turn Based Game Turn Order?

Feb 14, 2013

How would one cycle through a turn order in a turn-based game? I was thinking an array of every creature (including the player) and have a pointer to the array++ after the turn, but I couldn't put all the objects into an array.

View 2 Replies View Related

C++ :: BST Iterative Insert For Binary Search Tree

May 13, 2014

I'm writing the function as described in the title but it isn't quite working. It works as long as the value passed is less than the parent (going left) but when the value should be placed to the right, it doesn't actually insert the node.

template <typename T>
void BST<T>::insertHelper(BST<T>::BinNodePtr &subRoot, const T& item) {
BinNode *newNode;
BinNode *parent;
BinNode *child;

[Code] ....

FYI, I've commented out setting the children of the new leaf to NULL because the constructor already does that.

View 1 Replies View Related

C++ :: Iterative Loop Repeats More Times Than Expected

Nov 28, 2014

I tried my best but I can't figure out the problem. At the last part of "createArray", I output the final array created. I mean it to repeat once but then it repeat more times than I expect. createArray is an iterative function. If it repeats 3 times, than at last the array created which fulfil the criterion would be printed out 3+1 times.

I am trying to create an array with 3 numbers 5 times, resulting in a 2D array. The 3 numbers in a array are picked from 0 - 5. I enter createArray(5,3,5). Then these 5 arrays are compared with each other to see if there are repetitions. If there are, the whole process begins again, 5 arrays with 3 numbers each will be picked again and compared with each other. If there are no repetitions at last, there 5 arrays would be printed out.

void deleteArray(int** array){
delete[] array;
}
int** createArray(int simu_times, int randomrun,int numberofrun){
vector<Int_t>fChosenRun;
int** Array = new int*[simu_times];

[Code] ....

View 8 Replies View Related

C++ :: Binary Search Tree (Iterative Function To Insert)

Jun 23, 2013

I was studying BST and tried to make a iterative function to insert, the original recursive function is the following:

void insert(node *&tree, int value) {
if (!tree) {
tree = new node;
tree->num = value;
tree->left = tree->right = NULL;

[Code] ....

And the code that i did is (but doesn't work):

void insert(node *&tree, int value) {
if (!tree) {
tree = new node;
tree->num = value;
tree->left = tree->right = NULL;

[Code] ....

I don't see where the error is or why it doesn't work.

View 6 Replies View Related

C :: How To Turn Up Error Checking On IDE

Dec 8, 2013

The reason being is that it says that my program is right

Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define NFlights 10
struct date {
int month;
int day;
int year;
int hour;
int minute;

[Code] ....

View 6 Replies View Related

C/C++ :: Including All Repetitions For One Turn

Feb 22, 2015

I am trying to include all repetitions for just one turn but I keep getting

00.0 but I want (the one in red)
170.3 0 0.3
180.0 17 0.0
190.3 18 0.0
200.3 19 0.3
210.0 20 0.3
220.0 21 0.0
22 0.0

so basically I call a function that represents just one turn of getting a random number, and then when the player decides he wants to get a random number that is at least 17 and wants to repeat this 3x I have to print out this chart that shows the chances of the player rolling the numbers between 17-22 [how many times does he get 0,17,18,19,20,21,22] this is what I have

cout << "your score: " << (' ') << "chances for that score:" << endl;
/* score is the player's total score for the one turn */
int score = 0;
int score0 = 0; // 0

[Code]....

View 3 Replies View Related

C :: How To Turn User Input Into 2D Array

Mar 28, 2013

Say you the user inputs x number of names and then is to put in x amount of values for each name. How would you display these values in a 2d array and be able to add the values for each row which will represent each name?

View 5 Replies View Related

C++ :: How To Turn String Integer Into Array

Jul 23, 2013

int main () {
string integer1;
string integer2;
cout <<" enter your first number: " << endl;
cin >> integer1;
cout << endl;
cout << integer1 << " is your first number" << endl;
}

Now how do I turn the string integer into an array?

View 5 Replies View Related

C++ :: How To Turn A String Word Into Letters

Dec 9, 2013

I'm having trouble trying to turn a word into letters. I've seen other posts but they deal with a sentence and it only outputs the words only. What I want to know is how do they take a word (Ex: "word") and break it into individual letters, where I want to store them in a vector of string?

If it's not too much trouble, I would prefer without using pointers or "std:: " marks, since I am trying to avoid pointers and I'm using "using namespace std" all the time.

Ex:

In the example "word", it should output into:

"w"
"o"
"r"
"d"

and I will push them back into a vector of string where each vector element contains a letter.

View 2 Replies View Related

C :: How To Take String Of Numbers And Turn It Into Array Of Integers

Mar 2, 2014

I know strings are essentially just arrays of characters, so what would be the easiest way to take each individual digit and put it into a separate space in an array?

ex.) *str = "90210"

array[0] = 9
array[1] = 0
array[2] = 2
array[3] = 1
array[4] = 0

All my attempts at achieving this just result in an array full of garbage numbers. What I've done is

Code:
int *array;
array = malloc(sizeof(int)*(strlen(str));
for(i=0; i<strlen(str); i++) {
array[i] = str[i]
}

also, I should mention that the string will be defined in main, and its converted into an array in a separate function.

View 2 Replies View Related

C++ :: How To Make A Loop That Keeps Track Of Whose Turn It Is And Their Score

Dec 11, 2013

[URL] ....

This is my code, but how can I implement this?

View 1 Replies View Related

C++ :: Turn Based Game - How To Make It Cross Platform

Sep 12, 2014

I am starting a turn based battle (similar to pokemon) app. How could i make this and make it cross platform. Also is it possible to make it access gps and allow other devices with the same app communicate with each other?

I have done things on the command line but i never made anything with images so i dont even know where to start for this app.

View 4 Replies View Related

C/C++ :: How To Write A Function For A Single Turn Involving Die In A Game

May 16, 2014

So basically it consists of implementing a single turn for the game called 'pig' and printing out scores and probabilities of those scores. So this is what I have thus far :

int randomNum (int min, int max) {
return min + rand () % (max - min + 1);
} int singleTurn (int holdValue) {
int totalRoll = 0;
int score = 0;
do {
score = randomNum(1,6);

[code]....

View 13 Replies View Related

C++ :: Two Player Strategic Battle Turn Based Game - Assigning Controls

Nov 1, 2014

I am making a game which is a two player strategic battle turn based game..... The game will require each player to choose a attack. What would be the best key configuration for set of four attack. Like

player 1: 1,2,3,4;
player 2: 7,8,9,0;
where 1&7 are for kick 2&8 are for punch etc.....
or
player 1: q,w,e,r;
player 2: u,i,o,p;

View 2 Replies View Related

C++ :: Turn Binary File Data Into Unsigned Character Array For Inclusion In Executable

Jul 10, 2013

So I wrote a program to turn a binary file's data into an unsigned character array for inclusion in an executable. It works just super.

I'm wondering how I can write a program that will perform this operation on every file in a directory and all it's sub-directories so that I can I can include everything I need all at ounce.

View 9 Replies View Related

C++ :: Recursive Modification Of Map

Jun 23, 2014

I am having trouble with recursively modifying a <string, int> map (NCPB) -- when I call for what should be existing values in the map, I get "junk" values back (integers, but sometimes negative, large numbers, etc.). I've posted only the problematic function here:

int Count_Noncrossing(string RNA, map<string, int> &NCPB) {
map <string, int>::iterator it;
if (RNA.length() <= 2)//perfect short interval can only have 1 match; return 1 {
return 1;

[Code] ....

The problem is that when I ask for existing map values in a subsequent recursive call, they don't seem to be there. I imagine I'm missing something straightforward but can't seem to find it. I've tried declaring the map globally, passing it (as shown above), nothing seems to work.

View 3 Replies View Related

C :: Feedback On Recursive Function

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

C :: Recursive Crashes Program?

May 13, 2014

cause I cant find why it crashes. It compiles without any error,but crushes when i run it and I can't find where is wrong the code.

Code:

#include <stdio.h>
#include <time.h>
#include <math.h>

[Code].....

View 7 Replies View Related

C :: Way Recursive Calls Actually Works

Apr 27, 2013

This is simple recursive solution of Fibonacci number:

Code:

int fibo(int n)
{
if(n<=1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}

Now the recursion will generate a large recursion tree, like if n=5, 5 will call (5-1), (5-2) or 4,3 . What I want to know is, will fibo(n-1) will be called 1st go all the way to the base case 1, then do the summation or fibo(n-2) will be called right after fibo(n-1) ?

View 6 Replies View Related

C :: Recursive Function - Add All Even Numbers From N To 2

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

C :: Recursive Function Not Working

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

C/C++ :: Recursive Descent Parser?

Mar 22, 2014

I am implementing a recursive descent parser that recognizes strings in the language below.

The grammar:
A -> I = E | E
E -> T + E | T - E | T
T -> F * T | F / T | F
F -> P ^ F | P
P -> I | L | UI | UL | (A)
U -> + | - | !
I -> C | CI
C -> a | b | ... | y | z
L -> D | DL
D -> 0 | 1 | ... | 8 | 9

My input file has the following two strings:
a=a+b-c*d
a=a**b++c

The desired output:

String read from file: a=a+b-c*d
The string "a=a+b-c*d" is in the language.
String read from file: a=a**b++c
The string "a=a**b++c" is not in the language.

[Code].....

When I test the code without reading the text file and just write a string in the source code, it appears to parse fine. I believe my main problem is in the int main function and how i am reading the text file and outputting it. I was able to write the same program fine in Java.

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved