C++ :: Recursive Function - Take Number And Return With All Sevens Turned Into Eights

Apr 16, 2013

Task: Write a recursive function `seven_up` which takes a number and returns that number with all the sevens turned into eights.

- Example

cout<<seven_up(777)<<endl; //prints 888
cout<<seven_up(1234567890)<<endl; //prints 1234568890
cout<<seven_up(50)<<endl; //prints 50

- Hint: It's like removeFirst, except:

0. Base Case: If the number is one digit long, we don't want to erase it (by returning 0). Instead, riddle me this: When I have a one-digit number, what happens if I change all the 7's into 8's? Well, if the number is 7, it becomes 8, but otherwise it's unchanged.

1. Recursive Call: If the number is longer, then we strip off the last digit, figure out what the answer for the rest is (the recursive call on n/10), and then put the last digit back on the number when you're done.

Hint: You probably need to store the least digit (the n%10) and check if it's 7 separately.

#include <iostream>
using namespace std;
int removeFirst(int n) {
if(n<10)
return 0;
return 10*removeFirst(n/10)+n%10;

[Code] ......

I tried other algorithms but i don't get it.

View 14 Replies


ADVERTISEMENT

C/C++ :: Take In Unsigned Number And Afterwards Return N-th Member Of Recursive Function

Mar 14, 2015

I'm having an issue with the following assignment:

Program is supposed to take in an unsigned number and afterwards return the 'n-th' member of the following recursive function:

f(1) = 1, f(2) = 2; f(3) = 3; f(n+3) = f(n+2) + f(n+1) + f(n), n>0;

When I worked with Fibonacci it was pretty easy since I just had to decrement the next member for each step. I used the following:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int fib(int n) {
static int memorize[MAX] = {1,1};
if(memorize[n])
return memorize[n];

[Code] ....

My main problem is that I have no visual of the current function, as well as the fact that it takes the f(n+3) = f(n+2) + f(n+1) + f(n), whilist I've only got f(n) to begin with.

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

C++ :: Highest Digit In A Number (recursive Function)

Dec 23, 2013

I have this example problem in my school coursebook. How this program works? It determines the highest digit in a number.

#include <iostream>
using namespace std;
int m(int n) {
int a,b;

[Code] ....

View 4 Replies View Related

C++ :: Creating Recursive Function That Will Print Out A Number To A Power

Apr 9, 2013

I am trying to create a recursive function that i can call on in order to take a user inputed base and exponent and give final answer this is what i have but im completely lost after this i dont even know how to continue. What i have so far

#include <iostream>
using namespace std;
int Exp(int x,int y){
if(base <= 1 || exp == 0) return 1;
if(exp == 1) return base;
int main(){
int number, exp;

[Code] .....

After i set the base situations im not sure how to get the function to make the function take the base to the exponent recursively.

View 3 Replies View Related

C++ :: Recursive Function - Program Is Returning A Negative Number At The End

Nov 14, 2013

Why my program is returning a negative number at the end...attached is the program:

/*Write a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of 1 element.*/

#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
float recursiveMinimum (int ARRAY[], int n);

[Code] .....

View 2 Replies View Related

C++ :: While Loop - Use Non-recursive Function To Make Triangle Number Sum Of All Whole Numbers From 1 To N

Jul 31, 2014

My while loop is the problem. I want to use a non-recursive function to make the triangle number the sum of all whole numbers from 1 to N.

#include "stdafx.h"
#include <iostream>
using namespace std;
int triangle(int t);

[Code] ....

View 2 Replies View Related

C++ :: Make A Function To Return Placement Number

Mar 25, 2014

I have a class called items and stored in a vector as vector<items*> item.now I want to make a function to return the placement # that the item is stored at with a unsigned int. but if I don't find the item is there like a null unsigned int that can be returned.

unsigned int someFunc(vector<items*> item)
{
unsigned int size = item.size();
for (unsigned int i = 0; i < size; ++i)
{
if (item[i]->getItemName() == "sword")
{
return i;
}
}
//i need code here incase the item is not there
}

because the item might not be there and i would like to do a line if(unsigned int) do this code

View 19 Replies View Related

C++ :: Function Which Take An Integer And Return Number Of Digits In It

Jan 31, 2013

I want to write a function which take an integer and return the number of digits in it i.e

int i = 123456
func(i) {
some code
}

output
the number of the digits are 6

View 9 Replies View Related

C++ :: Function That Should Return Number Of Digits In Integer Returns Last Digit

Feb 18, 2015

Code:
int exploder(int number,int array[]) {
int functi = 0;
int digit = number % 10;
while (number > 0) {

[Code] ....

View 2 Replies View Related

C :: Will Return Root Statement At End Ever Return Value Other Than Value Passed To Function?

Mar 29, 2013

I'm writing some functions pertaining to binary trees. I've used recursion once before while learning quicksort but am still quite new and unfamiliar with it. And this is my first time touching a binary tree. So my question: In my addnode function, will the return root statement at the end ever return a value other than the value passed to the function?

Code:

#include <stdlib.h>
struct tnode
{
int data;
struct tnode * left;
struct tnode * right;
}

[code]....

View 4 Replies View Related

Visual C++ :: Detect And Find Rotation Angle If Skeleton Tracked Turned 180 Degree To Kinect

Sep 25, 2013

I m developing an application using kinect.The IDE I use is Visual studio 2012 and kinect SDK 1.8.I m developing using vc++

I want to overlay an image on the person tracked when the person turns 180 degress to kinect. ie the person is not facing the kinect.

how do i track the rotation angle of the person as he/she turns away from kinect.

View 7 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++ :: 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/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 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 :: 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







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