473,566 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive Function

As discussion last time, I am thinking if it is possible to change the
recursive function from:

void fun(Array a){
for (int i=0; i<MAX1; i++)
for(int j=0; j<MAX2; j++){
if ( a[i][j] != -1) {
/*... using a[i][j] to calculate some value...*/
a[i][j]=-1;
fun(a);
}
}

to:

void fun(Array& a){
/*......*/
}

and make sure the result will be same.
Jun 27 '08 #1
6 1243
cp************* **@gmail.com wrote:
As discussion last time, I am thinking if it is possible to change the
recursive function from:

void fun(Array a){
for (int i=0; i<MAX1; i++)
for(int j=0; j<MAX2; j++){
if ( a[i][j] != -1) {
/*... using a[i][j] to calculate some value...*/
a[i][j]=-1;
fun(a);
}
}

to:

void fun(Array& a){
/*......*/
}

and make sure the result will be same.
Its depends.
If you could re-write it as:

void fun(Array a) {
for (int i=0; i<MAX1; i++) {
for (int j=0; j<MAX2; j++) {
if ( a[i][j] != -1) {
calculateSomeVa lue(a[i][j]);
a[i][j]=-1;
fun(a);
}
}
}
}

Then it would be trivial. Just remove the "fun(a)" call, since it isn't
actually doing anything productive.

Now, if you need to pass more information into calculateSomeVa lue to
make it work, let us know what and why.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Jun 27 '08 #2
On Jun 24, 9:47*pm, Daniel Pitts
<newsgroup.spam fil...@virtuali nfinity.netwrot e:
cplusplusquest. ..@gmail.com wrote:
As discussion last time, I am thinking if it is possible to change the
recursive function from:
void fun(Array a){
* * for (int i=0; i<MAX1; i++)
* * * * for(int j=0; j<MAX2; j++){
* * * * * * * *if ( a[i][j] != -1) {
* * * * * * * * * */*... using a[i][j] to calculate some value...*/
* * * * * * * * * *a[i][j]=-1;
* * * * * * * * * *fun(a);
* * * *}
}
to:
void fun(Array& a){
* */*......*/
}
and make sure the result will be same.

Its depends.
If you could re-write it as:

* void fun(Array a) {
* * for (int i=0; i<MAX1; i++) {
* * *for (int j=0; j<MAX2; j++) {
* * * *if ( a[i][j] != -1) {
* * * * *calculateSomeV alue(a[i][j]);
* * * * *a[i][j]=-1;
* * * * *fun(a);
* * * *}
* * *}
* * }
* }

Then it would be trivial. Just remove the "fun(a)" call, since it isn't
actually doing anything productive.
How do you figure? Without knowing how "Array" is defined, it is not
possible for us to tell whether the call to fun(a) serves any useful
purpose or not. For example, one could imagine a more complete program
fragment - and one for which it is clear that the call to fun(a) is
useful:

const int MAX1 = 16;
const int MAX2 = 32;

typedef int (&Array)[MAX1][MAX2];

void fun(Array a)
{
for (int i = 0; i < MAX1; i++)
for (int j = 0; j < MAX2; j++)
if (a[i][j] != -1)
{
calculateSomeVa lue(a[i][j]);
a[i][j] = -1;
fun(a);
}
}

Greg
Jun 27 '08 #3
Greg Herlihy wrote:
On Jun 24, 9:47 pm, Daniel Pitts
<newsgroup.spam fil...@virtuali nfinity.netwrot e:
>cplusplusquest ...@gmail.com wrote:
>>As discussion last time, I am thinking if it is possible to change the
recursive function from:
void fun(Array a){
for (int i=0; i<MAX1; i++)
for(int j=0; j<MAX2; j++){
if ( a[i][j] != -1) {
/*... using a[i][j] to calculate some value...*/
a[i][j]=-1;
fun(a);
}
}
to:
void fun(Array& a){
/*......*/
}
and make sure the result will be same.
Its depends.
If you could re-write it as:

void fun(Array a) {
for (int i=0; i<MAX1; i++) {
for (int j=0; j<MAX2; j++) {
if ( a[i][j] != -1) {
calculateSomeVa lue(a[i][j]);
a[i][j]=-1;
fun(a);
}
}
}
}

Then it would be trivial. Just remove the "fun(a)" call, since it isn't
actually doing anything productive.

How do you figure? Without knowing how "Array" is defined, it is not
possible for us to tell whether the call to fun(a) serves any useful
purpose or not. For example, one could imagine a more complete program
fragment - and one for which it is clear that the call to fun(a) is
useful:

const int MAX1 = 16;
const int MAX2 = 32;

typedef int (&Array)[MAX1][MAX2];

void fun(Array a)
{
for (int i = 0; i < MAX1; i++)
for (int j = 0; j < MAX2; j++)
if (a[i][j] != -1)
{
calculateSomeVa lue(a[i][j]);
a[i][j] = -1;
fun(a);
}
}

Greg

Lets walk through the program shall we...
first, calculateSomeVa lue(a[0][0]) gets called.
a[0][0] = -1;
call to fun(a)

a[0][0] == -1, so it skips
calculateSomeVa lue(a[0][1]) gets caled
a[0][1] = -1;
call to fun(a)
a[0][0] and a[0][1] get skipped.... See where I'm going? If you don't
call fun(a), the same calculations will get done, minus the overactive
recursion and extra comparisons to -1

So, the call to fun(a) is still not useful. If you replace
calculateSomeVa lue with std::cout << i << ", " << j << ": " << a[i][j]
<< std::endl; You will see that the output is the same with or without
fun(a), but significantly more efficient without it.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Jun 27 '08 #4
>
How do you figure? Without knowing how "Array" is defined, it is not
possible for us to tell whether the call to fun(a) serves any useful
purpose or not. For example, one could imagine a more complete program
fragment - and one for which it is clear that the call to fun(a) is
useful:

* * const int MAX1 = 16;
* * const int MAX2 = 32;

* * typedef int (&Array)[MAX1][MAX2];

* * void fun(Array a)
* * {
* * * * for (int i = 0; i < MAX1; i++)
* * * * * * for (int j = 0; j < MAX2; j++)
* * * * * * * * if (a[i][j] != -1)
* * * * * * * * {
* * * * * * * * * * calculateSomeVa lue(a[i][j]);
* * * * * * * * * * a[i][j] = -1;
* * * * * * * * * * fun(a);
* * * * * * * * }
* * }

Greg
Here is my example code:

*************** *************** *************** *************** *************** *****
#include <iostream>

using namespace std;

struct Array{
int n[3][3];
};

void fun(Array a)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (a.n[i][j] != -1)
{
// calculateSomeVa lue(a[i][j]);
cout << i << " " << j << " " << a.n[i][j] << endl;
a.n[i][j] = -1;
fun(a); //with it and without it will be different
}
}

int main(){
Array b;

for(int i=0; i<3; i++)
for(int j=0; j<3; j++){
b.n[i][j] = i+j;
}

fun(b);
}
*************** *************** *************** *************** *************** ***
Jun 27 '08 #5
cp************* **@gmail.com wrote:
>How do you figure? Without knowing how "Array" is defined, it is not
possible for us to tell whether the call to fun(a) serves any useful
purpose or not. For example, one could imagine a more complete program
fragment - and one for which it is clear that the call to fun(a) is
useful:

const int MAX1 = 16;
const int MAX2 = 32;

typedef int (&Array)[MAX1][MAX2];

void fun(Array a)
{
for (int i = 0; i < MAX1; i++)
for (int j = 0; j < MAX2; j++)
if (a[i][j] != -1)
{
calculateSomeVa lue(a[i][j]);
a[i][j] = -1;
fun(a);
}
}

Greg

Here is my example code:

*************** *************** *************** *************** *************** *****
#include <iostream>

using namespace std;

struct Array{
int n[3][3];
};

void fun(Array a)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (a.n[i][j] != -1)
{
// calculateSomeVa lue(a[i][j]);
cout << i << " " << j << " " << a.n[i][j] << endl;
a.n[i][j] = -1;
fun(a); //with it and without it will be different
}
}

int main(){
Array b;

for(int i=0; i<3; i++)
for(int j=0; j<3; j++){
b.n[i][j] = i+j;
}

fun(b);
}
*************** *************** *************** *************** *************** ***
ah HAH!
I was assuming that Array had object semantics, not value semantics...

Now, if you change
void fun(Array a)
to
void fun(Array &a)

You will see that calling fun(a) makes no difference.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Jun 27 '08 #6
On Jun 27, 6:15*am, Daniel Pitts
<newsgroup.spam fil...@virtuali nfinity.netwrot e:
cplusplusquest. ..@gmail.com wrote:
How do you figure? Without knowing how "Array" is defined, it is not
possible for us to tell whether the call to fun(a) serves any useful
purpose or not. For example, one could imagine a more complete program
fragment - and one for which it is clear that the call to fun(a) is
useful:
* * const int MAX1 = 16;
* * const int MAX2 = 32;
* * typedef int (&Array)[MAX1][MAX2];
* * void fun(Array a)
* * {
* * * * for (int i = 0; i < MAX1; i++)
* * * * * * for (int j = 0; j < MAX2; j++)
* * * * * * * * if (a[i][j] != -1)
* * * * * * * * {
* * * * * * * * * * calculateSomeVa lue(a[i][j]);
* * * * * * * * * * a[i][j] = -1;
* * * * * * * * * * fun(a);
* * * * * * * * }
* * }
Greg
Here is my example code:
*************** *************** *************** *************** *************** *****
#include <iostream>
using namespace std;
struct Array{
* *int n[3][3];
};
void fun(Array a)
{
* *for (int i = 0; i < 3; i++)
* *for (int j = 0; j < 3; j++)
* * * * * *if (a.n[i][j] != -1)
* * * * * *{
// * * * * * * * * calculateSomeVa lue(a[i][j]);
* * * * * * * * * *cout << i << " " << j << " " << a.n[i][j] << endl;
* * * * * * * * * *a.n[i][j] = -1;
* * * * * * * * * *fun(a); * //with it and without it will be different
* * * * * *}
}
int main(){
* *Array b;
* *for(int i=0; i<3; i++)
* * * * * *for(int j=0; j<3; j++){
* * * * * * * * * *b.n[i][j] = i+j;
* * * * * *}
* *fun(b);
}
*************** *************** *************** *************** *************** ***

ah HAH!
I was assuming that Array had object semantics, not value semantics...

Now, if you change
void fun(Array a)
to
void fun(Array &a)

You will see that calling fun(a) makes no difference.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
passing with value and passing with reference, the result is different.
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2877
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays a sequence of spaces; recursive function 2 uses input to display ascending sequence of digits; likewise, recursive function 3 uses input to display...
4
2420
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2, opt1=opt1, opt2=opt2, opt3=opt3) return if type(arg2) is ListType:
4
9037
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic...
9
13174
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole stack and continue execution at the point of the initial call. Is that possible? Thanks, Bill
9
16805
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script type='text/javascript'> function myFunc(lev) { // if (lev) return myFunc(lev-1); var aStack=; nextFunc = arguments.callee;
41
3332
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions are that no local variable or global variable should be used.I have to use recursive functions.
10
2541
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive search....would any of you be so kind to explain it to me...maybe include some examples. I would GREATLY appreciate it!!!
4
2117
by: ThEoNeAnDOnLy | last post by:
I recently had an issue with my recursive project in class. Here is the code. // Recursion.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <conio.h> #include <iostream> using namespace std;
3
4223
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular reference, which means it is only collected by the mark-and-sweep collector, not by reference counting. Here is some code that demonstrates it: ===...
3
2331
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any other choice? For example, def func(self, x, y, A, B, C): #x, y change in recursive call
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.