Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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


P.S. (added from comments):
C++
#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
Updated 18-Dec-12 1:38am
v2
Comments
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++.
[no name] 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 <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");
}

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!
 
Share this answer
 
Need a direction for solving this problem. If you have any please provide.
Thank you
 
Share this answer
 
Comments
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.
What is the right code?
How can I change this part.
if (k[i]<0){
q[k[i]]=0;
}
 
Share this answer
 
Comments
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.

So, please, in your own interest, stop posting your responses as solutions!

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900