Thread: I really need help!!! please!

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    Exclamation I really need help!!! please!

    i hope im doing this right, im sorry i dont know anything about programming. This was my assignment:
    /*

    MAE9: Homeworki #3, Spring 2003

    Due on Thursday, April 24, before 11:00 pm



    Create a table for monthly payments of a house loans.

    Enter the price of a house from the keyboard, for example
    price = 450000.

    For annual interest rates: annual_rate = 0.045 to 0.075 with
    the increment of 0.0025, compute by using the for-loop the
    monthly payments for 15-year, 20-year, and 30-year loans.
    Print the results in the table on the screen.

    Then, copy the previous loop and modify it to calculate the
    total payments after 15, 20, and 30 years. Print results in
    another table on the screen.

    Use the formula for equal-monthly-payments:

    month_pay = price * i* (1 + i)^n/((1 + i)^n - 1)

    where i= annual_rate/12. (monthly interest rate),
    and n = the total number of payments.

    The total payment for a given loan is:

    tot = n * month_pay

    To compute (1 + i)^n use pow(1.0 + i, (double)n)

    Your output should look like this:

    price = 450000.00

    Monthly Payments

    Ann. Int. Rates 15-year loan 20-year loan 30-year loan
    ------------------------------------------------------------------
    0.0450 3442.47 2846.92 2280.08
    0.0475 3500.24 2908.01 2347.41

    ...... ....... ....... .......

    0.0750 4171.56 3625.17 3146.47



    Total Payment

    Ann. Int. Rates after 15 yr after 20 yr after 30 yr
    -----------------------------------------------------------------
    0.0450 619644.56 683261.33 820830.20
    0.0475 630043.85 697921.52 845068.69

    ...... ......... ......... .........

    0.0750 750880.01 870040.65 1132727.50

    */
    and this is my attempt:
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
    printf("\t\t\tMonthly Payments\nAnn. Int. Rates\t15-year loan\t20-year loan\t30-
    year loan\n--------------------------------------------------------------\n");
    int k;
    double r, m1, m2, m3, tot1, tot2, tot3, n, m, o, p, b;
    p=450000;
    n=180;
    m=240;
    o=360;
    for(k=1; k<=12; k++)
    {
    r = (.0450+(.0025*(k))),k;
    b = ("(r) /12."),r;
    m1 ={((p)*(b)*(1 + b) ^(n)}/{((1+b) ^(n)-1))};
    m2 ={((p)*(b)*(1 + b) ^(m)}/{((1+b) ^(m)-1))};
    m3 ={((p)*(b)*(1 + b) ^(o)}/{((1+b) ^(o)-1))};
    tot1 = (n * m1);
    tot2 = (m * m2);
    tot3 = (o * m3);
    printf("\t%1.4f\n",r);
    }
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What's the problem. Contrary to popular believe, none of us (with the exception of the Moderators) are telepathic.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    On a side note, code tags do little good if you don't bother actually indenting anything...

    Additionally, ^ is not the "power" operator. It's a XOR. You definately do not want this. (Especially since floating point numbers should have your compiler throwing fits when you try to use it.)

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    im sorry, im brand new at this im using ssh secure shell program to write this and i dont know anything about indenting. all i know is when i try to run it it keeps giving me these errors
    HW3.c: In function `main':
    HW3.c:15: incompatible types in assignment
    HW3.c:16: parse error before '{' token
    HW3.c:17: parse error before '{' token
    HW3.c:18: parse error before '{' token
    and i guess i have to use scanf or something to enter the price 450000 instead of just listing it, and every time i try that it says core dump
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
    double num1;
    printf("enter price:\n");
    scanf("%d", num1);
    printf("price = %d\n",num1);
    this is what happens:
    iacs5.ucsd.edu 50: gcc Hw3.c -lm
    iacs5.ucsd.edu 51: a.out
    enter price:
    45000
    Segmentation fault (core dumped)

    *if theres anything else i can put to help express what the problem is please tell me im sorry i dont know anything

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Scanf expects pointers. As such, you need to pass it the address of the variable:

    scanf( "%d", &myvar );

    Notice the "&" sign.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    12

    powers

    if ^ is not a power what is a power?

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    ^ is the xor if I'm not misken

    pow(a, b) along with math.h or cmath
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    I tried doing the & thing and this it still gave me a core dump, and i changed the ^ sign, thanks for all your help i really appreciate it
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
    double num1, p;
    printf("enter price:\n");
    scanf("%f", &p);
    printf("\t\t\tMonthly Payments\nAnn. Int. Rates\t15-year loan\t20-year loan\t30-
    year loan\n--------------------------------------------------------------\n");
    int k;
    double r, m1, m2, m3, tot1, tot2, tot3, n, m, o, b;
    n=180;
    m=240;
    o=360;
    for(k=.045; k<=.075; k=k+.0025)
    {
    b = ("(k) /12."),k;
    m1 = (p)*(b)*(pow(1 + b, (double)n))/(pow(1+b, (double)n)-1);                  
    m2 = (p)*(b)*(pow(1 + b, (double)m))/(pow(1+b, (double)m)-1);                  
    m3 = (p)*(b)*(pow(1 + b, (double)o))/(pow(1+b, (double)o)-1);                  
    tot1 = (n * m1);
    tot2 = (m * m2);
    tot3 = (o * m3);
    printf("\t%1.4f\n",r);
    }
    }
    and this is what it told me when i tried to run it:
    Code:
    HW3.c: In function `main':
    HW3.c:16: incompatible types in assignment
    iacs5.ucsd.edu 206: a.out
    enter price:
    45000
    Segmentation fault (core dumped)

  9. #9
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>b = ("(k) /12."),k;

    What is this for? This will not work.
    The keyboard is the standard device used to cause computer errors!

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    since p is a double, try using %lf.

    You're using k as a floating point, but you've delcared it as an int. I *think* it's truncating that loop to 0, but...

    use the %lf for your output too.
    Last edited by ronin; 04-24-2003 at 09:19 PM.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    i tried switching it to %lf and its still not going through and the whole line with b = ("(%lf) /12."), r; is because in the later equations i need the monthly interest rate which is the annual interest rate (%lf) divided by 12, if you could tell me another way to write it so that it would work thatd be great. heres what i have now and what it tells me, thanks so much for helping me out here:
    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {
    double num1, p;
    printf("enter price:\n");
    scanf("%d",&p);
    printf("\t\t\tMonthly Payments\nAnn. Int. Rates\t15-year loan\t20-year loan\t30-
    year loan\n--------------------------------------------------------------\n");
    int k;
    double r, m1, m2, m3, tot1, tot2, tot3, n, m, o, b;
    n=180;
    m=240;
    o=360;
    for(k=0; k<=12; k++)
    {
    r=(.0450+(.0025*k)), k;
    b = ("(%lf) /12."), r;
    m1 = (p)*(b)*(pow(1 + b, (double)n))/(pow(1+b, (double)n)-1);
    m2 = (p)*(b)*(pow(1 + b, (double)m))/(pow(1+b, (double)m)-1);
    m3 = (p)*(b)*(pow(1 + b, (double)o))/(pow(1+b, (double)o)-1);
    tot1 = (n * m1);
    tot2 = (m * m2);
    tot3 = (o * m3);
    printf("\t%1.4f\n",r);
    }
    }
    and what it says:
    Code:
    HW3.c: In function `main':
    HW3.c:17: incompatible types in assignment

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    im sorry also if anyone knows how to get it to stop core dumping cause ive tried adding a & and changing the first part from %f to %d neither worked so far.
    thanks

Popular pages Recent additions subscribe to a feed