C/C++ :: How To Decipher Recursion
May 4, 2014Suppose I'm given the following code:
int mystery(int n){
int something;
if(n > 0)
Suppose I'm given the following code:
int mystery(int n){
int something;
if(n > 0)
I need making a function that is deciphering multiple strings from a .txt file. The function is supposed to remove all instances of the first three characters from the string.
An example would be from l.pjkjsdfl.p[)sdfslkl.p ------> jkjsdf[)sdfslk
Basically it removes "l.p" from wherever it is in the string.
I am new to C++ and I don't know where to start from.
I'm writing a series of basic decipher programs and I have run into an issue where I get the correct answer when I start the loops at the iteration that contains the correct answer.
Code:
// generate key "words" with length of 3
for (int x = 0; x < 26; x++){
for (int y = 0; y < 26; y++){
for (int z = 0; z < 26; z++){
[Code] ....
This is the essence of the loop, I've attached the program in its entirety if necessary. Basically what happens is if I start the loops at x = 17, y = 7, z = 12, then I get the correct decipher shifts but if I start at 0,0,0 whenever it gets to that iteration (12,000 ish) the shifts are off by 2 or 3. "koq" should translate to "the" but im getting "dcz". Is this a simple bug in the or is something moving to fast for something else to keep up?
l3_ws.txt
main.cpp
To construct and write down algorithm of determination of the sum of squares of consecutive integers with recursion use. I tried to do something:
public static int RecSumSquare(int x, int n)
{
if (n < 0) throw new ArgumentException("n should be greater than zero");
if (n == 0) return 0;
else return x*x+RecSumSquare(x, n - 1);
}
But I don't know as the beginning and the end of this algorithm will look.
So the task is to find the node with minimum value of a binary tree (not binary search tree). the input is the pointer to the root of the tree. and i cannot make recursion work when i do if conditions. here is what i have Code: /*function 3-takses as input the pointer to the root of the tree and returns a pointer to the node with the minimum value*/
CPPtr minimumvalue(CPPtr SP){
CPPtr min = NULL; //node of minimum value
if(SP== NULL){ // if there is a node, begin comparing
return NULL;
}
else{
if(SP->data<SP->left->data){ //if the node has smaller value than its left child
min = SP; //update node of minimum value
[code].....
no matter where i call my function i get errors like unhandled exception at some memory. how to use recursion in this?
I have this problem set that has to use recursion to permutate scores. I have read in all the numbers and have tested that it works . l have also attached what I have so far. Here is what I think I need to do:I have to pass the structs into this permutation algorithm that is here:
Code:
#include <stdio.h>
void ListPermutations(char str[]);
void RecursivePermute(char str[], int k);
void ExchangeCharacters(char str[], int i, int j);
[Code]....
It seems that boost's file recursion requires that the file using the recursion must be in the include path. This makes using file recursion in a library header a problem as libraries may be located in a subdirectory of an include path (which is minor since the programmer can state the subdirectory in the recursive file call as long as it is not relative to the calling file). I also found it a problem when the main compile directory isn't in the compile path.
View 9 Replies View RelatedSo the task is to find the node with minimum value of a binary tree (not binary search tree). the input is the pointer to the root of the tree. and i cannot make recursion work when i do if conditions. here is what i have
CPPtr minimumvalue(CPPtr SP){
CPPtr min = NULL;//node of minimum value
if(SP== NULL){// if there is a node, begin comparing
return NULL;
[Code] ....
No matter where i call my function i get errors like unhandled exception at some memory. How to use recursion in this?
I want to convert the following program into a non-recursive code using a stack and a loop:
void write(int n) {
if (n>0) {
write(n-1);
cout << n << " ";
write(n-1);
}
}
And here is the code I am using but that currently does not work, and I do not know why:
stack<int> S;
S.push(n);
while (not S.empty()) {
int k = S.top();
[Code] .....
How to simulate that recursion. I thought it was enough to push into the stack the equivalent recursive call.
So i have a program it works which takes in a word reverses it prints it out. BUT! the problem is that the program is without recursion. convert the function into recursion.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
[Code].....
I am trying to print an outline. My code works up to a depth of 3. (The depth is the number of subsections - so 3 would be section 1, section 1.A, and section 1.A.1). It also works for a width (number of sections and each type of subsection) of 26, where it is capped. However, to get a larger depth, it would involve many more loops. Not only is that terrible code, it also freezes up the terminal I'm working on. I believe recursion would make it much nicer, but I'm struggling to grasp the idea when using a string (I understand when it is a number).
#include <stdio.h>
int sec(int width, int snum) {
char section[100];
sprintf(section, "Section ");
[Code].....
I put some checks in factorial program, I am confused how 27, 40 50 is coming in output and ans is 10.
#include<stdio.h>
int fact(int n) {
if(n==1) {
printf("hello1 %d
",n);
return 1;
[code].....
I have a linked list I made and am practicing a simple recursion function sum() which returns the sum of all numbers in each node of the list. Here is my sum() function:
int sum(ListNode *ptr){
if (ptr->next)
return (ptr->num + sum(ptr->next));
else
return(0);
}
where the initial arg passed through the ListNode * parameter is my head node (with null value). The output is fine until that point, then the program locks up and I get the "...has stopped working" error.
I have been asked to write three functions for a Liked List that can add remove and reverse the elements of the Linked List. Now, I have done the add function and It is displaying the elements properly using recursion function. But My removal function is not working and I tried all the possible logic I can think of, I dont know, where did I make mistakes.
This is My class for the Linked List
#ifndef __Linked_Lists_Recursive_function_Implementation___NumberList__
#define __Linked_Lists_Recursive_function_Implementation___NumberList__
#include <iostream>
using namespace std;
[Code]....
I am trying to input a recursion method. The code compiles, however, it is only giving me a value of 1. I am wanting the value of 5 when it is compiled. Why is this?
#include <iostream>
using namespace std;
int number(int x) {
if (x == 1)
[Code] ....
How to print to the screen the value of n after it has been multiplied.
For example: if I use cout << "n: " << n << " power: " << power << "
"; I
can see the variable "power" decrementing by 1, but I don't see the variable "n" incrementing with its new value after it has been multiplied by n * n.
#include <iostream>
using namespace std;
typedef unsigned short int USHORT;
typedef unsigned long int ULONG;
ULONG Getpower(USHORT n, USHORT power);
[Code] .....
I'm trying to write a program that converts a decimal number to a binary one. I have most of the program written, but I am having a little bit of trouble. Whenever I enter a decimal number, the program will convert it correctly to binary, however, the last number is not included in the conversion. EX: Converting 37 into binary (0100101) yields 010010 when entered into the program. BTW the program must utilize recursion to achieve this goal.
#include <iostream>
using namespace std;
void decToBinary(int num1);
int main() {
int num1;
[code]....
I need to sort an array of n given elements using recursion. What am i doing wrong here?
#include <iostream>
using namespace std;
int i=0, j=1, v[100], n;
[Code].....
How do function count factorials in this program?
#include<iostream>
#include<conio.h>
using namespace std;
void main(){
unsigned int factorial(unsigned int value);
[Code] .....
write a code that draw a block and diamond each in a single run by using '*' in recursion.
Below are one 10X10 rectangle and 6X4 diamond.
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
**
* *
* *
* *
* *
* *
* *
* *
**
Note: I couldn't write the diamond here appropriately somehow.
I am having to write a program that takes 2 sorted linked lists (x and y) and merge those to sorted into a third one (z). This has to be done via recursion. I think I am close but there is a logic error.
Code:
void SortedMergeRecur(Node*& xHead,Node*& yHead,Node*& zHead)
{Node* temp = 0;
if(xHead == 0) {
zHead = yHead;
yHead = 0;
}
[Code]...
This function is apparently causing infinite recursion, but I have no clue what's causing it:
Code:
int pow( int base, int exp ) { int somevariable = pow(base,exp-1);
if (exp == 0) {
return 1;
}
else {
return base * pow(base,exp-1);
}
}
I am trying to reverse a Linked list using recursion but whenever I try to print it after reversal it prints only the last element of the list.
Code:
void reverseRecursive(Node **root, Node *temp)
{
Node *next = temp->next;
if(temp->next == NULL)
{
*root = temp;
[Code]...
I have just posted the reverse function to enable easy readability rather than post the entire code
I wrote the following recursion for finding the distance between a root and a leaf, but am not quite sure how to make it work.
Code:
int calc_depth(Node* tree, Node* zero) {
if (!tree)
return 0;
if (zero->right || zero->left == tree)
return 1;
return (zero->right ? calc_depth(tree, zero->right) : calc_depth(tree, zero->left));
}
Q.print triangle pattern using one loop and recursion
eg: 5
Code:
#include <stdio.h>#include <conio.h>
void pattern(int n,int N) {
int i,c=1;
[Code]....
program for counting vowels in a string using recursion ?
View 12 Replies View Related