Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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):
#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
Comments
Jochen Arndt at 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 at 18-Dec-12 7:05am
   
Please provide code as an example
Jochen Arndt at 18-Dec-12 7:35am
   
This <b>can't</b> be directly coded in C++.
pwasser at 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 at 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&lt;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&lt;7;j++){ sum=0; k[0]=0; k[1]=0; s[0]=0; s[1]=0; for (i=0;i&lt;2;i++){ k[i]=j-b[i]; if (k[i]&lt;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&lt;7;j++){ printf("q[%d]= %f\n",j,q[j]); } scanf("\n"); }</stdio.h>
Stefan_Lang at 18-Dec-12 7:42am
   
I have added your code to the original question. 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: <code>if (k[i]&lt;0){ q[k[i]]=0; }</code> accesses q by a negative index!?
Member 9694908 at 18-Dec-12 8:44am
   
If you can find a solution for my code because it crashes, please provide one. Thank you,
Stefan_Lang at 18-Dec-12 12:37pm
   
We can't help you if you ignore the advice already given and don't provide the information we asked for. 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: bad
good
Please Sign up or sign in to vote.

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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Need a direction for solving this problem. If you have any please provide.
Thank you
  Permalink  
Comments
Richard MacCutchan at 18-Dec-12 6:50am
   
This is mathematics not programming.
Philippe Mori at 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: bad
good
Please Sign up or sign in to vote.

Solution 3

What is the right code?
How can I change this part.
if (k[i]<0){
q[k[i]]=0;
}
  Permalink  
Comments
Stefan_Lang at 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 at 18-Dec-12 20:07pm
   
It is hard to tell what is the right code... since your formula is not properly written <code>j*(q(j) = Sum (a(i)*b(i)*q(j-b(i))</code> 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)

  Print Answers RSS
0 Dnyaneshwar@Pune 1,300
1 Sergey Alexandrovich Kryukov 667
2 Tadit Dash 350
3 ravikhoda 300
4 Manas Bhardwaj 240
0 Sergey Alexandrovich Kryukov 11,224
1 OriginalGriff 6,897
2 Peter Leow 4,795
3 Abhinav S 3,838
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid