12,354,716 members (60,859 online)
Rate this:
See more:
Hello,

Need a code for implementation of a recursion formula. Formula is:

j*(q(j) = Sum (a(i)*b(i)*q(j-b(i))
where i=1 to 2 and j=1 to 6.

Thanks

```#include <stdio.h>
main(){
int a[2],b[2],k[2];
float s[2],q[6];
int i,j;
float sum;
a[0]=0;
b[0]=0;
for (i=0;i<2;i++){
printf("Call category %d\n",i+1);
printf("Input a%d ",i+1);
scanf("%f", &a[i]);
printf("Input b%d ",i+1);
scanf("%f", &b[i]);
}
q[0]=1;
for(j=1;j<7;j++){
sum=0;
k[0]=0;
k[1]=0;
s[0]=0;
s[1]=0;
for (i=0;i<2;i++){
k[i]=j-b[i];
if (k[i]<0){
q[k[i]]=0;
}
if (k[i]==0){
q[k[i]]=1;
}
s[i]=a[i]*b[i]*q[k[i]];
sum=sum+s[i];
}
q[j]=sum/j;
}
printf("\nq[0]= 1\n");
for (j=1;j<7;j++){
printf("q[%d]= %f\n",j,q[j]);
}
scanf("\n");
}```
Posted 18-Dec-12 0:20am
Edited 18-Dec-12 1:38am
v2
Jochen Arndt 18-Dec-12 7:00am

This can't be directly coded in C++ due to missing information:
1. Sum() is usually a function that adds the passed parameters but your equation has only one parameter
2. a(i), b(i) can be interpreted as functions or arrays (a[i], b[i])
3. A recursion requires a stop condition (max. iterations and/or a value is below a limit; limit is often named epsilon)
4. Missing indication of number type (int, float, double)
Member 9694908 18-Dec-12 7:05am

Please provide code as an example
Jochen Arndt 18-Dec-12 7:35am

This can't be directly coded in C++.
pwasser 18-Dec-12 7:18am

To use this forum you need to:

1. Pose your question clearly and unambiguously.
2. Provide the code that you have created in an attempt to solve the problem.

Why don't you start by explaining your problem clearly. That will require more than throwing up a formula.
Member 9694908 18-Dec-12 7:22am

This is my code:
#include
main(){
int a[2],b[2],k[2];
float s[2],q[6];
int i,j;
float sum;
a[0]=0;
b[0]=0;
for (i=0;i<2;i++){
printf("Call category %d\n",i+1);
printf("Input a%d ",i+1);
scanf("%f", &a[i]);
printf("Input b%d ",i+1);
scanf("%f", &b[i]);
}
q[0]=1;
for(j=1;j<7;j++){
sum=0;
k[0]=0;
k[1]=0;
s[0]=0;
s[1]=0;
for (i=0;i<2;i++){
k[i]=j-b[i];
if (k[i]<0){
q[k[i]]=0;
}
if (k[i]==0){
q[k[i]]=1;
}
s[i]=a[i]*b[i]*q[k[i]];
sum=sum+s[i];
}
q[j]=sum/j;
}
printf("\nq[0]= 1\n");
for (j=1;j<7;j++){
printf("q[%d]= %f\n",j,q[j]);
}
scanf("\n");
}
Stefan_Lang 18-Dec-12 7:42am

On a sidenote, that code may crash due to accessing q[6] towards the end: you defined q as a 6-element array, i. e. with elements from 0 through 5 only! even if it doesn't crash, you may be overwriting some variable in your stack, resulting in unexpected behaviour.

Also, this part:
```if (k[i]<0){ q[k[i]]=0; }```
accesses q by a negative index!?
Member 9694908 18-Dec-12 8:44am

If you can find a solution for my code because it crashes, please provide one.
Thank you,
Stefan_Lang 18-Dec-12 12:37pm

Again: I saw this response only because I happened to check and wondered why you didn't answer!

We cannot help you if you don't tell us what that code is supposed to do!

Rate this:

## Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
Rate this:

## Solution 2

Need a direction for solving this problem. If you have any please provide.
Thank you
Richard MacCutchan 18-Dec-12 6:50am

This is mathematics not programming.
Philippe Mori 18-Dec-12 20:10pm

You need to give us more complete information if you hope to get some help. Properly write the formula and if necessary put an image of the formula (or maybe a link to a web site). Also having an idea of the purpose of the formula could help.
Rate this:

## Solution 3

What is the right code?
How can I change this part.
if (k[i]<0){
q[k[i]]=0;
}
Stefan_Lang 18-Dec-12 8:41am

First of all - Learn to use this forum correctly:
- When you wish to reply to a comment (such as this one), use the green 'Reply' link at the top right of that particular comment
- When you wish to reply to a solution (such as what you have just written), use the blue 'Have Question or Comment' link at the bottom of that particular solution.
- When you have a solution to the original question - and only then - enter your solution in the "Add your solution here" Edit box at the bottom of the page!

Your question is NOT a solution, and the person you're responding to (I) did not get a notification of your question! It was just by chance I saw your addition.

As to your question: how should we know? I can only tell you this will crash, not how it's supposed to be.
Philippe Mori 18-Dec-12 20:07pm

It is hard to tell what is the right code... since your formula is not properly written `j*(q(j) = Sum (a(i)*b(i)*q(j-b(i))` and we have no idea of what it is supposed to do.

One thing that is usre is that if k[i] is less than 0, then it cannot be a valid index for q[...].

What to do in such a case dépends on the problem. If the bad index is because of invalid data, then maybe you want to display a message to the user and quit. In other situation, you might simply want to ignore cases where the index is negative.

Top Experts
Last 24hrsThis month
 OriginalGriff 474 Member 12599256 383 CHill60 128 Sergey Alexandrovich Kryukov 108 Andreas Gieriet 105
 OriginalGriff 9,778 Sergey Alexandrovich Kryukov 6,481 Dave Kreskowiak 3,249 ppolymorphe 2,441 Karthik Bangalore 2,266