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. 6 1238 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) {
calculateSomeValue(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 calculateSomeValue to
make it work, let us know what and why.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
On Jun 24, 9:47*pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.netwrote:
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) {
* * * * *calculateSomeValue(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)
{
calculateSomeValue(a[i][j]);
a[i][j] = -1;
fun(a);
}
}
Greg
Greg Herlihy wrote:
On Jun 24, 9:47 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.netwrote:
>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) { calculateSomeValue(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)
{
calculateSomeValue(a[i][j]);
a[i][j] = -1;
fun(a);
}
}
Greg
Lets walk through the program shall we...
first, calculateSomeValue(a[0][0]) gets called.
a[0][0] = -1;
call to fun(a)
a[0][0] == -1, so it skips
calculateSomeValue(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
calculateSomeValue 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/>
>
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)
* * * * * * * * {
* * * * * * * * * * calculateSomeValue(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)
{
// calculateSomeValue(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);
}
************************************************** **************************** 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) { calculateSomeValue(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)
{
// calculateSomeValue(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/>
On Jun 27, 6:15*am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.netwrote:
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)
* * * * * * * * {
* * * * * * * * * * calculateSomeValue(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)
* * * * * *{
// * * * * * * * * calculateSomeValue(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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,...
|
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. ...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |