Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #164058 > unrolled thread

Can this program be improved?

Started byManu Raju <MR@invalid.invalid>
First post2021-12-25 01:00 +0000
Last post2021-12-29 15:21 +0000
Articles 13 on this page of 113 — 19 participants

Back to article view | Back to comp.lang.c


Contents

  Can this program be improved? Manu Raju <MR@invalid.invalid> - 2021-12-25 01:00 +0000
    Re: Can this program be improved? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-12-24 21:44 -0800
      Re: Can this program be improved? David Brown <david.brown@hesbynett.no> - 2021-12-25 12:56 +0100
      Re: Can this program be improved? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-12-25 11:13 -0800
      Re: Can this program be improved? Manfred <noname@add.invalid> - 2021-12-25 23:50 +0100
      Re: Can this program be improved? Manu Raju <MR@invalid.invalid> - 2021-12-26 18:52 +0000
        Re: Can this program be improved? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-12-26 19:22 +0000
          Re: Can this program be improved? Manu Raju <MR@invalid.invalid> - 2021-12-26 21:30 +0000
            Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-26 22:02 +0000
            Re: Can this program be improved? Richard Damon <Richard@Damon-Family.org> - 2021-12-26 18:24 -0500
            Re: Can this program be improved? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-12-27 00:47 +0000
      Re: Can this program be improved? Guillaume <message@bottle.org> - 2021-12-29 17:52 +0100
        Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-29 17:49 +0000
          Re: Can this program be improved? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-12-29 11:33 -0800
            Re: Can this program be improved? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-29 22:45 -0500
              Re: Can this program be improved? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-12-30 00:35 -0800
                Re: Can this program be improved? David Brown <david.brown@hesbynett.no> - 2021-12-30 10:19 +0100
            Re: Can this program be improved? Bart <bc@freeuk.com> - 2022-01-01 18:19 +0000
              Re: Can this program be improved? Bart <bc@freeuk.com> - 2022-01-01 19:33 +0000
          Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-29 14:37 -0800
            Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-29 23:09 +0000
              Re: Can this program be improved? scott@slp53.sl.home (Scott Lurndal) - 2021-12-29 23:54 +0000
                Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-29 16:11 -0800
                Re: Can this program be improved? Kaz Kylheku <480-992-1380@kylheku.com> - 2021-12-30 00:34 +0000
                  Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-30 12:49 +0000
                    Re: Can this program be improved? Kaz Kylheku <480-992-1380@kylheku.com> - 2021-12-30 21:04 +0000
                      Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-31 02:13 +0000
              Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-29 16:09 -0800
                Re: Can this program be improved? Kaz Kylheku <480-992-1380@kylheku.com> - 2021-12-30 00:59 +0000
                  Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-29 17:48 -0800
                    Re: Can this program be improved? Kaz Kylheku <480-992-1380@kylheku.com> - 2021-12-30 20:47 +0000
                Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-30 01:06 +0000
                  Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-30 04:48 -0800
                  Re: Can this program be improved? Manfred <noname@add.invalid> - 2021-12-30 18:16 +0100
                    Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-31 02:20 +0000
                      Re: Can this program be improved? Manfred <noname@add.invalid> - 2021-12-31 18:25 +0100
                      Re: Can this program be improved? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-31 18:13 -0500
                        Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-31 23:59 +0000
                          Re: Can this program be improved? "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-12-31 16:20 -0800
                          Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-31 16:35 -0800
                            Re: Can this program be improved? Bart <bc@freeuk.com> - 2022-01-01 00:53 +0000
                              Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-31 20:22 -0800
                                Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-31 22:04 -0800
                                  Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-01 14:59 -0800
                                    Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-02 17:22 -0800
                                      Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-02 20:49 -0800
                                        Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-03 03:09 -0800
                                          Re: Can this program be improved? scott@slp53.sl.home (Scott Lurndal) - 2022-01-03 16:22 +0000
                                          Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-03 14:42 -0800
                                            Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-03 14:52 -0800
                                              Re: Can this program be improved? gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-03 22:58 +0000
                                                Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 04:27 -0800
                                                  Re: Can this program be improved? gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-04 13:39 +0000
                                              Re: Can this program be improved? David Brown <david.brown@hesbynett.no> - 2022-01-04 00:18 +0100
                                              Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-03 15:24 -0800
                                                Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-03 15:43 -0800
                                                  Re: Can this program be improved? Öö Tiib <ootiib@hot.ee> - 2022-01-03 21:19 -0800
                                                  Re: Can this program be improved? Mateusz Viste <mateusz@xyz.invalid> - 2022-01-04 09:23 +0100
                                                    Re: Can this program be improved? Guillaume <message@bottle.org> - 2022-01-04 18:18 +0100
                                                      Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 10:35 -0800
                                                        Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-04 14:07 -0800
                                                          Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 15:11 -0800
                                                            Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-01-04 15:47 -0800
                                                              Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 18:30 -0800
                                                          Re: Can this program be improved? scott@slp53.sl.home (Scott Lurndal) - 2022-01-04 23:46 +0000
                                                      Re: Can this program be improved? Bart <bc@freeuk.com> - 2022-01-04 18:41 +0000
                                                        Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 10:52 -0800
                                                          Re: Can this program be improved? scott@slp53.sl.home (Scott Lurndal) - 2022-01-04 19:00 +0000
                                                            Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 11:15 -0800
                                                              Re: Can this program be improved? Guillaume <message@bottle.org> - 2022-01-04 21:57 +0100
                                                          Re: Can this program be improved? Bart <bc@freeuk.com> - 2022-01-04 21:02 +0000
                                                            Re: Can this program be improved? scott@slp53.sl.home (Scott Lurndal) - 2022-01-04 21:38 +0000
                                                            Re: Can this program be improved? Manfred <noname@add.invalid> - 2022-01-04 23:01 +0100
                                                  Re: Can this program be improved? David Brown <david.brown@hesbynett.no> - 2022-01-04 09:31 +0100
                                                    Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 01:30 -0800
                                                      Re: Can this program be improved? David Brown <david.brown@hesbynett.no> - 2022-01-04 14:55 +0100
                                                        Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-04 06:21 -0800
                                                          Re: Can this program be improved? David Brown <david.brown@hesbynett.no> - 2022-01-04 16:49 +0100
                                                            Re: Can this program be improved? gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-04 15:56 +0000
                                                      Re: Can this program be improved? gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-04 15:03 +0000
                                            Re: Can this program be improved? gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-03 22:54 +0000
                                      Re: Can this program be improved? Mateusz Viste <mateusz@xyz.invalid> - 2022-01-03 09:22 +0100
                                        Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-03 03:30 -0800
                                          Re: Can this program be improved? David Brown <david.brown@hesbynett.no> - 2022-01-03 17:22 +0100
                                            Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-03 08:48 -0800
                                              Re: Can this program be improved? Mateusz Viste <mateusz@xyz.invalid> - 2022-01-03 18:42 +0100
                                                Re: Can this program be improved? Bart <bc@freeuk.com> - 2022-01-03 17:59 +0000
                                                Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-03 11:09 -0800
                                                  Re: Can this program be improved? Mateusz Viste <mateusz@xyz.invalid> - 2022-01-03 20:32 +0100
                                                    Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-03 11:58 -0800
                                              Re: Can this program be improved? scott@slp53.sl.home (Scott Lurndal) - 2022-01-03 18:13 +0000
                                                Re: Can this program be improved? gazelle@shell.xmission.com (Kenny McCormack) - 2022-01-03 20:05 +0000
                                Re: Can this program be improved? Bart <bc@freeuk.com> - 2022-01-01 16:53 +0000
            Re: Can this program be improved? Kaz Kylheku <480-992-1380@kylheku.com> - 2021-12-30 00:29 +0000
    Re: Can this program be improved? Bart <bc@freeuk.com> - 2021-12-25 12:47 +0000
      Re: Can this program be improved? Manu Raju <MR@invalid.invalid> - 2021-12-26 18:56 +0000
    Re: Can this program be improved? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-12-25 17:38 +0000
      Re: Can this program be improved? Öö Tiib <ootiib@hot.ee> - 2021-12-25 10:58 -0800
        Re: Can this program be improved? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-12-25 19:16 +0000
          Re: Can this program be improved? Öö Tiib <ootiib@hot.ee> - 2021-12-25 11:25 -0800
        Re: Can this program be improved? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-12-25 15:01 -0800
          Re: Can this program be improved? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-12-25 23:09 +0000
            Re: Can this program be improved? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-12-25 20:56 -0800
      Re: Can this program be improved? scott@slp53.sl.home (Scott Lurndal) - 2021-12-25 19:30 +0000
        Re: Can this program be improved? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-25 15:50 -0800
          Re: Can this program be improved? Manu Raju <MR@invalid.invalid> - 2021-12-26 19:02 +0000
      Re: Can this program be improved? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-12-25 11:53 -0800
      Re: Can this program be improved? Manu Raju <MR@invalid.invalid> - 2021-12-26 19:00 +0000
    Re: Can this program be improved? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-12-25 11:22 -0800
      Re: Can this program be improved? Manu Raju <MR@invalid.invalid> - 2021-12-26 19:01 +0000
    Re: Can this program be improved? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-25 13:27 -0800
      Re: Can this program be improved? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-12-25 16:06 -0800
    Re: Can this program be improved? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-12-29 15:21 +0000

Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]


#164073

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2021-12-25 15:01 -0800
Message-ID<sq87sr$da9$1@dont-email.me>
In reply to#164063
On 12/25/2021 10:58 AM, Öö Tiib wrote:
> On Saturday, 25 December 2021 at 19:38:17 UTC+2, Lew Pitcher wrote:
>> interest = Opening * r / 1200; /* neither Opening nor r change within the j loop */
>> repaid = payBack; /* payBack does not change within the j loop */
> 
> And since you removed "interest" out of j loop also "Total" should go out
> and since "Total" went out also "closing" so go out and since j loop is
> now empty it should be erased. ;-)

Tell that to the bank... ;^)

[toc] | [prev] | [next] | [standalone]


#164074

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2021-12-25 23:09 +0000
Message-ID<sq88b9$tp6$3@dont-email.me>
In reply to#164073
On Sat, 25 Dec 2021 15:01:46 -0800, Chris M. Thomasson wrote:

> On 12/25/2021 10:58 AM, Öö Tiib wrote:
>> On Saturday, 25 December 2021 at 19:38:17 UTC+2, Lew Pitcher wrote:
>>> interest = Opening * r / 1200; /* neither Opening nor r change within the j loop */
>>> repaid = payBack; /* payBack does not change within the j loop */
>> 
>> And since you removed "interest" out of j loop also "Total" should go out
>> and since "Total" went out also "closing" so go out and since j loop is
>> now empty it should be erased. ;-)
> 
> Tell that to the bank... ;^)

Öö Tiib wasn't wrong. I didn't take my advice far enough :-)

As for "the bank", the OP's program produces identical results with
the removal of the j loop, so if the original program was good enough,
then the OP can take the loop-invariant version "to the bank" as well  ;-)

Keep well, my friends.
-- 
Lew Pitcher
"In Skills, We Trust"

[toc] | [prev] | [next] | [standalone]


#164081

From"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Date2021-12-25 20:56 -0800
Message-ID<sq8slv$b0r$1@dont-email.me>
In reply to#164074
On 12/25/2021 3:09 PM, Lew Pitcher wrote:
> On Sat, 25 Dec 2021 15:01:46 -0800, Chris M. Thomasson wrote:
> 
>> On 12/25/2021 10:58 AM, Öö Tiib wrote:
>>> On Saturday, 25 December 2021 at 19:38:17 UTC+2, Lew Pitcher wrote:
>>>> interest = Opening * r / 1200; /* neither Opening nor r change within the j loop */
>>>> repaid = payBack; /* payBack does not change within the j loop */
>>>
>>> And since you removed "interest" out of j loop also "Total" should go out
>>> and since "Total" went out also "closing" so go out and since j loop is
>>> now empty it should be erased. ;-)
>>
>> Tell that to the bank... ;^)
> 
> Öö Tiib wasn't wrong. I didn't take my advice far enough :-)
> 
> As for "the bank", the OP's program produces identical results with
> the removal of the j loop, so if the original program was good enough,
> then the OP can take the loop-invariant version "to the bank" as well  ;-)
> 
> Keep well, my friends.


Ahhhh! Touche. Merry Christmas!

[toc] | [prev] | [next] | [standalone]


#164069

Fromscott@slp53.sl.home (Scott Lurndal)
Date2021-12-25 19:30 +0000
Message-ID<djKxJ.156055$Wkjc.120982@fx35.iad>
In reply to#164062
ram@zedat.fu-berlin.de (Stefan Ram) writes:
>Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
>>You've received very good advice from others. I'll add my voice
>>to the chorus, now. But, for only one point ...
>
>  I have a totally different POV on this than yall!
>
>  1st  The "greater-than" signs at the start of every
>  line are a violation of Usenet customs and/or 
>  syntax errors in the C program.

Or, perhaps, they are standard Usenet quoting characters, in use
for four decades.

[toc] | [prev] | [next] | [standalone]


#164075

FromKeith Thompson <Keith.S.Thompson+u@gmail.com>
Date2021-12-25 15:50 -0800
Message-ID<871r204539.fsf@nosuchdomain.example.com>
In reply to#164069
scott@slp53.sl.home (Scott Lurndal) writes:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
>>>You've received very good advice from others. I'll add my voice
>>>to the chorus, now. But, for only one point ...
>>
>>  I have a totally different POV on this than yall!
>>
>>  1st  The "greater-than" signs at the start of every
>>  line are a violation of Usenet customs and/or 
>>  syntax errors in the C program.
>
> Or, perhaps, they are standard Usenet quoting characters, in use
> for four decades.

They are, but they're generally used to quote text from previous
articles.  The code in the question is original text.  I'm not
necessarily criticizing the original poster, but IMHO it would have been
better for the code not to be prefixed with anything, particularly so
that readers can copy-and-paste or save the article and easily extract
the code from it.

My newsreader (Gnus) showed me a lot of strange characters.  Here's what
the original article looked like to me:

> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
>
> int main()
> {
> \240 float P = 100000; // Loan Amount
> \240 float r = 7.5; // Advertised Interest Rate
> \240 int n = 12; // Loan Period
> \240 float payBack = (P * r / 1200 * pow(1 + r / 1200, n)) / (pow((1 +
> r / 1200), n) - 1);

the \240 is a NO-BREAK SPACE character.  And when I tried to save the
article, emacs gave me an error message; it was unable to encode the
\240 characters to UTF-8.  It appears that the original article was
encoded in ISO-8859-1 or something similar, but not marked as such in
the headers.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

[toc] | [prev] | [next] | [standalone]


#164086

FromManu Raju <MR@invalid.invalid>
Date2021-12-26 19:02 +0000
Message-ID<sqafa1$o55$1@dont-email.me>
In reply to#164075
On 25/12/2021 23:50, Keith Thompson wrote:
>
>
> My newsreader (Gnus) showed me a lot of strange characters.  Here's what
> the original article looked like to me:

The code was copy/pasted from VS Code so that might be the reason.


>
>
> the \240 is a NO-BREAK SPACE character.  And when I tried to save the
> article, emacs gave me an error message; it was unable to encode the
> \240 characters to UTF-8.  It appears that the original article was
> encoded in ISO-8859-1 or something similar, but not marked as such in
> the headers.

The code was copied from VS Code and pasted in Mozilla Thunderbird. Not 
sure why it changed.


>

[toc] | [prev] | [next] | [standalone]


#164070

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2021-12-25 11:53 -0800
Message-ID<sq7sr5$geg$1@dont-email.me>
In reply to#164062
On 12/25/2021 9:38 AM, Lew Pitcher wrote:
>
>      interest = Opening * r / 1200;	/* neither Opening nor r change within the j loop */
>      repaid = payBack;			/* payBack does not change within the j loop */
>

Yes, I missed this part. The whole loop

         for (int j = 0; j <= n; j++)
         {
             interest = Opening * r / 1200;
             Total = Opening + interest;
             repaid = payBack;
             closing = Total - payBack;
         }

does not have any observable behavior, its body does not depend on `j` 
and actions performed inside do not have any cumulative effect. Each 
iteration begins in the same input state.

This means that the loop is completely pointless. It is equivalent to a 
one-time execution of its body

             interest = Opening * r / 1200;
             Total = Opening + interest;
             repaid = payBack;
             closing = Total - payBack;

-- 
Best regards,
Andrey Tarasevich

[toc] | [prev] | [next] | [standalone]


#164084

FromManu Raju <MR@invalid.invalid>
Date2021-12-26 19:00 +0000
Message-ID<sqaecm$1tn2$1@gioia.aioe.org>
In reply to#164062
On 25/12/2021 17:38, Lew Pitcher wrote:
> You've received very good advice from others. I'll add my voice
> to the chorus, now. But, for only one point ...
> 
>
> 
> When writing a loop such as above, you should consider placing
> loop-invariant calculations outside of the loop. Most
> compilers would optimize the loop-invariant statements such that
> they only execute once, so this isn't really a performance issue.
> 
> However, it /is/ a readability issue. So, your given logic would
> be easier to read as...
> 
> 


Thank you.

[toc] | [prev] | [next] | [standalone]


#164067

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2021-12-25 11:22 -0800
Message-ID<864k6w1ocn.fsf@linuxsc.com>
In reply to#164058
Manu Raju <MR@invalid.invalid> writes:

[revised to repair spacing issues]

> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
>
> int main()
> {
>     float P = 100000; // Loan Amount
>     float r = 7.5; // Advertised Interest Rate
>     int n = 12; // Loan Period
>     float payBack = (P * r / 1200 * pow(1 + r / 1200, n))
>         / (pow((1 + r / 1200), n) - 1);
>     float Opening = P; // Opening Balance
>     float closing = 0; // Closing Balance
>     float interest = 0;
>     float Total = 0;
>     float repaid = 0;
>     
>     printf("\nMonthly payment is: %.2f\n", payBack);
>
>     printf("%10s %14s %13s %10s %14s %12s\n", "Month", "Beginning",
>         "Interest", "Total", "Repayment", "Balance");
>
>     for (int i = 1; i <= n; i++)
>     {
>         for (int j = 0; j <= n; j++)
>         {
>             interest = Opening * r / 1200;
>             Total = Opening + interest;
>             repaid = payBack;
>             closing = Total - payBack;
>         }
>         printf("%10d %14.0f %13.0f %10.0f %14.0f %12.0f\n", i,
>             Opening, interest, Total, repaid, closing);
>         Opening = closing;
>     }
>     return 0;
> }

I gave some other comments in another response.  Here is
a re-writing to make my suggestions more concrete.

(Note to all:  Merry Christmas!)


#include <stdio.h>
#include <math.h>

static  void    show_payment_table( double, double, int );

int
main(void){
    show_payment_table( 100000, 7.5, 12 );
    return  0;
}

void
show_payment_table( double amount, double yearly_percentage_rate, int months ){
    double const  monthly_rate  = yearly_percentage_rate / 100 / 12;
    double const  compounded    = pow( 1 + monthly_rate, months );
    double const  adjustment    = compounded / (compounded - 1);
    double const  payment       = amount * monthly_rate * adjustment;

    double balance = amount;

    printf( "\nMonthly payment is: %.2f\n", payment );

    printf( "%10s %14s %13s %10s %14s %12s\n",
        "Month", "Beginning", "Interest", "Total", "Repayment", "Balance"
    );

    for(  int i = 1;  i <= months;  i++  ){
        double const  interest  = balance * monthly_rate;
        double const  owing     = balance + interest;
        double const  remaining = owing - payment;

        printf( "%10d %14.0f %13.0f %10.0f %14.0f %12.0f\n",
            i, balance, interest, owing, payment, remaining
        );
        balance = remaining;
    }
}

[toc] | [prev] | [next] | [standalone]


#164085

FromManu Raju <MR@invalid.invalid>
Date2021-12-26 19:01 +0000
Message-ID<sqael1$k53$1@dont-email.me>
In reply to#164067
On 25/12/2021 19:22, Tim Rentsch wrote:
> Manu Raju <MR@invalid.invalid> writes:
> 
> [revised to repair spacing issues]
> 

Thank you. Very helpful and using a function was exactly what I wanted 
to do.

[toc] | [prev] | [next] | [standalone]


#164071

FromMalcolm McLean <malcolm.arthur.mclean@gmail.com>
Date2021-12-25 13:27 -0800
Message-ID<ef7819a1-7d75-4c45-a4a4-71cf1bbb69dbn@googlegroups.com>
In reply to#164058
On Saturday, 25 December 2021 at 03:38:05 UTC, Manu Raju wrote:
> > 
> > 
> > #include <stdio.h> 
> > #include <stdlib.h> 
> > #include <math.h> 
> > 
> > int main() 
> > { 
> >   float P = 100000; // Loan Amount 
> >   float r = 7.5; // Advertised Interest Rate 
> >   int n = 12; // Loan Period 
> >   float payBack = (P * r / 1200 * pow(1 + r / 1200, n)) / (pow((1 + 
> > r / 1200), n) - 1); 
> >   float Opening = P; // Opening Balance 
> >   float closing = 0;  // Closing Balance 
> >   float interest = 0; 
> >   float Total = 0; 
> >   float repaid = 0; 
> > 
> >   printf("\nMonthly payment is: %.2f\n", payBack); 
> > 
> >     printf("%10s %14s %13s %10s %14s %12s\n", "Month", "Beginning", 
> > "Interest", "Total", "Repayment", "Balance"); 
> > 
> >     for (int i = 1; i <= n; i++) 
> >     { 
> >         for (int j = 0; j <= n; j++) 
> >         { 
> >             interest = Opening * r / 1200; 
> >             Total = Opening + interest; 
> >             repaid = payBack; 
> >             closing = Total - payBack; 
> >         } 
> >         printf("%10d %14.0f %13.0f %10.0f %14.0f %12.0f\n", i, 
> > Opening, interest, Total, repaid, closing); 
> >         Opening = closing; 
> >     } 
> >     return 0; 
> > } 
> > 
> > /* 
> > * Output should be: 
> > Monthly payment is: 8675.71 
> >      Month      Beginning      Interest      Total Repayment Balance 
> >          1         100000           625     100625 8676 91949 
> >          2          91949           575      92524 8676 83848 
> >          3          83848           524      84372 8676 75697 
> >          4          75697           473      76170 8676 67494 
> >          5          67494           422      67916 8676 59240 
> >          6          59240           370      59610 8676 50935 
> >          7          50935           318      51253 8676 42577 
> >          8          42577           266      42843 8676 34168 
> >          9          34168           214      34381 8676 25706 
> >         10          25706           161      25866 8676 17190 
> >         11          17190           107      17298 8676 8622 
> >         12           8622            54       8676 8676 0 
> > 
> > */ 
> > 
> > 
> > 
> > 
> >

 for (int j = 0; j <= n; j++) 

This line looks supicious. You are applying the interest 13 times. That might be
right (13 4 week months in a year), but it needs a comment.
YOu shouldn't re-use "n", as other have suggested.

[toc] | [prev] | [next] | [standalone]


#164076

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2021-12-25 16:06 -0800
Message-ID<sq8bml$35q$1@dont-email.me>
In reply to#164071
On 12/25/2021 1:27 PM, Malcolm McLean wrote:
> 
>   for (int j = 0; j <= n; j++)
> 
> This line looks supicious. You are applying the interest 13 times. That might be
> right (13 4 week months in a year), but it needs a comment.
> YOu shouldn't re-use "n", as other have suggested.

True, but as it has already been stated above, the body of the loop does 
not have any cumulative effect. It is not really "applying" anything to 
anything.

You can run this loop 10000 times, or you can run it only once - makes 
no difference whatsoever.

-- 
Best regards,
Andrey Tarasevich

[toc] | [prev] | [next] | [standalone]


#164098

FromLew Pitcher <lew.pitcher@digitalfreehold.ca>
Date2021-12-29 15:21 +0000
Message-ID<sqhuee$mht$1@dont-email.me>
In reply to#164058
One last thought....

On Sat, 25 Dec 2021 01:00:00 +0000, Manu Raju wrote:

[snip]
>>   float payBack = (P * r / 1200 * pow(1 + r / 1200, n)) / (pow((1 +  r / 1200), n) - 1);
[snip]

You need some documentation, especially around the important but obscure
parts like the statement above.

While I've been out of the financial computing world for quite some time,
I still can recognize /parts/ of this statement as relating to compound
interest calculations, but the /exact/ calculation escapes me.

A comment within your code, describing the sort of calculation you make
here, along with a reference to it's source, would go a long way to
making the program readable.

When you come back to this code next year, or when your teacher
evaluates the code, such a comment would make the intent of this
calculation clearer (and perhaps even assist in debugging).

HTH
-- 
Lew Pitcher
"In Skills, We Trust"

[toc] | [prev] | [standalone]


Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]

Back to top | Article view | comp.lang.c


csiph-web