Thread: Trying to do a summation but not getting a value. (Rookie programmer)

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    6

    Trying to do a summation but not getting a value. (Rookie programmer)

    Code:
     done =0;   n = presentvalue = 0;
    
    
    
    
    
    
       // Main simulation loop.
       while(!done) {
            while(n<100000){
    
    
          setFlagA=setFlagBBB=setFlagBB=setFlagB=setFlagC=0;
    
    
          // Initialize credit rating.
          i = credit0;
    
    
          // Determine the month k of the bond's last cash flow. Initialize to 0.
          k = 0;
    
    
          // Evolve the credit rating month by month to the bond's maturity.
          while (1) {
    
    
             // Current credit rating is "i", updated rating will be "j".
    
    
             // Determine new credit rating via inverse transform.
             U = MTUniform (0);
             int jmin;
             jmin = (i >= 1 ? i-1 : 0);
             for (j = jmin; j <= 8; j++) {
                if (U <= P[i][j]) {
                   break;
                }
             }
    
    
             // If not yet defaulted, issuer survives one more month.
             if (j != 8) {
                k ++;
             }
    
    
             if (k==maturity){
                    n++;
                    break;
             }
    
    
             if (j == 8) {
                    presentvalue=(20000000 * exp(-r*k*1/12))+presentvalue;
                    n++;
                break;
    
    
             while (i>j){
                 if (j==3 && setFlagA==0){
                       presentvalue+=20000000 * exp(-r*k*1/12);
                       setFlagA==1;
                       break;
             }
                else if (j==4 && setFlagBBB==0){
    
    
                    presentvalue+= 20000000 * exp(-r*k*1/12);
                    setFlagBBB==1;
                    break;
                }
                else if(j==5 && setFlagBB==0){
    
    
                         presentvalue+= 20000000 * exp(-r*k*1/12);
                         setFlagBB==1;
                         break;
                    }
                    else if(j==6 && setFlagB==0){
    
    
                             presentvalue+= 20000000 * exp(-r*k*1/12);
                             setFlagB==1;
                             break;
                        }
                        else if (j==7 && setFlagC==0){
    
    
                                 presentvalue+= 20000000 * exp(-r*k*1/12);;
                                 setFlagC==1;
                                 break;
    
    
                }
             }
    
    
             // Update the issuer's current credit rating.
             i = j;
    
    
          }
            }
    
    
    
    
             }
    printf("The present value is %8.3f\n",presentvalue/n);
          }
    
    
     if (presentvalue>0) {
                done = 1;
       }
    
    
       // Pause before closing up window.
       Exit ();
    
    
    }


    The code is meant to sum the present value of a credit swap payout by using RNGs. So, I am running a loop to see if the bond rating dropped to a new low and if it does I sum the PV. But my output is 0.0000, and it constantly repeats without stopping.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,148
    Your code has atrocious formatting. Perhaps we could fix it to something like this:
    Code:
        done = 0;
        n = presentvalue = 0;
    
        // Main simulation loop.
        while (!done) {
            while (n < 100000) {
    
                setFlagA = setFlagBBB = setFlagBB = setFlagB = setFlagC = 0;
    
                // Initialize credit rating.
                i = credit0;
    
                // Determine the month k of the bond's last cash flow. Initialize to 0.
                k = 0;
    
                // Evolve the credit rating month by month to the bond's maturity.
                while (1) {
                    // Current credit rating is "i", updated rating will be "j".
                    // Determine new credit rating via inverse transform.
                    U = MTUniform(0);
                    int jmin;
                    jmin = (i >= 1 ? i - 1 : 0);
                    for (j = jmin; j <= 8; j++) {
                        if (U <= P[i][j]) {
                            break;
                        }
                    }
                    // If not yet defaulted, issuer survives one more month.
                    if (j != 8) {
                        k++;
                    }
                    if (k == maturity) {
                        n++;
                        break;
                    }
                    if (j == 8) {
                        presentvalue = (20000000 * exp(-r * k * 1 / 12)) + presentvalue;
                        n++;
                        break;
                        while (i > j) {
                            if (j == 3 && setFlagA == 0) {
                                presentvalue += 20000000 * exp(-r * k * 1 / 12);
                                setFlagA == 1;
                                break;
                            }
                            else if (j == 4 && setFlagBBB == 0) {
                                presentvalue+= 20000000 * exp(-r * k * 1 / 12);
                                setFlagBBB == 1;
                                break;
                            }
                            else if (j == 5 && setFlagBB == 0) {
                                presentvalue+= 20000000 * exp(-r * k * 1 / 12);
                                setFlagBB == 1;
                                break;
                            }
                            else if (j == 6 && setFlagB == 0) {
                                presentvalue += 20000000 * exp(-r * k * 1 / 12);
                                setFlagB == 1;
                                break;
                            }
                            else if (j == 7 && setFlagC == 0) {
                                presentvalue += 20000000 * exp(-r * k * 1 / 12);
                                setFlagC == 1;
                                break;
                            }
                        }
                        // Update the issuer's current credit rating.
                        i = j;
                    }
                }
            }
            printf("The present value is %8.3f\n", presentvalue / n);
        }
    
        if (presentvalue > 0) {
            done = 1;
        }
        // Pause before closing up window.
        Exit();
    }
    Now, with this better formatting, two likely mistakes present themselves:
    • exp(-r * k * 1 / 12) might result in integer division. You probably should have written: exp(-r * k / 12.0). This should be saved in a variable since it appears repeatedly without change. Actually, the fact that the declarations of r and k are not available hints that your function is too long. You should write functions that do one thing and do it well.
    • This has no net effect:
      Code:
      setFlagA == 1;
      You probably wanted:
      Code:
      setFlagA = 1;
      The same goes for the other flags. Actually, the flags don't seem to have descriptive names, and furthermore you are using magic numbers with your j == 3, j == 5, etc, comparisons.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    That was very helpful. I really appreciate it. I apologize for the awful organization. I am receiving an error for the final line that there are conflicting exit types. How can I end this loop without such an issue?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,148
    Quote Originally Posted by Connor Joseph
    I am receiving an error for the final line that there are conflicting exit types. How can I end this loop without such an issue?
    What is the exact error message? Perhaps you declared Exit differently from how you used it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    6
    I receive "warning: data definition has no type" and conflicting types for Exit, on the line for Exit();

    Also, I receive expected identifier or ( before if" on

    if(presentvalue > 0) { done = 1;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,148
    Quote Originally Posted by Connor Joseph
    I receive "warning: data definition has no type" and conflicting types for Exit, on the line for Exit();
    What is the declaration for Exit?

    Quote Originally Posted by Connor Joseph
    Also, I receive expected identifier or ( before if" on

    if(presentvalue > 0) { done = 1;
    Post the smallest and simplest program that you expect should compile but which demonstrates this error. The error probably lies elsewhere, e.g., because of your poor formatting, you have an extra or missing brace, that sort of thing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    I think your missing closing brace is for ending all the code within this
    block. At first it looked like the while loop block but that is terminated
    correctly. Again - take a look and see if you can spot it.

    if (j == 8) {
    presentvalue = (20000000 * exp(-r * k * 1 / 12)) + presentvalue;
    n++;
    break;
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Summation of N numbers
    By ARYAA in forum C Programming
    Replies: 30
    Last Post: 01-03-2016, 05:21 AM
  2. matrix summation
    By chaklader in forum C Programming
    Replies: 19
    Last Post: 05-30-2010, 03:51 PM
  3. summation in c
    By bertazoid in forum C Programming
    Replies: 24
    Last Post: 10-20-2008, 02:52 PM
  4. partially summation
    By joerg in forum C Programming
    Replies: 0
    Last Post: 11-23-2007, 10:47 AM
  5. Help With Simple Summation...
    By TOPFLOR in forum C Programming
    Replies: 19
    Last Post: 08-09-2006, 10:50 AM