Thread: Need help on loop!

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Need help on loop!

    This is what its suppose to look like for the output:
    --------------------------------------------------------------------------
    Year Value Interest Payment Amt Retired
    1 100000.00 6000.00 13586.80 8000.00
    2 92000.00 5520.00 13526.05 8000.00
    3 84000.00 5040.00 13527.03 8000.00
    .
    .
    .
    10 13000.00 780.00 13780.01 13000.00
    --------------------------------------------------------------------------
    =====Enter values are 100bonds, $1000 each, 6% interestRate and 10 years

    value = bond * bondprice

    P = value*R/(1-(1/(1+R)^N)) R = rate of interest
    N = number of years left
    P = 13586.80

    interest = R * value =.06 * 100000 = 6000
    Number Retired = nearest integer of (P-interest)/bondprice
    = nearest integer (7586.80/1000) = 8



    I need help on the for loop calculations because after i run the for loop in main the number wouldnt come out right. I needs some help on how to fix this could anyone help
    me?







    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    int input(float*, int*, float*);
    float calculation(float, int, float, int, float*, float*, int, float*);
    
    float subCalculation(float, float, float);
    
    int main(void)
    { // variable declaration
        float bondPrice;
        int initBond;
        float intRate; 
        int numYear;
        float value;
        float annualPay;
        float amtRetired;
        float interest;
        int N;
    
        N = 0;
    
    //function call to input
        numYear = input(&bondPrice, &initBond, &intRate);
        printf("YEAR VALUE INTEREST PAYMENT AMT RETIRED\n");
        for(N = 0; numYear - N >= 0; N++)
            {
                 annualPay = calculation(bondPrice, initBond, intRate, numYear, &value, &interest, N, &amtRetired);
                 printf("%d %8.2f %8.2f %8.2f %8.2f\n", N, value, interest, annualPay, amtRetired * bondPrice);
             }
    
        system("PAUSE");
        return 0;
    }
    //main
    ////////////////////////////////////////////////////////////////////////////////
    int input(float* price, int* bond, float* interestRate)
    {
    // variable declaration
        int years;
    //prompt user input for all listed in function "Pre"
        printf("Please enter price per bond: ");
        scanf("%f", price);
        printf("\nPlease enter the initial number of bonds: ");
        scanf("%d", bond);
        printf("\nPlease enter the interest rate: ");
        scanf("%f", interestRate);
        printf("\nPlease enter the number of years the debt is to be paid: ");
        scanf("%d", &years);
    //return years
        return years;
    }
    //input
    ////////////////////////////////////////////////////////////////////////////////
    float calculation(float price, int bond, float interestRate, int years, float* value,
        float* interest, int N, float* amtRetired)
    { // variable declaration
        int r;
        float payment;
    //calculate value of bond
        *value = (float)price * bond;
        *interest = (float)(interestRate/100) * *value;
    
    
        payment = (*value * (interestRate/100))/(1 -(1/pow(1 + (interestRate/100), years - N)));
    
        *amtRetired = subCalculation(price, payment, *interest);
    
    
    // return payment
        return payment;
    }
    //calculations
    ////////////////////////////////////////////////////////////////////////////////
    float subCalculation(float price, float payment, float interest)
    { // variable declaration
    
        float amtRetired;
        amtRetired = (payment - interest)/(price);
    
        amtRetired = floor(amtRetired +.5);
    
    
        return amtRetired;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So there are a few problems.
    First, your loop goes from 0 to 10 instead of 1 to 10. Fix your for loop to use > instead of >= and use N+1 in your printf.

    Second, you have value as bond price * number of bonds. This is the initial value, but each year, you retire a certain amount from your bond value, so value needs to decrease. Subtract amtRetired from value each time through the loop.

    Third you will need to change subCalculation to be:
    Code:
    float subCalculation(float price, float payment, float interest)
    {
        float amtRetired;
    
        amtRetired = (payment - interest)/(price);
        amtRetired = floor(amtRetired +.5) * price;  // scale this back up so we know how much we retired
    
        return amtRetired;
    
    }
    That should about do it for fixing your code. A couple design notes:
    It's generally a good idea to turn your interest rate into a decimal to avoid having to divide it by 100 everywhere (you use .06, not 6 when doing all your calcs). Just put a *interest /= 100 at the end of your input function.

    Also, calling functions calculation and subCalculation is not descriptive either in terms of naming or reflecting your solution. Calculation really does several calcs, and calls subCalculation for just one more thing. I find calculation a little difficult to read with all those dereferences for your pointer variables. Keep those functions as small as possible, even if it means a function simply subtracts or multiplies two numbers. This will make your code more readable and help you spot calculation and logic errors. Here's how I laid out my version of your program:
    Code:
        amt_retired = 0;
        value = calc_init_value(bond_price, n_bonds);
        for (...) {
            value = calc_value(value, amt_retired);
            interest = calc_interest(rate, value);
            payment = calc_payment(interest, rate, years_left);
            amt_retired = calc_amt_retired(payment, interest, bond_price);
            printf(...);
        }

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh yeah, remember to fix your printf to just use amtRetired instead of amtRetired * bondPrice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM