Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If imprinted foil seal under cap is broken or missing when purchased, do not use.


devel / comp.lang.c / Can this program be improved?

SubjectAuthor
* Can this program be improved?Manu Raju
+* Re: Can this program be improved?Andrey Tarasevich
|+- Re: Can this program be improved?David Brown
|+- Re: Can this program be improved?Tim Rentsch
|+- Re: Can this program be improved?Manfred
|+* Re: Can this program be improved?Manu Raju
||`* Re: Can this program be improved?Lew Pitcher
|| `* Re: Can this program be improved?Manu Raju
||  +- Re: Can this program be improved?Bart
||  +- Re: Can this program be improved?Richard Damon
||  `- Re: Can this program be improved?Lew Pitcher
|`* Re: Can this program be improved?Guillaume
| `* Re: Can this program be improved?Bart
|  +* Re: Can this program be improved?Andrey Tarasevich
|  |+* Re: Can this program be improved?James Kuyper
|  ||`* Re: Can this program be improved?Andrey Tarasevich
|  || `- Re: Can this program be improved?David Brown
|  |`* Re: Can this program be improved?Bart
|  | `- Re: Can this program be improved?Bart
|  `* Re: Can this program be improved?Keith Thompson
|   +* Re: Can this program be improved?Bart
|   |+* Re: Can this program be improved?Scott Lurndal
|   ||+- Re: Can this program be improved?Keith Thompson
|   ||`* Re: Can this program be improved?Kaz Kylheku
|   || `* Re: Can this program be improved?Bart
|   ||  `* Re: Can this program be improved?Kaz Kylheku
|   ||   `- Re: Can this program be improved?Bart
|   |`* Re: Can this program be improved?Keith Thompson
|   | +* Re: Can this program be improved?Kaz Kylheku
|   | |`* Re: Can this program be improved?Keith Thompson
|   | | `- Re: Can this program be improved?Kaz Kylheku
|   | `* Re: Can this program be improved?Bart
|   |  +- Re: Can this program be improved?Malcolm McLean
|   |  `* Re: Can this program be improved?Manfred
|   |   `* Re: Can this program be improved?Bart
|   |    +- Re: Can this program be improved?Manfred
|   |    `* Re: Can this program be improved?James Kuyper
|   |     `* Re: Can this program be improved?Bart
|   |      +- Re: Can this program be improved?james...@alumni.caltech.edu
|   |      `* Re: Can this program be improved?Keith Thompson
|   |       `* Re: Can this program be improved?Bart
|   |        `* Re: Can this program be improved?Keith Thompson
|   |         +* Re: Can this program be improved?Malcolm McLean
|   |         |`* Re: Can this program be improved?Keith Thompson
|   |         | `* Re: Can this program be improved?Malcolm McLean
|   |         |  +* Re: Can this program be improved?Keith Thompson
|   |         |  |`* Re: Can this program be improved?Malcolm McLean
|   |         |  | +- Re: Can this program be improved?Scott Lurndal
|   |         |  | `* Re: Can this program be improved?Keith Thompson
|   |         |  |  +* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |+* Re: Can this program be improved?Kenny McCormack
|   |         |  |  ||`* Re: Can this program be improved?Malcolm McLean
|   |         |  |  || `- Re: Can this program be improved?Kenny McCormack
|   |         |  |  |+- Re: Can this program be improved?David Brown
|   |         |  |  |`* Re: Can this program be improved?Keith Thompson
|   |         |  |  | `* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |  +- Re: Can this program be improved?Öö Tiib
|   |         |  |  |  +* Re: Can this program be improved?Mateusz Viste
|   |         |  |  |  |`* Re: Can this program be improved?Guillaume
|   |         |  |  |  | +* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |  | |`* Re: Can this program be improved?Keith Thompson
|   |         |  |  |  | | +* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |  | | |`* Re: Can this program be improved?Keith Thompson
|   |         |  |  |  | | | `- Re: Can this program be improved?Malcolm McLean
|   |         |  |  |  | | `- Re: Can this program be improved?Scott Lurndal
|   |         |  |  |  | `* Re: Can this program be improved?Bart
|   |         |  |  |  |  `* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |  |   +* Re: Can this program be improved?Scott Lurndal
|   |         |  |  |  |   |`* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |  |   | `- Re: Can this program be improved?Guillaume
|   |         |  |  |  |   `* Re: Can this program be improved?Bart
|   |         |  |  |  |    +- Re: Can this program be improved?Scott Lurndal
|   |         |  |  |  |    `- Re: Can this program be improved?Manfred
|   |         |  |  |  `* Re: Can this program be improved?David Brown
|   |         |  |  |   `* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |    +* Re: Can this program be improved?David Brown
|   |         |  |  |    |`* Re: Can this program be improved?Malcolm McLean
|   |         |  |  |    | `* Re: Can this program be improved?David Brown
|   |         |  |  |    |  `- Re: Can this program be improved?Kenny McCormack
|   |         |  |  |    `- Re: Can this program be improved?Kenny McCormack
|   |         |  |  `- Re: Can this program be improved?Kenny McCormack
|   |         |  `* Re: Can this program be improved?Mateusz Viste
|   |         |   `* Re: Can this program be improved?Malcolm McLean
|   |         |    `* Re: Can this program be improved?David Brown
|   |         |     `* Re: Can this program be improved?Malcolm McLean
|   |         |      +* Re: Can this program be improved?Mateusz Viste
|   |         |      |+- Re: Can this program be improved?Bart
|   |         |      |`* Re: Can this program be improved?Malcolm McLean
|   |         |      | `* Re: Can this program be improved?Mateusz Viste
|   |         |      |  `- Re: Can this program be improved?Malcolm McLean
|   |         |      `* Re: Can this program be improved?Scott Lurndal
|   |         |       `- Re: Can this program be improved?Kenny McCormack
|   |         `- Re: Can this program be improved?Bart
|   `- Re: Can this program be improved?Kaz Kylheku
+* Re: Can this program be improved?Bart
|`- Re: Can this program be improved?Manu Raju
+* Re: Can this program be improved?Lew Pitcher
|+* Re: Can this program be improved?Öö Tiib
||+* Re: Can this program be improved?Lew Pitcher
|||`- Re: Can this program be improved?Öö Tiib
||`* Re: Can this program be improved?Chris M. Thomasson
|+* Re: Can this program be improved?Stefan Ram
|+- Re: Can this program be improved?Andrey Tarasevich
|`- Re: Can this program be improved?Manu Raju
+* Re: Can this program be improved?Tim Rentsch
+* Re: Can this program be improved?Malcolm McLean
`- Re: Can this program be improved?Lew Pitcher

Pages:12345
Can this program be improved?

<sq63mi$gom$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19625&group=comp.lang.c#19625

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: MR...@invalid.invalid (Manu Raju)
Newsgroups: comp.lang.c
Subject: Can this program be improved?
Date: Sat, 25 Dec 2021 01:00:00 +0000
Organization: Aioe.org NNTP Server
Lines: 65
Message-ID: <sq63mi$gom$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 03:37:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="35959a1c8236bbd966fadd4e4f0f88de";
logging-data="17174"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zJiLtxm5hQDhG6Bwk19ZxH25WQTxDln0="
Cancel-Lock: sha1:/C2XG0gVTaDdQQldMed75Gu9UzI=
Content-Language: en-US
 by: Manu Raju - Sat, 25 Dec 2021 01:00 UTC

>
>
> #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
>
> */
>
>
>
>
>

Re: Can this program be improved?

<sq6b3t$ce5$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19626&group=comp.lang.c#19626

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Fri, 24 Dec 2021 21:44:27 -0800
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <sq6b3t$ce5$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 05:44:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="27f668ab4f0b3c526bbb2068dcc3f23d";
logging-data="12741"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qOUMP6tzofvyexuIiVIdM"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
Cancel-Lock: sha1:5TwKclFI15fYnJh8Z06XLKTyy/w=
In-Reply-To: <sq63mi$gom$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Sat, 25 Dec 2021 05:44 UTC

On 12/24/2021 5:00 PM, 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;
>> }

Certainly.

1. int main(void)

2. Stop using `float` for local variables. "Default" floating-point type
in C is `double`. Everything else is used only if you have a good reason
to do that. In this case you don't.

3. Stop using "regular" floating point types for representing monetary
quantities. They are not good for that purpose.

4. It appears that you are actually trying to use `float` to represent
_integer_ quantities (judging by your `printf`). Why?

5. Stop using `pow` for calculating integer powers. You don't really
need `<math.h>` here.

6. Stop piling up all variable declarations at the beginning of the
function. Declare variables as locally as possible.

7. Stop using dummy initializers for your variables. It is better to
leave them uninitialized than initialize them to dummy zeros. This point
is actually connected to the previous one: once you start declaring
variables as locally as possible, you'll normally have a meaningful
initializers for them at the point of declaration.

8. What's going on with capitalization in your variables names?
`Opening`, `closing`, `interest`, `Total`? Is this a convention of some
sort?

9. "Magical constants"? What on Earth is `1200`?

10. Repetitive subexpressions, like `1 + r / 1200` (some explicit, some
slightly obfuscated) are probably a matter of style.... But anyway: DRY
- do not repeat yourself.

--
Best regards,
Andrey Tarasevich

Re: Can this program be improved?

<sq70ss$hlh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19627&group=comp.lang.c#19627

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 12:56:11 +0100
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <sq70ss$hlh$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq6b3t$ce5$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 25 Dec 2021 11:56:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="35eb2b9474509422a5e5edb06047be4f";
logging-data="18097"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+EZJXRi6kanV2MS6rWcy8dgTSacbLMqiQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:0D1lwtsPRCaCpTDmIE4NNQ5Udtc=
In-Reply-To: <sq6b3t$ce5$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sat, 25 Dec 2021 11:56 UTC

On 25/12/2021 06:44, Andrey Tarasevich wrote:

>
> Certainly.
>
> 1. int main(void)
>
> 2. Stop using `float` for local variables. "Default" floating-point type
> in C is `double`. Everything else is used only if you have a good reason
> to do that. In this case you don't.
>
> 3. Stop using "regular" floating point types for representing monetary
> quantities. They are not good for that purpose.
>
> 4. It appears that you are actually trying to use `float` to represent
> _integer_ quantities (judging by your `printf`). Why?
>
> 5. Stop using `pow` for calculating integer powers. You don't really
> need `<math.h>` here.
>
> 6. Stop piling up all variable declarations at the beginning of the
> function. Declare variables as locally as possible.
>
> 7. Stop using dummy initializers for your variables. It is better to
> leave them uninitialized than initialize them to dummy zeros. This point
> is actually connected to the previous one: once you start declaring
> variables as locally as possible, you'll normally have a meaningful
> initializers for them at the point of declaration.
>
> 8. What's going on with capitalization in your variables names?
> `Opening`, `closing`, `interest`, `Total`? Is this a convention of some
> sort?
>
> 9. "Magical constants"? What on Earth is `1200`?
>
> 10. Repetitive subexpressions, like `1 + r / 1200` (some explicit, some
> slightly obfuscated) are probably a matter of style.... But anyway: DRY
> - do not repeat yourself.
>

That's a good list. Let me add:

11. Be very suspicious of short variable names with a comment attached.
It /might/ be the best choice, because of the way the variable is used
or how it matches some external information (like a mathematical
formula). But often it is better to give the variable a more complete
name. (As a rule of thumb, the smaller the scope, the shorter the
variable name you can get away with.)

12. Be clear in your units - perhaps also including them in names or
even type information (though that is not common in C). Make it clear
which variables are money, percentages, monthly rates, annual rates, etc.

13. Any variables that are fixed at initialisation and not changed
should be declared with "const". It makes code clearer and lets the
compiler catch some kinds of mistakes.

14. It looks like you are mixing up 12 years loan period and 12 months
per year, using the same rather anonymous variable "n" for the purpose.

(Don't take this long list as a sign that your code is terrible. It's
not bad, and I'm sure we've all seen far worse. But these are all tips
for ways to improve it and get in good coding habits early.)

Re: Can this program be improved?

<sq73tp$21e$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19628&group=comp.lang.c#19628

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 12:47:54 +0000
Organization: A noiseless patient Spider
Lines: 110
Message-ID: <sq73tp$21e$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 12:47:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="db6fc0d9a2c67874c6cb273ef8a158ec";
logging-data="2094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BQT91SDvjfUQdRhRVCxFxwsCxmkwLCGE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.0
Cancel-Lock: sha1:APDwPLqY4sivr/7fklO//hBP7z4=
In-Reply-To: <sq63mi$gom$1@dont-email.me>
 by: Bart - Sat, 25 Dec 2021 12:47 UTC

On 25/12/2021 01:00, 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;
>> }

Unlike the other replies, I think this program is largely OK. However
I'd make the following tweaks, for these reasons (I've assumed US$ amounts):

* Use double rather than float; this is more precise. With float, the
final balance will be out by some cents (hidden if shown to nearest
dollar, but sometimes you will see "-0" because of negative cents)

* I've made capitalisation more consistent

* I've lined up the comments, and added extra notes

* 'r' has been changed to 0.075 from 7.5%, to avoid that mysterious 1200
in the code. Now with '12' is easier to guess it is 12 months and you
are calculating a monthly rate.

* The display layout has been adjusted (see below), but that was just my
own preference.

On the last point, in your post, the last two columns were not properly
aligned, but when I tried, they were properly right-justified.

(Interestingly, the complicated expression initialising 'payback'
revealed a bug in one compiler, making it generate an erroneous value.
But that is not your problem. Just be aware if using Tiny C.)

Example:
------------------------------------------------------------

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

int main(void)
{ double P = 100000; // Loan Amount (all amounts dollars(etc))
double r = 7.5/100; // Advertised Interest Rate (percent/100,
annual)
int n = 12; // Loan Period (months)
double payback = // Monthly repayment
(P * r/12 * pow(1+r/12, n)) / (pow((1+r/12),n) - 1);
double opening = P; // Opening Balance
double closing = 0; // Closing Balance
double interest = 0;
double total = 0;
double repaid = 0;

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

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

for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= n; j++)
{
interest = opening * r / 12;
total = opening + interest;
repaid = payback;
closing = total - payback;
}
printf("%10d %14.0f %10.0f %10.0f %10.0f %12.0f\n", i,
opening, interest, total, repaid, closing);
opening = closing;
}
}

Re: Can this program be improved?

<sq7ktu$tp6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19629&group=comp.lang.c#19629

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 17:38:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <sq7ktu$tp6$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 17:38:06 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="33c735418ef9507286bed66627f2ad4c";
logging-data="30502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZoxxhdEdzDeccvz7Gi12Jq0daZHRNyf4="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:eye+Z1016+LaKdhdci1JvtZxMbI=
 by: Lew Pitcher - Sat, 25 Dec 2021 17:38 UTC

You've received very good advice from others. I'll add my voice
to the chorus, now. But, for only one point ...

On Sat, 25 Dec 2021 01:00:00 +0000, 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;
>>         }

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...

for (int i = 1; i <= n; i++)
  {
   interest = Opening * r / 1200; /* neither Opening nor r change within the j loop */
    repaid = payBack; /* payBack does not change within the j loop */
for (int j = 0; j <= n; j++)
    {
     Total = Opening + interest;
      closing = Total - payBack;
    }
printf("%10d %14.0f %13.0f %10.0f %14.0f %12.0f\n",
i, Opening, interest, Total, repaid, closing);
Opening = closing;
}

>>         printf("%10d %14.0f %13.0f %10.0f %14.0f %12.0f\n", i,
>> Opening, interest, Total, repaid, closing);
>>         Opening = closing;
>>     }
>>     return 0;
>> }

--
Lew Pitcher
"In Skills, We Trust"

Re: Can this program be improved?

<8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19630&group=comp.lang.c#19630

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ac8:5f47:: with SMTP id y7mr9573354qta.342.1640458701039;
Sat, 25 Dec 2021 10:58:21 -0800 (PST)
X-Received: by 2002:ad4:5ba3:: with SMTP id 3mr8987502qvq.59.1640458700941;
Sat, 25 Dec 2021 10:58:20 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 25 Dec 2021 10:58:20 -0800 (PST)
In-Reply-To: <sq7ktu$tp6$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.164; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.164
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com>
Subject: Re: Can this program be improved?
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Sat, 25 Dec 2021 18:58:21 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 6
 by: Öö Tiib - Sat, 25 Dec 2021 18:58 UTC

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. ;-)

Re: Can this program be improved?

<comment-20211225200407@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19631&group=comp.lang.c#19631

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: 25 Dec 2021 19:09:55 GMT
Organization: Stefan Ram
Lines: 26
Expires: 1 Mar 2022 11:59:58 GMT
Message-ID: <comment-20211225200407@ram.dialup.fu-berlin.de>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de lQ1XHULZ7n+UTOaYIx2LbgDqI8Izq/+vsT3nowfWY2uyIF
X-Copyright: (C) Copyright 2021 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Sat, 25 Dec 2021 19:09 UTC

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.

2nd Questions raised in the Subject header field
should be repeated in the body of the post.

3rd I also was not able to decode some characters.
Maybe the OP misses the declaration of the correct
encoding? (Could also be a problem with my newsreader.
I'm not sure here.)

4th When the required output is given as
a certain text and this is the whole specification,
the most robust way to implement this is to write
a single puts or fputs call with this string given
as a string literal or a sequence of string literals,
or maybe a sequence of puts or fputs calls.

Re: Can this program be improved?

<868rw81or6.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19632&group=comp.lang.c#19632

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 11:13:49 -0800
Organization: A noiseless patient Spider
Lines: 122
Message-ID: <868rw81or6.fsf@linuxsc.com>
References: <sq63mi$gom$1@dont-email.me> <sq6b3t$ce5$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="1fe31837ad76f46de1d32de2fe408cda";
logging-data="3928"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/g8c8ugGmsnidodNUTVugN9ISRSxgJNwM="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:hyKhrfg25vmGT1zBTFXtO5zoDr8=
sha1:HTemm04td317th7e+61s1z8wI5g=
 by: Tim Rentsch - Sat, 25 Dec 2021 19:13 UTC

Andrey Tarasevich <andreytarasevich@hotmail.com> writes:

> On 12/24/2021 5:00 PM, Manu Raju wrote:
>
>> [revised to repair white space]
>>
>> #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;
>> }
>
> Certainly.
>
> 1. int main(void)

Check.

> 2. Stop using `float` for local variables. "Default" floating-point
> type in C is `double`. Everything else is used only if you have a
> good reason to do that. In this case you don't.

Check.

> 3. Stop using "regular" floating point types for representing
> monetary quantities. They are not good for that purpose.

For this level of programming I think floating point is okay. A
problem that is important is the program makes no effort to round
(to the nearest cent, for example), so the calculation gives
funny results typical of calculations done with floating point.
But that problem can be addressed without throwing out floating
point altogether.

> 4. It appears that you are actually trying to use `float` to
> represent _integer_ quantities (judging by your `printf`). Why?

To me that just looks like what the print formats are, not what
the kinds of quantities are.

> 5. Stop using `pow` for calculating integer powers. You don't
> really need `<math.h>` here.

I have to vote against this comment. Using pow() makes it
obvious what is being done, the exponent being integral
notwithstanding.

> 6. Stop piling up all variable declarations at the beginning of
> the function. Declare variables as locally as possible.

Good advice here I would say, but this rule is a point of style
with differing viewpoints.

> 7. Stop using dummy initializers for your variables. It is better
> to leave them uninitialized than initialize them to dummy zeros.
> This point is actually connected to the previous one: once you
> start declaring variables as locally as possible, you'll normally
> have a meaningful initializers for them at the point of declaration.

Check.

> 8. What's going on with capitalization in your variables names?
> Opening`, `closing`, `interest`, `Total`? Is this a convention of
> some sort?

Check.

> 9. "Magical constants"? What on Earth is `1200`?

Check. However I think it should be added that the "magicness"
of the constant(s) can be addressed by rephrasing the expressions
where they are used, without necessarily taking the constants
out of those expressions.

> 10. Repetitive subexpressions, like `1 + r / 1200` (some explicit,
> some slightly obfuscated) are probably a matter of style.... But
> anyway: DRY - do not repeat yourself.

More significant here, I think, is that defining a variable with
the value of this expression offers an opportunity to give some
sort of descriptive name to the quantity.

Two additional points:

The #include <stdlib.h> can be removed.

The inner loop (with 'j' as the index variable) does the same
calculation over and over again. There is no reason to make
that a loop - just write the body.

Re: Can this program be improved?

<sq7qmv$tp6$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19633&group=comp.lang.c#19633

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 19:16:47 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <sq7qmv$tp6$2@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
<8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 19:16:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="33c735418ef9507286bed66627f2ad4c";
logging-data="30502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ztWX7A96WntEiwfOvOJn/72if0jAggoE="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:nH2UOx8sYGWxUzdrQlI8sXqHTtM=
 by: Lew Pitcher - Sat, 25 Dec 2021 19:16 UTC

On Sat, 25 Dec 2021 10:58:20 -0800, Öö 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. ;-)

Why, yes. Now that you mention it.

But we must leave /some/ details as exercises for the student, agreed?

--
Lew Pitcher
"In Skills, We Trust"

Re: Can this program be improved?

<864k6w1ocn.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19634&group=comp.lang.c#19634

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 11:22:32 -0800
Organization: A noiseless patient Spider
Lines: 85
Message-ID: <864k6w1ocn.fsf@linuxsc.com>
References: <sq63mi$gom$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="1fe31837ad76f46de1d32de2fe408cda";
logging-data="3928"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gtAQLMe5+7HRILDFv43hh7W3qmh+N/BA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:Y71lKbhb6fjzgtSvWwzDoy26G9E=
sha1:UGwPeG/hMLTGDefaUwJjwKRFfN0=
 by: Tim Rentsch - Sat, 25 Dec 2021 19:22 UTC

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;
}
}

Re: Can this program be improved?

<c4ca9a57-902a-485b-a458-69826f744270n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19635&group=comp.lang.c#19635

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:29c8:: with SMTP id gh8mr10025477qvb.78.1640460340806;
Sat, 25 Dec 2021 11:25:40 -0800 (PST)
X-Received: by 2002:ad4:5c45:: with SMTP id a5mr10027980qva.7.1640460340706;
Sat, 25 Dec 2021 11:25:40 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 25 Dec 2021 11:25:40 -0800 (PST)
In-Reply-To: <sq7qmv$tp6$2@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=94.246.251.164; posting-account=pysjKgkAAACLegAdYDFznkqjgx_7vlUK
NNTP-Posting-Host: 94.246.251.164
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
<8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com> <sq7qmv$tp6$2@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c4ca9a57-902a-485b-a458-69826f744270n@googlegroups.com>
Subject: Re: Can this program be improved?
From: oot...@hot.ee (Öö Tiib)
Injection-Date: Sat, 25 Dec 2021 19:25:40 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 21
 by: Öö Tiib - Sat, 25 Dec 2021 19:25 UTC

On Saturday, 25 December 2021 at 21:16:59 UTC+2, Lew Pitcher wrote:
> On Sat, 25 Dec 2021 10:58:20 -0800, Öö 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. ;-)
>
> Why, yes. Now that you mention it.
>
> But we must leave /some/ details as exercises for the student, agreed?

Oops. I added too much spoilers?
My bad. But usually at that point the teacher hints that
finding the rest of cases is exercise.

Re: Can this program be improved?

<djKxJ.156055$Wkjc.120982@fx35.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19636&group=comp.lang.c#19636

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!paganini.bofh.team!news.dns-netz.com!news.freedyn.de!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!feeder1.feed.usenet.farm!feed.usenet.farm!peer01.ams4!peer.am4.highwinds-media.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx35.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Can this program be improved?
Newsgroups: comp.lang.c
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me> <comment-20211225200407@ram.dialup.fu-berlin.de>
Lines: 13
Message-ID: <djKxJ.156055$Wkjc.120982@fx35.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 25 Dec 2021 19:30:17 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 25 Dec 2021 19:30:17 GMT
X-Received-Bytes: 1234
 by: Scott Lurndal - Sat, 25 Dec 2021 19:30 UTC

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.

Re: Can this program be improved?

<sq7sr5$geg$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19637&group=comp.lang.c#19637

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 11:53:07 -0800
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <sq7sr5$geg$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 19:53:09 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="27f668ab4f0b3c526bbb2068dcc3f23d";
logging-data="16848"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fARSfrmH32+VvdQ7b4PgZ"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
Cancel-Lock: sha1:2lZGz5IHLyzw0sWAv1rm1ZmDiYQ=
In-Reply-To: <sq7ktu$tp6$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Sat, 25 Dec 2021 19:53 UTC

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

Re: Can this program be improved?

<ef7819a1-7d75-4c45-a4a4-71cf1bbb69dbn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19638&group=comp.lang.c#19638

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:120b:: with SMTP id y11mr9932434qtx.544.1640467628929;
Sat, 25 Dec 2021 13:27:08 -0800 (PST)
X-Received: by 2002:a05:6214:21ec:: with SMTP id p12mr9959347qvj.82.1640467628777;
Sat, 25 Dec 2021 13:27:08 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 25 Dec 2021 13:27:08 -0800 (PST)
In-Reply-To: <sq63mi$gom$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:fcec:8bae:f87d:8886;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:fcec:8bae:f87d:8886
References: <sq63mi$gom$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ef7819a1-7d75-4c45-a4a4-71cf1bbb69dbn@googlegroups.com>
Subject: Re: Can this program be improved?
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Sat, 25 Dec 2021 21:27:08 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 69
 by: Malcolm McLean - Sat, 25 Dec 2021 21:27 UTC

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.

Re: Can this program be improved?

<sq877f$dof$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19639&group=comp.lang.c#19639

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 23:50:23 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sq877f$dof$1@gioia.aioe.org>
References: <sq63mi$gom$1@dont-email.me> <sq6b3t$ce5$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="14095"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Sat, 25 Dec 2021 22:50 UTC

On 12/25/2021 6:44 AM, Andrey Tarasevich wrote:
> 6. Stop piling up all variable declarations at the beginning of the
> function. Declare variables as locally as possible.

Put this way, this is questionable advice, IMO.
Experienced programmers may have your same opinion, but some others,
equally experienced, may label that same programming style as sloppy -
I've seen some myself.

In order to turn this opinion into good advice, I think there is more to
add to it:

1) Functions should perform simple, well defined tasks, which implies
that complex tasks should be split into multiple functions. After you do
this, then it becomes natural that you don't have "piles" of local
variables, and you may well have the variables you need at the beginning
of the function and still be "local".

2) Make good use of language constructs to keep counters, indexes etc.
local: for example, instead of declaring a generic counter "n" at the
beginning of the function and reuse it multiple times, prefer the following:

for (int n = 0; n < COUNT; ++n)
{ /* ... */
}

and thus have multiple "for" (and friends) constructs each with its own
counter.

3) If in the function body you have a code section that performs some
specific operation that needs its local variables, you may use a local
scope for it, and declare its variables inside it (check point 1 above
as well):

int myfunction(void)
{ /* ... */

{
int myvar = 42;
/* ... */
}

/* ... */
}

Obviously, the body of language constructs like "if", "for", "while",
"do" make natural local scopes that are good candidates for this purpose.

4) For algorithmic parameters that may affect the entire task, (e.g.
number of months in a year), you may well have a "const" variable at the
beginning of the function. "static const" at the beginning of the source
file is also an option, as well as many others.

>
> 7. Stop using dummy initializers for your variables. It is better to
> leave them uninitialized than initialize them to dummy zeros. This point
> is actually connected to the previous one: once you start declaring
> variables as locally as possible, you'll normally have a meaningful
> initializers for them at the point of declaration.

This is tricky. One of the worst bugs I have ever encountered was
"strlen" being called on an uninitialized string buffer.

I believe the best option is to initialize variables with meaningful
values, which leads to locality, as you say, but with my notes above.
However, as a last resort, it is probably better to leave them
uninitialized, rather than dummy zeros, *if* you have a decent compiler
that will spot its use before assignment.

Re: Can this program be improved?

<sq87sr$da9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19640&group=comp.lang.c#19640

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 15:01:46 -0800
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <sq87sr$da9$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
<8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 23:01:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b7658d7ee1383eeef7e94980a038b721";
logging-data="13641"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18eC7DB/bldzC9slFNWpcxLfEs++dxtjME="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
Cancel-Lock: sha1:qxxakjlpgsVDPGhOWrM6rUYDDaQ=
In-Reply-To: <8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 25 Dec 2021 23:01 UTC

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... ;^)

Re: Can this program be improved?

<sq88b9$tp6$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19641&group=comp.lang.c#19641

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 23:09:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <sq88b9$tp6$3@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
<8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com>
<sq87sr$da9$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 25 Dec 2021 23:09:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="92ddfe9a5f125766f689ba1cafe032db";
logging-data="30502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18YdFwEDfYv5BaNtvOm0gacQn7vpy4Zzcc="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:04K070b7F1HiBNDXZXpVVV69ZBY=
 by: Lew Pitcher - Sat, 25 Dec 2021 23:09 UTC

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"

Re: Can this program be improved?

<871r204539.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19642&group=comp.lang.c#19642

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 15:50:18 -0800
Organization: None to speak of
Lines: 47
Message-ID: <871r204539.fsf@nosuchdomain.example.com>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
<comment-20211225200407@ram.dialup.fu-berlin.de>
<djKxJ.156055$Wkjc.120982@fx35.iad>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="9799c5b66c60baf3193d79f5b19f947d";
logging-data="17367"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vN8zquj/Ufm65wkrxu0PY"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:YxPvCEt7ab3bmY18O/NaZFwXwW4=
sha1:bst+Vi3gIWokghfbDAoIK6KV5Ks=
 by: Keith Thompson - Sat, 25 Dec 2021 23:50 UTC

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 */

Re: Can this program be improved?

<sq8bml$35q$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19643&group=comp.lang.c#19643

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 16:06:44 -0800
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <sq8bml$35q$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me>
<ef7819a1-7d75-4c45-a4a4-71cf1bbb69dbn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 26 Dec 2021 00:06:45 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e4d30634e431ae460ab7eff862e16eca";
logging-data="3258"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Yx7+M2pDC/9zhyUKSM0P0"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
Cancel-Lock: sha1:hvG3PD2QX6Oj/1OAEoCmldi7f5E=
In-Reply-To: <ef7819a1-7d75-4c45-a4a4-71cf1bbb69dbn@googlegroups.com>
Content-Language: en-US
 by: Andrey Tarasevich - Sun, 26 Dec 2021 00:06 UTC

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

Re: Can this program be improved?

<sq8slv$b0r$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19648&group=comp.lang.c#19648

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sat, 25 Dec 2021 20:56:31 -0800
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <sq8slv$b0r$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
<8519787f-a7e7-4a09-8c44-507413b217e5n@googlegroups.com>
<sq87sr$da9$1@dont-email.me> <sq88b9$tp6$3@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 26 Dec 2021 04:56:32 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b7658d7ee1383eeef7e94980a038b721";
logging-data="11291"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DUyt7EmhY4pP4W1CRYePAvCQfedlzo9Q="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.4.1
Cancel-Lock: sha1:QDNdQHimskUemhz4FT8z1aOfdK4=
In-Reply-To: <sq88b9$tp6$3@dont-email.me>
Content-Language: en-US
 by: Chris M. Thomasson - Sun, 26 Dec 2021 04:56 UTC

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!

Re: Can this program be improved?

<sqae28$g83$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19649&group=comp.lang.c#19649

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: MR...@invalid.invalid (Manu Raju)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sun, 26 Dec 2021 18:52:14 +0000
Organization: Aioe.org NNTP Server
Lines: 57
Message-ID: <sqae28$g83$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq6b3t$ce5$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 26 Dec 2021 18:59:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ad769829b7da17e43deaa7750baa4522";
logging-data="16643"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kZ6w6YG4ocQjs548lC/3EvefG2ajkJD0="
Cancel-Lock: sha1:RIen4Katb5UfOF9JJRvFYioZ0ow=
In-Reply-To: <sq6b3t$ce5$1@dont-email.me>
Content-Language: en-US
 by: Manu Raju - Sun, 26 Dec 2021 18:52 UTC

On 25/12/2021 05:44, Andrey Tarasevich wrote:
> On 12/24/2021 5:00 PM, 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;
>>> }
>
>
> 9. "Magical constants"? What on Earth is `1200`?

It is just a figure 12 * 100. You first need to convert percentage into
decimals by dividing by 100; Then you need to divide the number again by
12 to give monthly rate. Interest rate is normally annual rate. Hope
this explains the magic number.

X-Mozilla-Status: 0811
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:

Re: Can this program be improved?

<sqae29$g88$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19650&group=comp.lang.c#19650

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: MR...@invalid.invalid (Manu Raju)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sun, 26 Dec 2021 18:56:22 +0000
Organization: Aioe.org NNTP Server
Lines: 80
Message-ID: <sqae29$g88$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq73tp$21e$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 26 Dec 2021 18:59:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ad769829b7da17e43deaa7750baa4522";
logging-data="16648"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/B2Il6nThgkr/5SWmvFICqImkS5WOJf2w="
Cancel-Lock: sha1:0pJkBRP23zF8lxYqGdoWGfg7Qhc=
In-Reply-To: <sq73tp$21e$1@dont-email.me>
Content-Language: en-US
 by: Manu Raju - Sun, 26 Dec 2021 18:56 UTC

On 25/12/2021 12:47, Bart wrote:
> On 25/12/2021 01:00, Manu Raju wrote:
>>>
>>>
>
> Unlike the other replies, I think this program is largely OK. However
> I'd make the following tweaks, for these reasons (I've assumed US$
> amounts):
>
> * Use double rather than float; this is more precise. With float, the
> final balance will be out by some cents (hidden if shown to nearest
> dollar, but sometimes you will see "-0" because of negative cents)
>
> * I've made capitalisation more consistent
>
> * I've lined up the comments, and added extra notes
>
> * 'r' has been changed to 0.075 from 7.5%, to avoid that mysterious 1200
>   in the code. Now with '12' is easier to guess it is 12 months and you
>   are calculating a monthly rate.
>
> * The display layout has been adjusted (see below), but that was just my
>   own preference.
>
> On the last point, in your post, the last two columns were not properly
> aligned, but when I tried, they were properly right-justified.
>
> (Interestingly, the complicated expression initialising 'payback'
> revealed a bug in one compiler, making it generate an erroneous value.
> But that is not your problem. Just be aware if using Tiny C.)
>
>
> Example:
> ------------------------------------------------------------
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>
>
> int main(void)
> {
>   double P = 100000;        // Loan Amount (all amounts dollars(etc))
>   double r = 7.5/100;       // Advertised Interest Rate (percent/100,
> annual)
>   int n = 12;               // Loan Period (months)
>   double payback  =         // Monthly repayment
>          (P * r/12 * pow(1+r/12, n)) / (pow((1+r/12),n) - 1);
>   double opening  = P;      // Opening Balance
>   double closing  = 0;      // Closing Balance
>   double interest = 0;
>   double total    = 0;
>   double repaid   = 0;
>
>   printf("\nMonthly payment is: %.2f\n", payback);
>
>     printf("%10s %14s %10s %10s %10s %12s\n", "Month", "Beginning",
> "Interest", "Total", "Repayment", "Balance");
>     puts("
> ------------------------------------------------------------------");
>
>     for (int i = 1; i <= n; i++)
>     {
>         for (int j = 0; j <= n; j++)
>         {
>             interest = opening * r / 12;
>             total = opening + interest;
>             repaid = payback;
>             closing = total - payback;
>         }
>         printf("%10d %14.0f %10.0f %10.0f %10.0f %12.0f\n", i,
>             opening, interest, total, repaid, closing);
>         opening = closing;
>     }
> }
>

Thank you for your feedback.

Kind regards Merry Christmas.

Re: Can this program be improved?

<sqaecm$1tn2$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19651&group=comp.lang.c#19651

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!xU/gTEbcPYfFw56bW7SDhw.user.46.165.242.91.POSTED!not-for-mail
From: MR...@invalid.invalid (Manu Raju)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sun, 26 Dec 2021 19:00:00 +0000
Organization: Aioe.org NNTP Server
Message-ID: <sqaecm$1tn2$1@gioia.aioe.org>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="63202"; posting-host="xU/gTEbcPYfFw56bW7SDhw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manu Raju - Sun, 26 Dec 2021 19:00 UTC

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.

Re: Can this program be improved?

<sqael1$k53$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19652&group=comp.lang.c#19652

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: MR...@invalid.invalid (Manu Raju)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sun, 26 Dec 2021 19:01:00 +0000
Organization: Aioe.org NNTP Server
Lines: 9
Message-ID: <sqael1$k53$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <864k6w1ocn.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 26 Dec 2021 19:09:21 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ad769829b7da17e43deaa7750baa4522";
logging-data="20643"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2lLeGWOukeyOhZ7MmCR2QiuF4wuDmURc="
Cancel-Lock: sha1:FXwlI9SHVKeUoBxLe+rwvAEYq3Y=
In-Reply-To: <864k6w1ocn.fsf@linuxsc.com>
Content-Language: en-US
 by: Manu Raju - Sun, 26 Dec 2021 19:01 UTC

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.

Re: Can this program be improved?

<sqafa1$o55$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19653&group=comp.lang.c#19653

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: MR...@invalid.invalid (Manu Raju)
Newsgroups: comp.lang.c
Subject: Re: Can this program be improved?
Date: Sun, 26 Dec 2021 19:02:00 +0000
Organization: Aioe.org NNTP Server
Lines: 24
Message-ID: <sqafa1$o55$1@dont-email.me>
References: <sq63mi$gom$1@dont-email.me> <sq7ktu$tp6$1@dont-email.me>
<comment-20211225200407@ram.dialup.fu-berlin.de>
<djKxJ.156055$Wkjc.120982@fx35.iad> <871r204539.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 26 Dec 2021 19:20:33 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ad769829b7da17e43deaa7750baa4522";
logging-data="24741"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/S/6apWawotAPY6BkS5XDc5y/JJ9ZNvs4="
Cancel-Lock: sha1:p3YDQ6orsccGqK3p0gkqUpRZMDQ=
In-Reply-To: <871r204539.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: Manu Raju - Sun, 26 Dec 2021 19:02 UTC

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.

>

Pages:12345
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor