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
ADVERTISEMENT
Mar 27, 2013
I would like to modify attributes like modification/creation dates.The function is correctly working as if I type in "ls -al", the timestamp is correct. But when using my program to read these attributes, it returns the "real" modification/creation date. Here is the function that shows the timestamp :
Code:
time_t t = sb.st_mtime; struct tm tm = *localtime (&t);
char s[32];
strftime (s, sizeof s, "%d/%m/%Y %H:%M:%S", &tm);
printf ("%-14s %s", lecture->d_name, s); And here is the code for modifying the timestamps : Code: void modifyAttributes(char * file, int mtime, int atime)
}
[code]....
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 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
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
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
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
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
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
Feb 10, 2014
how to recursively modify my program. The problem I'm hacing is the the program is not looping correctly and also not printing the correct number. I've calculated the payoff correctly, also I've only been able to print the first section of R3. I can't figure out how to loop it to get R2 to stay at 2 then go to 3 after all possiblities of R2 at 2. Enventually, R1 will change to 2 then 3; 3 being the highest number earned. To be mentioned that will be three recursive function loopR1, loopR2, and loopR3 for each column.
The result of the program should look like:
R1 R2 R3
1 1 1 payoff is 1
1 1 2 .......... 1
1 1 3 .......... 1
1 2 1 .......... 1
1 2 2 .......... 1
...
...
...
3 3 2 .............. 5
this is what I have so far:
#include <stdio.h>
#include <stdlib.h>
int payOff(int r1, int r2, int r3);
void loopR3(int R3, int upto);
void loopR2(int R2, int upto);
[Code].....
View 12 Replies
View Related
Apr 28, 2014
I have the following code that does not work. It prints out 1 when it should print out 5 and 3 respectively. I know that the code is not the pretties due to ( *arr == value) but i am just learning C++.
Spoiler
template <typename T>
int countOccurrencesRecursively(T* arr, int size, T value){
//the base case is when the the array is empty
if(size == 0) return 0;
else return *arr == value + countOccurrencesRecursively(++arr, --size, value);
[Code] .....
View 2 Replies
View Related
Apr 19, 2013
So I'm going through and trying to do a recursive implementation of a Heap. I keep getting access violations for some reason on my sifts (_siftUp) - even though I'm trying to insert into sub[0] (currSize = 0 in the constructor). I don't think either of my sifts are implemented correctly, are they?
Here's my Heap:
Code:
#ifndef HEAP_H
#define HEAP_H
//**************************************************************************
template<typename TYPE>
[Code].....
View 5 Replies
View Related
Feb 11, 2014
I'm trying to write some naive binary serialization code and wanted to cut down on repetition of logic for serializing/deserializing nested vectors or other STL containers to reduce the chance of typos etc, and thought templates might hold a solution for me.
Code:
template <typename T> void serializeField(IWriter& writer, const T& val) {
writer.write((char*)&val, sizeof(T));
}
template<typename U, typename V>
template <> void serializeField(IWriter& writer, const U<V>& collection)
[Code] ....
Is there a way to do something like this? It isn't a big deal for me to just manually write code to serialize my vectors to the needed depth, but it sure would be nice to get this working.
View 10 Replies
View Related
Mar 6, 2015
In this code nothing modify except function minMaxSearchRecursive
Code:
int min(int a, int b) {
if (a < b) {
return a;
} else {
return b;
[Code] .......
View 9 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
Sep 21, 2013
I wrote a fuction in C with the prototype 'void raisePowerOf2(int array[],int pow);'
If someone want to find the value of 2^456 ,just have to invoke this function 456 as the value for pow and int array with 2 elements :1 & -1 as the argument for the array.(There I used -1 to denote the end of the array.)
But it seems that this function doesn't give the exact answer
And I tried this from java also,with the same implementation.It produced the answer precisely .
I tried for hours, but unable to detect reasons why this code blok in C doesn't work properly
This is the code in c
Code:
#include<stdio.h>
void raisePowerOf2(int array[],int pow);
int main(){
int a[2]={1,-1};
raisePowerOf2(a,5);
return 0; }
void raisePowerOf2(int array[],int pow){
[Code]...
This is the code in java....
Code:
public class NewClass4 {
void raisePowerOf2(int array[],int pow){
final int len=array.length;
int store[]=new int[len+1];
int qtnt=0;
for(int i=len-1;i>=0;i--){
store[i+1]=(array[i]*2)%10+qtnt;
qtnt=(array[i]*2)/10;
[Code]...
View 7 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
May 20, 2013
I've been studying "algorithm" by myself by book. And there's this part - vulnerable point of "define" syntax on recursive programming.
Code:
int max_arr2(int arr[], int arr_len) {
int (arr_len == 1)
return arr[0];
else
return max(arr[arr_len-1], max_arr2(arr, arr_len-1);
.
.
This book says, " function 'max_arr2' is just recursive function, without any problem. but if max() is defined like this,
#define max(x,y) ((x)>(y) ? (x):(y))
and if array is just declined, it will take very long time."
I typed this code and run, and actually it really takes long time.
View 1 Replies
View Related
May 22, 2013
would like to know that my algorithm has asymptotic complexity recursive?
int solve(pair<int,int> actual)
{
if(actual.first == PuntoInicio.first && actual.second == PuntoInicio.second)
return 1;
[Code]....
View 1 Replies
View Related
Dec 27, 2014
I've been working on a matrix class and I ran into a problem in writing my matrix class. I keep getting 0 as a determinant and with some debugging, I found that I was losing allocated data or something similar. This algorithm I'm pretty sure works because I used this same algorithm in a function I made in python. [URL] .... That's where I found the algorithm.
/*template <class T>
double Matrix<T>::det(T* array, size_t dim, bool recursion)*/
double det(T* array = NULL, size_t dim = 0, bool recursion = false) {
if (recursion == false) {
if (m != n) {
return 0;
[Code] .....
View 2 Replies
View Related
Mar 18, 2014
i've just started learning building structures in c++ and they gave us an exercise of writing a recursive merge code of linked lists - just merging without sorting... i don't even know how to start this is how i started so far.... i know that the break in the recursive function is when i get to the end of the first list and then to start linking the second list..as you can see i wrote a function that uses recursive function...
LIST merge3(LIST lst1, LIST lst2) {
LNODE* curr1 = lst1.head;
LNODE* curr2 = lst1.head;
LIST mergeList;
mergeList.head = NULL;
mergeList.tail = NULL;
[code].....
View 1 Replies
View Related
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
Mar 9, 2014
The goal of this program is to take 4 neighboring elements in an array and add them together. The program asks user for the number of rows and columns to start out with and the program will then continue to print the board until 1 element remains.
I'm having problems getting my program to compile (line 19)
#include <iostream>
#include <cstring>
#include <cstdlib>
[Code].....
View 3 Replies
View Related