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


ADVERTISEMENT

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 View Related

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/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/C++ :: Reading In A String And Turning It To Int

Dec 10, 2014

I have a problem with transforming a string, for example

"13 + 19"

and store this in a list as seperate integers,

list = {13, 19};

and another list with the +, -, /:

list2 = {+};

this is my function:

int evaluate(char* formula, int* result) {
struct List *listofintegers = list_create(); //creates a list, this is the structure:
/*
struct ListNode {
int value;
struct ListNode* next;

[Code] ....

This is how i execute my function in my main.c:

int value;
evaluate("19 + 16", value);

This is what i get in my prompt:

I will also have to seperate the +, - and / in another list,

I don't even know how i can get my string when char* formula is given as an argument...

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++ :: Turning BMP Image To Min Finite Automata

May 14, 2013

I don't know how to start with it, first thing that came up to my mind was "loop through an array of bits" in bitmap (monochromatic), but I can't get the bits. Here's code I have:

#include <iostream>
#include <fstream>
using namespace std;
unsigned char* readBMP(char* filename) {

[Code] .....

No errors, it's working fine but I just can't get the bits of picture. I've tried to "cout" values form header (width, height) and they're ok, but I wonder how to get representation of picture itself. I thought that this code will give me something like array of 1 and 0.

I have to convert black and white (monochromatic) bmp image to min. finite automata.

View 12 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++ :: Turning Number Symbol Into ASCII Character

May 28, 2014

Im programming a roguelike game using visual c++ Microsoft express 2010 and i made a multidimensional array for my first map. I have the walls as # and was wondering how i could turn those into ascii symbol 219. Also i need to know how to turn specific text certain colors.

View 5 Replies View Related

C++ :: Turning Digits Of Integer Into Array Elements?

Nov 19, 2013

I have an integer that the user enters. I need each digit of the integer to be set as an element of an array. the integer could also be entered as an array, but I need the user not to have to enter each element and press ENTER.

View 5 Replies View Related

C Sharp :: Create Instance Of Form Into Non Form Class To Access Button / Label

Nov 23, 2014

I have a non form class. I want to update label/ check status of check box etc.. in non form class ( here resides functions that contains logic). How can i do that ?

View 4 Replies View Related

C :: Turning String Of Numbers Into Array Of Individual Integers

Mar 2, 2014

In my program, I am fed a string that contains integers such as Code: *str = "45678" and my program is simply supposed to read the number in and store each given number in a separate spot in an integer array. So basically, when my program has finished running it should be stored like:

Code:

arr[0] = 4
arr[1] = 5
arr[2] = 6
arr[3] = 7
arr[4] = 8

The way I have been attempting to do this was just copying it to the array like so:

Code:

arr = malloc(sizeof(int) *(strlen(str));
for (i=0; i<strlen(str); i++) {
a->digits[i] = str[i];
}

however, this just seems to return an impossibly high garbage value when I do. I'm assuming the way I'm trying to store it is 'illegal', but I cant seem to find online a proper way to do it.

View 2 Replies View Related

C# :: How To Access User Control Form One Of The Child Form

Apr 22, 2014

I hav created mdi parent form that contain one user control. I want to access that user control from one of the child form

View 4 Replies View Related

C# :: Login Then Close Form And Open Main Form

Aug 25, 2014

This is more of a Application Design question, Let's say I have Form1(Login), this is opened from Program.cs, when the user enters details, the Event Handler makes a instance of a class, adds the data and stores the instance within Program.cs class.

So the best thing to do here, is to close the Form1(Login) and open Form2(Main) via Program.cs so that I can send the List<Login> object with my Login instance objects to said form. This form will remain open throughout the applications use as it's like the main GUI for all the programs functions.

When I close this form, I may want to save some information before the application is terminated, so I may as well store the Login instances in Program.cs anyway.

Main stepping stone: Multiple (usually around 3 max) users can login this system if need be, which will mean closing Form2(Main) and then opening Form1(Login) so that once Form1 is closed it can recreate the From2(Main) form and pass the new List<Login> to that form.

What's the best way to do this, at the moment i'm creating the Form2 instance like so:

//....
//Detect the Login form being closed
loginForm.Closed += new EventHandler(OnLoginClosed);
//...
//Open the main form when the login is closed
private void OnLoginClosed(object sender, EventArgs e)

[Code] .....

This seems like it will work and do the job, but is there a better and cleaner way? The List<Login> will be passed to Forms which are created via Form2(Main) as this information will be needed. It's just that ive been told not to use Forms for too much data containment.

View 2 Replies View Related

C# :: Sharing Variables From Login Form To Another Form

Mar 10, 2015

I have a form with 2 text boxes (Email and Password)

The user fills in the text boxes and clicks on the Log in button. The code behind the log in button does the following, First connects to a table (Users) in phpmyadmin. Next runs a SQL query (SELECT * FROM `users` WHERE Email = '" + sEmail + "' AND Password = '" + sPassword + "'") sEmail being the variable created from the text entered in Email text box and the same for password.

Next if the record count == to 1 it opens up the main menu form and if the record count == 0 it fails and the user does not get to the main menu.

All of the above is fine and working however what I want to do is take over a variable from the log in form to the other forms.

The code is below for the sign in button as all my code is behind that (I think this may be where I'm going wrong).

public partial class WelcomeForm : Form{
public static string connStr = "server = localhost; " +
"database = ppw5; " +
"uid = James; " +
"pwd = buster;";

[Code] .....

And the Main menu form where I'd like to take a variable over with me, lets assume the variable is the UserID from the database table that I pull from the dTable I created.

public partial class MenuForm : Form {
//Call the CloseProgram class and create a new method called ClassClose.
CloseProgram ClassClose = new CloseProgram();
WelcomeForm User = new WelcomeForm();
public MenuForm() {
InitializeComponent();

[Code] .....

View 7 Replies View Related

C# :: Calling Method In One Form From Another Form?

Dec 6, 2014

I have a program which when a button is clicked on Form1 it runs a vision system operation on an image in a window on a Form1. There are also other buttons on the Form1 which can change things like threshold levels so the inspection can be run again with different settings. It all works fine but I want to change it to remove the buttons from Form1 so that when the user clicks on a button on Form1 it opens up another form, Form2, and all the buttons which were on Form1 are now on Form2 and the image analysis should run on Form1 when the buttons are used on Form2. I have taken over all the code from Form1 to Form2 and I'm trying to make this happen by creating a new oject on Form1 which calls a method on Form2 which contains the code which was in Form1 and called when the button was clicked on Form1 - not working!! I have a constructor in Form2 but I think I have become monumentally confused.

This was the code which was on Form1 when the inspection button was clicked.

private void cmdInspectOnce_Click(object sender, EventArgs e)
{
iReturn = VisionSystem.InvModeSet(IpeEngCtrlLib.I_MODE.I_EXE_MODE_ONCE);
if (_imgEngines!=null && _imgEngines[0]!=null)
_imgEngines[0].cmdInspectOnce();//InvModeSet(IpeEngCtrlLib.I_MODE.I_EXE_MODE_ONCE);
}

View 1 Replies View Related

C# :: Selection From 1 Form DatagridView On Second Form

Sep 5, 2014

I have 3 Windows Form: frmMain, frmFirstForm, and frmSecondForm.

frmMain is the form that will load when the application is started with 2 tabs to access frmFirstForm and frmSecondForm.

Here's my problem:

After making a selection from a combobox on frmFirstForm, I would like a datagridview header to change based on the selection made. Here is a list of items on one combobox: Item1, Item2, Item3, Item4. When somebody chooses Item one, the header on the datagridview on frmSecondForm should display: JohnItem1, PaulItem1, BradItem1 and so on. Those items are not part of the comboboxes, but they will be shown as header text on the datagridview on the second form either automatically or after clicking OK from frmMain. There's no OK button on frmFirstForm, but on frmMain.

The only code I have right now is the if statement on the form load on frmSecondForm. Here's what I have:

private void HeaderTextLoad() {
if(comboboxonfirstform.text = "Item1") {
datagridview1.Columns[0].HeaderText = "ItemJohn";
datagridview1.Columns[1].HeaderText = "ItemPaul";
... And so on.
}
}

View 1 Replies View Related

C++ :: Turning Arabic Numerals To Roman Numerals?

Nov 11, 2013

this program is supposed to turn Arabic Numbers into Roman Numerals, but after making the program shown below I only am able to get some alt codes to show up (I got a green lantern symbol to show up)

How would I fix this program to show Roman numerals?

#include <iostream>
#include <climits>
#include <cctype>
using namespace std;
int main(void)//makes all values void until entered {
system("color 0C");
while(true)

[code]....

View 1 Replies View Related

C++ :: How To Convert Compact Form Of Matrix Into Matrix Form

Oct 4, 2014

3 3
2 1 3 2 3
1 0 2
1 2 6

First line shows row and column number

First index of the second line tells the number of non-zero entries of the first row and second index tell the column number where the non zero entry is placed

for 1st row of matrix:

non-zero entries=2
column number=1
non-zero entry=3
column number=2
non-zero entry=2

covert this in the given form
0 3 3
2 0 0
0 0 6

View 1 Replies View Related

C++ :: Turning A Limited Float Into Another Float?

Nov 20, 2013

I can do the folowing:

float var1 ;
var1 = 9.12345 ;
printf("%.2f",var1) ;

the output will be 9.12. What if I wanted to save that as another separate float with displaying it on screen?

View 1 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







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