C++ :: Recursion To Loop Using Stack

Oct 15, 2014

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.

View 2 Replies


ADVERTISEMENT

C/C++ :: Recursion In Stack To Get Maximum Element And Copying Stacks

Nov 22, 2012

I am still new to stacks and recursion and am trying to implement two functions using recursion to get the maximum element in a stack and copying one stack into the other but it keeps repeating some elements and i dont understand why!!

this is the code for the functions:

void copyStack(MyStack &firstStack, MyStack &secondStack) {
    if(!firstStack.empty())    {
        string x=firstStack.top();
        firstStack.pop();
        //secondStack.push(x);

[Code] ....

View 2 Replies View Related

C :: Print Triangle Pattern Using One Loop And Recursion?

Dec 18, 2013

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]....

View 8 Replies View Related

C++ :: Object Creation On Stack In Loop?

Apr 29, 2014

have a piece of code

for (int i = 0; i<5; i++)
{
CMyStackObject sobj;
//
...
}

Does standard guarantees that instances of sobj will be different for different i?

View 3 Replies View Related

C/C++ :: How To Loop Through A Stack And Printout Each Of Its Elements

Sep 14, 2014

I have a program that consist of a postfix calculator for a class assigment, my program is basically done, however I am missing how to loop through the stack and print each element of it. My output is suppose to look something like this:

Token: 4+5 (my token would be the infix expression that the user inputs)

output:45+ (my output would be the postfix expression)

Stack(bottom to top) :empty + + empty (This is suppose to be the process of the stack, but I do not know how to get this values to print out)

and I already have the token and output part, however I do not know how could I get the stack values, in order to print them out.

My program is the following:

#include <sstream>
#include<iostream>
#include<cstring>
#include<iomanip>
#include <vector>
#include <math.h>
#include <windows.h>
using namespace std;

[code].....

how to proceed with printing the stack so that it looks like my example output?

View 4 Replies View Related

C# :: Algorithm With Recursion Use

May 12, 2014

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.

View 2 Replies View Related

C/C++ :: How To Decipher Recursion

May 4, 2014

Suppose I'm given the following code:

int mystery(int n){

int something;
if(n > 0)

View 1 Replies View Related

C++ :: Return A Node Using Recursion

Mar 27, 2013

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?

View 6 Replies View Related

C :: Use Recursion To Permutate Scores

Feb 8, 2013

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]....

View 1 Replies View Related

C++ :: File Recursion Limitation?

Apr 28, 2013

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 Related

C++ :: Want To Return A Node Using Recursion

Mar 28, 2013

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

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?

View 1 Replies View Related

C++ :: Reverser Function Using Recursion?

May 8, 2014

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].....

View 2 Replies View Related

C/C++ :: Recursion With Printing Strings?

Oct 20, 2014

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].....

View 1 Replies View Related

C/C++ :: Factorial Program With Recursion

Nov 9, 2014

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].....

View 2 Replies View Related

C++ :: Recursion With Linked List

Aug 28, 2013

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.

View 1 Replies View Related

C/C++ :: Recursion Using Linked List

Apr 28, 2014

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]....

View 4 Replies View Related

C/C++ :: How To Input A Recursion Method

Feb 7, 2015

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] ....

View 9 Replies View Related

C++ :: Recursion - How To Print To The Screen Value Of N After It Has Been Multiplied

Mar 20, 2013

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] .....

View 2 Replies View Related

C++ :: Converting Decimal To Binary Using Recursion

Feb 25, 2013

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]....

View 4 Replies View Related

C++ :: Sorting Array Of N Given Elements Using Recursion?

Dec 29, 2013

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].....

View 2 Replies View Related

C++ :: How Do Function Count Factorials - Recursion

Apr 3, 2013

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] .....

View 7 Replies View Related

C++ :: Drawing A Rectangle And Diamond Using Recursion?

Oct 23, 2013

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.

View 1 Replies View Related

C++ :: Merging 2 Linked Lists Into Third One Via Recursion

Apr 17, 2012

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]...

View 3 Replies View Related

C++ :: Function Causing Infinite Recursion Crash

Oct 24, 2014

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);
}
}

View 10 Replies View Related

C :: Reversal Of Doubly Linked List Using Recursion

Mar 6, 2015

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

View 7 Replies View Related

C :: Recursion For Finding Distance Between Root And Leaf?

Jun 19, 2013

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));
}

View 14 Replies View Related







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