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
ADVERTISEMENT
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
View Related
Apr 3, 2014
How can i write a function that will read an "unsigned integer" into a variable of type "unsigned short int"? i can not use cin >> inside the function.. so i am looking for atleast a hint!
View 16 Replies
View Related
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
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
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
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
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
Jul 12, 2013
In windows thread function not a void (as pthread in Linux).
Its type is DWORD(unsigned int) so my question is
How to return a struct (contain alot of different data types) from DWORD (unsigned int) thread in windows?
Code:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
typedef struct {
char* name;
int id;
[Code] .......
View 12 Replies
View Related
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
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
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
Dec 16, 2012
In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.
void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}
View 4 Replies
View Related
Oct 29, 2014
I was going through the following statement in a c book:
"interpreting -1 as unsigned int leads to a big +ve number"
-1 is already unsigned .... then what does the above statement mean ??
View 3 Replies
View Related
Oct 29, 2012
Here is the code:
int unsigned long a,b;
scanf("%lu",&a);
scanf("%lu",&b);
printf("%lu",a*b);
suppose we input a very large number on a and b (no more than 1000 digit)
like a = 1234567890123456789012345678901234567890
and b = 9876543210987654321098765432109876543210
it outputs 1, why is it?
It is probably the answer is too big, is there any way to show a*b?
View 1 Replies
View Related
Feb 23, 2014
I get the following error in XCode whenever I try to access the member I created 'randomGen' in a separate class in a different header file. I have made sure to include the header file and have tried to access it through an object.
This is the code I enter when trying to access the method from randomiser.h in main.cpp. It is also an overloaded function with doubles and integers:
RandomG randomiser;
randomiser.randomGen(); // 'Call to member function 'randomGen' is ambiguous'
This is the code inside randomiser.h:
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class RandomG {
[Code] ....
This is the error inside xcode: [URL] ....
I have tried seperating the code for the functions in another class (main.cpp) and then running and it seems to works, so I'm not sure why I can't put everything in the .h file and then access it?
I would like it in a seperate file so it doesn't clutter my main. I am writing a game with SDL so that might be confusing and I would like the window to have a random title and other random properties, so it would be easier to use a function.
View 3 Replies
View Related
Feb 3, 2013
here's one more thing id like to do to make the input even better able to handle user error, but im not sure if its possible or at least easy. I need the function to return a large positive number. As of right now, it can handle users entering characters, but what if the user enters a negative number? is there a way to check to see if what is coming in is negative before the sign gets lost in conversion to unsigned"ness"?
Code:
unsigned long getNum(char prompt[80])
{
unsigned long darts;
printf("%s", prompt);
while((scanf("%lu", &darts)) != 1)
{
[code]....
View 3 Replies
View Related
Mar 21, 2013
If I wanted to call a member function inside another member function, how would I do that, if it's possible?
For example, if I have Find(int key) defined already and wanted to call it while i was overloading operator+.
View 2 Replies
View Related
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
Mar 24, 2015
I have the following code, but it crashes on the "data = " line...
Code:
void Test(string& data) {
unsigned char* msg = (unsigned char*)malloc(MAX_LENGTH));
...
data = string(reinterpret_cast<const char*>(msg), MAX_LENGTH);
}
I know I could just return string, but what is wrong with this code that is making it crash?
View 6 Replies
View Related
Dec 28, 2012
#include <iostream>
class Hello {
public:
void Test() {
[Code].....
As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .
View 5 Replies
View Related
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
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
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
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
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