473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Program

>In an intrivew I had informed to add a array content using recurtion .

I wrote like this

void sum(int a[],int n)
{
static sum;
if (n==0)
return;

sum+=a[n-1];
sum(a,n-1);
}
i think it will work then he told to write without static variable.

then I wrote like this
int sum(int a[],int n)
{
if(n==0)
return 0;

return a[n-1]+sum(a,n-1);
}
then he told you are gone some where wrong. and told me to write whith
out using size of the array.
On that time i could not write program, but I told him that i can
limit by specifing some end character to inform the end of limit.

Now i think It would be a right program

int sum(int a[])
{
if(a[0]==-1) /* end of character I assume to be -1*/
return 0;
return a[0]+sum(++a);
}
!! Is it a right program and give me the right result !!

Aug 17 '05 #1
3 1085
kolari wrote:
Now i think It would be a right program

int sum(int a[])
{
if(a[0]==-1) /* end of character I assume to be -1*/
return 0;
return a[0]+sum(++a);
}
!! Is it a right program and give me the right result !!


I don't know if this is relevant here or not but even if your compiler is
able to optimize tail-recursion, it cannot do it here. So depending on the
size of a[] you will run out of stack space quite fast.

tail recursive would be (not tested :-) :

int sum(int *a, int erg)
{
if (*a == -1) return erg;
return sum(a+1, erg + *a);
}

call it with: sum(data,0)

Michael
Aug 17 '05 #2
int n = (sizeof a) / (sizeof a[0]);

will retrun you the size of the array instead of sending it through
explicitly

And your function works just fine, as far as I can see
int sum(int a[],int n)
{
if (n)
return a[n-1]+sum(a,n-1);
else
return 0;
}

Aug 17 '05 #3
that was a good idia to find the size of a array . but sum of array I
have to increment the base address which is possible in called
function.

i.e

....
....
....
....sum(++a)..
....
....

Aug 18 '05 #4

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

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.