problem with recursion

i need to buld a recursion that will bring back information about num

Page 1 of 1

2 Replies - 534 Views - Last Post: 02 January 2009 - 07:56 AM Rate Topic: -----

#1 rafi1082  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 02-January 09

problem with recursion

Posted 02 January 2009 - 05:24 AM

the program sepose to check if the number of times that a digit is shown in a number is an odd or a pair times and print 1 or 0 in a match
i am getting this error
Error 1 error C2664: 'time' : cannot convert parameter 1 from 'int [250]' to 'int' c:\c\141\141\141.cpp 36 141

#include "stdafx.h"
#include "iostream"
using namespace std; 
#define SIZE 250

int exict(int a,int b) // check if dig is in the array
{
	if (a==b)
		return 1;
}

int time (int num, int min, int max, int dig) //recurcion
{
	int count;
	if (min==max+1)
		count++;
		
	return exict(time(num, min,((min+max)/2),dig),time(num,((1+max+min)/2),max,dig));  
}

void main() 
{
	int i,k=0,result,size,dig;
	int num[SIZE]={0};
	
	//input
	cout<<"Please enter a number: ";
	cin>>num[SIZE];
	cout<<"Please enter a digit: ";
	cin>>dig;
	for (i=0; i<SIZE; i++)//gets the last loaction of number from the array
	{
		if (k<num[i])
			k=num[i];
	}
	result=time(num,0,k,dig);
	if (result%2)
		cout<<1<<"\n";
	else
		cout<<0<<"\n";
}


Is This A Good Question/Topic? 0
  • +

Replies To: problem with recursion

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: problem with recursion

Posted 02 January 2009 - 05:48 AM

Your time function signature int time (int num, int min, int max, int dig) //recurcion takes first parameter as integer, but you are sending array from main in call, that's the problem in there. [You need some modifications in that code to make it work with array... I don't thing this one will work even if you make it work by modifying first parameter.]
Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: problem with recursion

Posted 02 January 2009 - 07:56 AM

Just about to look over your code, but in the mean time, what's this?
#include "iostream"
"" is for header files which are in the same directory as your project.

Did you mean #include <iostream>?

Oh, and main should always return int, not void. :)

EDIT:
Right, got it.

It's because you're defining num as an array in your main function, but then you try to pass it to a function which accepts a single int.

I haven't tested it, but did you mean something like int time (int num[], int min, int max, int dig) //recurcion?
Passing an array[] instead of a single int.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1