C/C++ :: Changing Recursive Function Into Iterator Function?

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


ADVERTISEMENT

C++ :: Changing Recursive Function Into Iterator Function?

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

C/C++ :: Changing Recursive Function Into Iterator Function

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

C++ :: Translating Recursive Function Into Iterator Function

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

C++ :: Function To Find Out Iterator Of Certain Value In Vector

Sep 4, 2013

I defined the following function to find out the iterator of a certain value in the vector. I defined it as such so if the value exist in the vector then return a iterator of it, if not then return a pointer pointing to nonsense.:

vector<tIndex_t>::iterator unlabelTit(const tIndex_t index) {
for(vector<tIndex_t>::iterator it=unlabelT.begin(); it<unlabelT.end(); it++) {
if(index==*it) return it;
} return NULL;
}

But looks this one is not accepted by compiler, saying I cannot do this kind of comparison like:

unlabelTit(i)!=NULL;

so I just delete the return NULL; and then the compiler giving me warning about no return statement before }.

a pointer pointing to nonsense? how to do that?

View 3 Replies View Related

C++ :: Write A Template Which Defines Min Max Operators On Vectors - Iterator Function?

Jul 3, 2012

I have a .cpp file which I have to create a header file for. I started it but I have stuck and it is full of errors.

I have some tasks (see comments in the code):

Task 2: I have to write a template which defines min max operators on vectors, it must be a custom vector template. The main program only demonstrates that it creates a data structure which calls for min max operators.

Task 3: I need a special min max function which watches for any changes and it has to work lineally so it has to step along the elements of the vectors determining the min max values.

Task 4: I have to create an iterator function

Here is the main.cpp code:

Code:
#include <iostream>
#include "mmvec.h"
#include <algorithm>
#include <string>
#include "mmvec.h"
struct Limited {
int val;

[Code] ...

View 1 Replies View Related

C :: Changing The Value Of Something From A Function

Oct 30, 2013

I was talking to someone earlier about how to change the value of something from a function, and they said what was needed was to use a ** to change something, and was wondering if I could get a walk - through of what happens. I understand a single pointer well enough, but a pointer through a pointer is kind of confusing to me. Here is a simple example.

Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
int add(int ** TOP, int * stack);

int *stack = NULL;

[Code] ....

Why is it that when the program prints the address of TOP in main, it is different than the address of TOP in the function? Is it because it is a different instance of TOP because it is in the function? When I put the number on *TOP, and come out of the function back to main, it then says the address of TOP is the number entered into *TOP, and am not sure why. And the **TOP ++ at the end I am thinking it increments malloc by 1, therefore bringing the pointer TOP up to point at the next element, or am I completely off base there?

View 7 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 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++ ::  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 :: Functions To Prove Variables Not Changing By Another Function

Jul 8, 2013

It is said that variables in a function cannot be changed by another function. Only by using pointers can variable values be changed. I am writing some functions to try to prove this theory, but I can't get it right.

Code:

#include <stdio.h>
#include <stdlib.h>
int main(void){
int x = 10;
printf("default x value is %d ",x);

[Code] ......

Code:

#include <stdio.h>
void try1(int x){
printf("x in try1 is %d
", x);
x++;
printf("x in try1 after ++ is %d

[Code]...

View 13 Replies View Related

C++ :: Changing Values In A Matrix Using Void Function

Jun 29, 2013

I have the following void function devised to assign "+1" or "-1" to each element of a matrix at random. While the function does what I want, when I print the values of the matrix all are set to zero:

#include <vector>
#include "aRand.h"
#include <iostream>
void initConfig(std::vector<std::vector<int> > premat, int nL, int nN) {
int * pnRand;
pnRand = 0;

[Code]...

The function pnRand_plus returns a pointer to an array of random numbers from 1 to 100, with seed time(NULL) + i. The values printed in main are zero, despite the values printed during the function run are fine (-1s and +1s).

View 2 Replies View Related

C++ :: Holding Function In Variable And Changing It Dynamically

Jun 30, 2014

I need a variable that will just hold a function that I can change in the middle of the application, even to different function type with different amount of parameters ... is this even possible? At the moment I have this

struct REPLY {
string *sReply;
function<int()> special = [](){return 0;};
REPLY()

[Code] .....

And then change it back to blank function returning 0 after calling it

(reply.special)();
reply.special = [](){return 0;};

This works fine (there is no reason why it should not, right? Now, if I want to have another function, let's say

void test2(string str) {
MessageBox(NULL, str.c_str(), NULL, NULL);
}

How do I point this one to the variable special, when I want to call it like that (or something similar)

(reply.special)("test string");

is this even possible? if so, how? i tried to create function pointer (didnt compile at all) or use template (neither did this) and how to do this as I discovered functional lib just a while ago.

View 12 Replies View Related

C :: How To Print Out Recursive Function In Main

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

C :: Converting For Loop To Recursive Function

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

C :: Space Complexity Of Recursive Function

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

C :: Recursive Function For Diagonal Winnercheck?

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

C++ :: Convert A Recursive Function To A Loop?

Dec 13, 2014

I am trying to convert a recursive function to a loop. However, it is not working.

//recursive function
int function(int x){
if (x % 3 == 0){

[Code]....

View 1 Replies View Related

C++ :: Libjson Non-recursive Parser Function

Oct 8, 2013

how to write a non-recursive JSON parser function using libjson in C++. libjson is quite useful librray. It's source code of libjson comes with an example C++ parser but it uses recursion to parse JSON arrays and child nodes. I am looking for parser function based on libjson that does not use recursion to parse JSON arrays and child nodes.

View 1 Replies View Related

C :: Change / Fill List Without Changing Function Type

Feb 13, 2013

I am an IT student currently learning linked list. I have a problem with my code here. After I call addFront() my list doesn't change when I display it. How do I somewhat change/fill my list without changing the function type? I know it works on pointers still messed up with linked list.

Code:

typedef struct node *nodeptr;
struct node{int item;
nodeptr next;};
typedef nodeptr List;
}

[code]....

View 3 Replies View Related

C++ :: Changing Virtual Function Output Without Using Any Data Member

May 10, 2014

Instead of this:

#include <iostream>
struct Object {
int size; // Want to avoid this because size is (almost always) constant
Object (int s): size(s) {} // for every Object subtype.

[Code] ....

I want this:

#include <iostream>
struct Object {
virtual int getSize() const = 0;
};
struct Block: Object {
int getSize() const {return 5;} // always 5, except once in a blue moon it may change

[Code] ....

The Decorator Pattern works (getSize() can then return 6) but it is a poor choice for my program because it will cause more problems (due to many containers holding its old address, among other things. Any way to achieve this without any change of address, and without creating new storage somewhere, either inside the class or outside the class (too much responsibility to follow that stored value for the rest of the program just for this rare change, and creating a data member uses up too much memory for the many, many Block instances)?

View 5 Replies View Related

C :: Segmentation Fault In Call To Recursive Function

Apr 12, 2014

Task: To create a recursive function to sort elements in an array of integers.

The function must start the sorting from the first element, and the recursion calls must go on until the last element in the array is sorted. In each step of recursion, the function must work only with the subset of array elements that have not been sorted yet.

Problem: I am getting a 'Segmentation fault: 11' in the recursive call to the function (please, see the code below).

Environment: Mac, OS X = Mavericks

Code:
////////////////////////////////////////////////////////////////////////////////
////////// Recursively sorting an array of ints.
////////// Arguments: array, array size, index from where to start sorting.
void sort_incr_array_int_recursive(int a[], int size, int i){
int tmp, idx_small, small = a[i];

// Locating the smaller element in the array section.

[Code] ....

View 4 Replies View Related

C :: Function To Find Starting Point For Recursive

Mar 6, 2015

I am trying to create a function to find the entry point of my map.But my program does not seem to be working correctly. I am trying to place a dot there because I will have to use recursion to fill up the whole map. But I'm not asking for the answer. I just need writing a function to locate the starting row for the first column of the maze (first non zero element). My code seems to have a bug in it when I try and call my function FindEntry. What I am trying to do is read in column by column until I can find the starting point and then place a dot there with ASCII character 249. This is my code so far:

Code:
#include<stdio.h>
#include<Windows.h>
#define HEIGHT 21
#define WIDTH 78

[Code]....

If you try and run it, it will give you a bug. But if you comment out the FindEntry function in the main it will work and look like this:

View 7 Replies View Related

C :: Reverse Array Of Characters Using Recursive Function?

Mar 15, 2014

Eg. User input : abcde
Program Output : edcba

I keep on getting weird ASCI symbol in return, I couldn't achieve what I need, and tried debugging for days,

Code: char ar[20];
int len,n=0;
printf("enter the string to be reversed :
");

[Code].....

View 5 Replies View Related

C++ :: Recursive Function Which Find Partition Of A Number N

May 18, 2013

I am supposed to write a recursive function which find the partition of a number n ,, for example if n=3 , it should print 1 1 1 , 1 2 , 3

I wrote the program but i am getting the two partition 1 2 and 2 1 which are the same ,, how can i avoid that ?
this is the code :

void PrintPartition( int n , int A[] , int j ) {
if( n<=0 ) {
printArray( A, j );
return ;
} for( int i=1 ; i<=n ; i++ ) {
A[j]=i;
PrintPartition( n-i , A ,j+1 );
} }

the first call of the function is : PrintPartition( n , A , 0 ) ;

View 3 Replies View Related







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