Nested loop with function

This is a discussion on Nested loop with function within the C Programming forums, part of the General Programming Boards category; I have used this board once before and it was very helpful especially when I was blinded by the obvious ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Question Nested loop with function

    I have used this board once before and it was very helpful especially when I was blinded by the obvious so I thought I would ask to see if I have my loops back wards or incorrect. I am trying to build a program that displays a loan table for a 1000 loan for each interest rate depending on what you select to start with and end with along with a step size. The number of months is 30-72 incementing by 6 horizontally with the apr incrementing but the step size vertically like

    apr 30 36 42 48 54 60 66 72

    5.5 35.53 35.53 4951639 bunch a mumbo jumbo followed by 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000
    6.0 35.75 35.75 Followed by the same mumbo jumbo and so on to it reaches its apr end

    It seems to be getting the apr each time and calculating using the function but does nothing with the months and all the mumbo jumbo I have no clue. I have included the code and any guidance of where I have gone wrong would be helpful.
    Code:
    #include <math.h>
    #include <stdio.h>
    # define mbegin 30
    #define mlimit 72
    #define mstep 6
    
    /* Function protoype */
    double loan (double p, double apr, int m, double c);
    
    int main(void) {
    	double p = 1000;	
    	double aprstart = 0;
    	double aprend = 0;
    	double apr =0;
    	double step = 0;
    	int m = 0;
    	double c = 0;
    	
    	printf("\n");
    	printf("\n");
    	printf("        Big Deal Autos\n");
    	printf("        Auto Loan Program\n");
    	printf("        By James Alexander");
    	printf("\n");
    	printf("\n");
    	printf("What is the starting Apr?\n");
    	scanf("&#37;lf",&aprstart);
    	printf("What is ths ending Apr?\n");
    	scanf("%lf",&aprend);	
    	printf("What is the APR step size?\n");
    	scanf("%lf",&step);	
    	apr = aprstart;
    	
    	printf("\n");
    	printf("\n");
    	printf("APR       30       36       42       48       54       60       66       72\n");
    	printf("\n");
    	for (m = mbegin;
    		m <= mlimit;
    		m += mstep) {
    	while (apr != aprend + step) {
    	printf("%.2f    %.2f    %.2f     %.2f      %.2f      %.2f     %.2f     %.2f     %.2f",apr,		
    	loan(p, apr, m, c));
    	printf("\n");
    	apr = apr + step;
    	}
    	
    	}
    }
    
    	double
    	loan(double p, double apr, int m, double c)
    {
    	double r = (apr / 12) * 0.01;
    	double a = p * r;
    	double b= 1-(pow(1+r,-m));
    	c= a / b;
    	
    	return (c);
    
    }
    Last edited by Dave_Sinkula; 03-19-2008 at 02:18 PM. Reason: Fixed code tags.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Avoid checking for floating point equality.
    Code:
    while (apr != aprend + step)
    Use inequality instead; for example:
    Code:
    while (apr <= aprend + step)
    And feed printf enough arguments:
    Code:
    	printf("&#37;.2f %.2f %.2f     %.2f      %.2f      %.2f     %.2f     %.2f     %.2f",apr,		
    	loan(p, apr, m, c));
    [edit]Maybe something like this?
    Code:
       while ( apr <= aprend + step )
       {
          printf("%.2f    ", apr);
          for ( m = mbegin; m <= mlimit; m += mstep )
          {
             printf("%.2f    ", loan(p, apr, m, c));
          }
          printf("\n");
          apr = apr + step;
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    17

    Smile Got it!

    Got it right anyways thanks. Got my outer loop mixed with inner and got the function call correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Either my loop is bad or my function is messed up
    By crazychile in forum C Programming
    Replies: 25
    Last Post: 11-02-2008, 01:04 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. 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
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21