Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #69147 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-09-03 08:54 -0700 |
| Last post | 2015-09-04 14:23 +0100 |
| Articles | 15 on this page of 55 — 11 participants |
Back to article view | Back to comp.lang.c
Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 08:54 -0700
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 17:07 +0100
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:02 -0700
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:12 -0700
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:18 -0700
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 18:30 +0100
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 19:53 +0200
Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-03 21:03 +0100
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 22:29 +0200
Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-03 21:51 +0100
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 23:37 +0200
Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-03 23:07 +0100
Re: Program to compute amount of coins James Kuyper <jameskuyper@verizon.net> - 2015-09-03 18:24 -0400
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-04 00:30 +0200
Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-03 16:36 -0700
Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 14:00 -0700
Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 13:58 -0700
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-07 05:07 +0200
Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-07 13:14 +0100
Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-08 02:39 -0700
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-08 18:29 +0200
Re: Program to compute amount of coins James Kuyper <jameskuyper@verizon.net> - 2015-09-08 13:15 -0400
Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-08 10:16 -0700
Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-09 09:37 -0700
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-04 04:09 -0700
Re: Program to compute amount of coins Bartc <bc@freeuk.com> - 2015-09-04 12:16 +0100
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-04 14:11 +0200
Re: Program to compute amount of coins Ike Naar <ike@iceland.freeshell.org> - 2015-09-04 19:06 +0000
Re: Program to compute amount of coins James Kuyper <jameskuyper@verizon.net> - 2015-09-04 15:12 -0400
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-05 01:54 +0200
Re: Program to compute amount of coins Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-05 01:01 +0100
Re: Program to compute amount of coins bartekltg <bartek@gmail.com> - 2015-09-05 02:50 +0200
Re: Program to compute amount of coins David Thompson <dave.thompson2@verizon.net> - 2015-09-20 18:35 -0400
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 18:09 +0200
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 17:11 +0100
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 18:38 +0200
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:08 -0700
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 18:20 +0100
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 19:32 +0200
Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-03 12:18 -0700
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:16 -0700
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 18:24 +0100
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:37 -0700
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 19:02 +0100
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-03 10:42 -0700
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-03 19:57 +0200
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 19:05 +0100
Re: Program to compute amount of coins Bartc <bc@freeuk.com> - 2015-09-03 21:23 +0100
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-03 21:55 +0100
Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-03 16:39 -0700
Re: Program to compute amount of coins Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 14:02 -0700
Re: Program to compute amount of coins Keith Thompson <kst-u@mib.org> - 2015-09-06 17:04 -0700
Re: Program to compute amount of coins Alla _ <modelling.data@gmail.com> - 2015-09-04 03:48 -0700
Re: Program to compute amount of coins bartekltg <bartekltg@gmail.com> - 2015-09-04 14:04 +0200
Re: Program to compute amount of coins Richard Heathfield <rjh@cpax.org.uk> - 2015-09-04 14:23 +0100
Page 3 of 3 — ← Prev page 1 2 [3]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 10:16 -0700 |
| Message-ID | <38b63296-fa9c-4a1e-a350-71ec5a9ba5d0@googlegroups.com> |
| In reply to | #69147 |
I've tried this:
#include <stdio.h>
#include <math.h>
int main(void)
{
float amount = 0;
int coin25 = 0;
int change = round(amount * 100);
printf ("Please, enter the amount of change: \n");
if (scanf("%f", &amount) == 1)
{
while (change >= 24)
{
coin25 = change/25;
amount = amount / 25;
}
}
printf ("%i %f\n", coin25, amount);
/** using amount here as well to check if the loop
works **/
Doesn't work
}
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 18:24 +0100 |
| Message-ID | <wn%Fx.392999$LU5.114780@fx12.am4> |
| In reply to | #69167 |
On 03/09/2015 18:16, Alla _ wrote:
> I've tried this:
> #include<stdio.h>
> #include<math.h>
>
> int main(void)
> {
> float amount = 0;
> int coin25 = 0;
> int change = round(amount * 100);
Too soon!
>
> printf ("Please, enter the amount of change: \n");
>
> if (scanf("%f",&amount) == 1)
> {
Should have done it here, after scanf.
>
> while (change>= 24)
> {
> coin25 = change/25;
> amount = amount / 25;
> }
Forget the loop. You don't need the loop.
coin25 = amount / 25;
amount = amount % 25;
Now you can move on to calculate smaller coins:
coin10 = amount / 10;
amount = amount % 10;
coin05 = amount / 5;
amount = amount % 5;
coin01 = amount;
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 10:37 -0700 |
| Message-ID | <3c727e7b-47c0-4574-bb74-ae4bd8aac61f@googlegroups.com> |
| In reply to | #69170 |
On Thursday, September 3, 2015 at 9:24:54 PM UTC+4, Richard Heathfield wrote:
> On 03/09/2015 18:16, Alla _ wrote:
> > I've tried this:
> > #include<stdio.h>
> > #include<math.h>
> >
> > int main(void)
> > {
> > float amount = 0;
> > int coin25 = 0;
> > int change = round(amount * 100);
>
> Too soon!
>
> >
> > printf ("Please, enter the amount of change: \n");
> >
> > if (scanf("%f",&amount) == 1)
> > {
>
> Should have done it here, after scanf.
>
> >
> > while (change>= 24)
> > {
> > coin25 = change/25;
> > amount = amount / 25;
> > }
>
> Forget the loop. You don't need the loop.
>
> coin25 = amount / 25;
> amount = amount % 25;
>
> Now you can move on to calculate smaller coins:
>
> coin10 = amount / 10;
> amount = amount % 10;
>
> coin05 = amount / 5;
> amount = amount % 5;
>
> coin01 = amount;
>
> --
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within
honestly, before posting here I tried
to put change after the scanf, and
received an endless program.
#include <stdio.h>
#include <math.h>
int main(void)
{
float amount = 0;
int coin25 = 0;
printf ("Please, enter the amount of change: \n");
if (scanf("%f", &amount) == 1)
{
int change = round(amount * 100);
while (change > 24)
{
coin25 = change/25;
amount = amount / 25;
}
}
printf ("%i %f\n", coin25, amount);
}
I also tried without the loop, and, of course,
as you said, it works perfectly.
but I do have an issue with counting modulo,
because amount is a float. I tried both 25
and 25.0; get the same error message (as
the one I asked about in one of my previous
threads some time ago, and received an explanation
that this issue is a norm):
grtrain4.c: In function 'main':
grtrain4.c:19: error: invalid operands to binary % (have 'float' and 'int')
and
grtrain4.c: In function 'main':
grtrain4.c:19: error: invalid operands to binary % (have 'float' and 'double')
#include <stdio.h>
#include <math.h>
int main(void)
{
float amount = 0;
float remainder;
int coin25 = 0;
printf ("Please, enter the amount of change: \n");
if (scanf("%f", &amount) == 1)
{
int change = round(amount * 100);
if (change > 24)
{
coin25 = change/25;
amount = amount % 25.0;
}
}
printf ("%i %.2f\n", coin25, amount);
}
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 19:02 +0100 |
| Message-ID | <GW%Fx.373305$0B6.86751@fx27.am4> |
| In reply to | #69173 |
On 03/09/2015 18:37, Alla _ wrote:
<snip>
> honestly, before posting here I tried
> to put change after the scanf, and
> received an endless program.
Did you find out why? That's an important part of learning to program.
Instead of trying things at random, you need to sit back and study the
problem, by all means asking here for help if you can't see the answer,
and keep studying the thing that went wrong until you know *why* it went
wrong. Then you fix that one thing, and re-test. If it still doesn't
work, you go round the cycle again.
> #include<stdio.h>
> #include<math.h>
>
> int main(void)
> {
> float amount = 0;
> int coin25 = 0;
>
> printf ("Please, enter the amount of change: \n");
>
> if (scanf("%f",&amount) == 1)
> {
> int change = round(amount * 100);
>
> while (change> 24)
> {
> coin25 = change/25;
> amount = amount / 25;
This loop will never terminate because change never changes!
<snip>
> but I do have an issue with counting modulo,
> because amount is a float. I tried both 25
> and 25.0; get the same error message (as
> the one I asked about in one of my previous
> threads some time ago, and received an explanation
> that this issue is a norm):
>
> grtrain4.c: In function 'main':
> grtrain4.c:19: error: invalid operands to binary % (have 'float' and 'int')
Right. You can't do % (integer modulo arithmetic) on floats or doubles,
only on integers.
But you don't *need* to do modulo on "amount". You need to do it on
"change" - or at least you would, if you were not under the constraint
of using a loop.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 10:42 -0700 |
| Message-ID | <b1788ecd-109e-4a64-8927-e426bb691b81@googlegroups.com> |
| In reply to | #69147 |
On Thursday, September 3, 2015 at 7:54:28 PM UTC+4, Alla _ wrote:
> Hello!
>
> I am working on a program that should
> compute the amount of coins: the user
> enters amount, and, to make it simple
> any amount entered is in dollars, not
> cents.
>
> Therefore, I have to:
> - prompt the user to enter the number
> - convert that into cents, round the result
> - compute how much of 25¢ coins are
> contained is this number
>
> Initially, I split the program, wrote
> the 'prompt the user part', and then
> combined with a computational part.
>
> "Prompting" part, including "rounding",
> works.
>
> Computational part doesn't produce the
> correct outcome. I've tried different
> approaches.
>
> Please, help with hints and tips, but
> do not write a program for me - if I fail
> to get your tips, I will ask again :)
>
> "Algorithm" that I thought can help
> to solve the computational part is:
>
> use while loop, take amount; until amount
> is bigger than 24, reduce amount by 25, and
> count the number of 25¢ coins with each iteration
> of the loop.
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void)
> {
> float amount; /** I should ask for a float value in $,
> and then convert it to amount in cents**/
> int coin25 = 0;
> int change = round(amount * 100);
>
> printf ("Please, enter your amount in $: \n");
>
> if (scanf("%f", &amount) == 1)
> {
>
> while (change >= 24)
> {
> change = change - 25;
> coin25++;
> }
>
> }
> printf ("%i\n", coin25);
>
> }
>
> Thank you!
honestly, before posting here I tried
to put change after the scanf, and
received an endless program.
#include <stdio.h>
#include <math.h>
int main(void)
{
float amount = 0;
int coin25 = 0;
printf ("Please, enter the amount of change: \n");
if (scanf("%f", &amount) == 1)
{
int change = round(amount * 100);
while (change > 24)
{
coin25 = change/25;
amount = amount / 25;
}
}
printf ("%i %f\n", coin25, amount);
}
I also tried without the loop, and, of course,
as you said, it works perfectly.
but I do have an issue with counting modulo,
because amount is a float. I tried both 25
and 25.0; get the same error message (as
the one I asked about in one of my previous
threads some time ago, and received an explanation
that this issue is a norm):
grtrain4.c: In function 'main':
grtrain4.c:19: error: invalid operands to binary % (have 'float' and 'int')
and
grtrain4.c: In function 'main':
grtrain4.c:19: error: invalid operands to binary % (have 'float' and 'double')
#include <stdio.h>
#include <math.h>
int main(void)
{
float amount = 0;
int coin25 = 0;
printf ("Please, enter the amount of change: \n");
if (scanf("%f", &amount) == 1)
{
int change = round(amount * 100);
if (change > 24)
{
coin25 = change/25;
amount = amount % 25.0;
}
}
printf ("%i %.2f\n", coin25, amount);
}
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-03 19:57 +0200 |
| Message-ID | <msa1ll$9u5$1@node2.news.atman.pl> |
| In reply to | #69175 |
On 03.09.2015 19:42, Alla _ wrote:
> On Thursday, September 3, 2015 at 7:54:28 PM UTC+4, Alla _ wrote:
>> Hello!
>>
>> I am working on a program that should
>> compute the amount of coins: the user
>> enters amount, and, to make it simple
>> any amount entered is in dollars, not
>> cents.
>>
>> Therefore, I have to:
>> - prompt the user to enter the number
>> - convert that into cents, round the result
>> - compute how much of 25¢ coins are
>> contained is this number
>>
>> Initially, I split the program, wrote
>> the 'prompt the user part', and then
>> combined with a computational part.
>>
>> "Prompting" part, including "rounding",
>> works.
>>
>> Computational part doesn't produce the
>> correct outcome. I've tried different
>> approaches.
>>
>> Please, help with hints and tips, but
>> do not write a program for me - if I fail
>> to get your tips, I will ask again :)
>>
>> "Algorithm" that I thought can help
>> to solve the computational part is:
>>
>> use while loop, take amount; until amount
>> is bigger than 24, reduce amount by 25, and
>> count the number of 25¢ coins with each iteration
>> of the loop.
>>
>> #include <stdio.h>
>> #include <math.h>
>>
>> int main(void)
>> {
>> float amount; /** I should ask for a float value in $,
>> and then convert it to amount in cents**/
>> int coin25 = 0;
>> int change = round(amount * 100);
>>
>> printf ("Please, enter your amount in $: \n");
>>
>> if (scanf("%f", &amount) == 1)
>> {
>>
>> while (change >= 24)
>> {
>> change = change - 25;
>> coin25++;
>> }
>>
>> }
>> printf ("%i\n", coin25);
>>
>> }
>>
>> Thank you!
>
> honestly, before posting here I tried
> to put change after the scanf, and
> received an endless program.
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void)
> {
> float amount = 0;
> int coin25 = 0;
>
> printf ("Please, enter the amount of change: \n");
>
> if (scanf("%f", &amount) == 1)
> {
> int change = round(amount * 100);
>
> while (change > 24)
> {
> coin25 = change/25;
> amount = amount / 25;
> }
>
> }
> printf ("%i %f\n", coin25, amount);
>
> }
>
> I also tried without the loop, and, of course,
> as you said, it works perfectly.
> but I do have an issue with counting modulo,
> because amount is a float. I tried both 25
> and 25.0; get the same error message (as
> the one I asked about in one of my previous
> threads some time ago, and received an explanation
> that this issue is a norm):
>
> grtrain4.c: In function 'main':
> grtrain4.c:19: error: invalid operands to binary % (have 'float' and 'int')
>
> and
> grtrain4.c: In function 'main':
> grtrain4.c:19: error: invalid operands to binary % (have 'float' and 'double')
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void)
> {
> float amount = 0;
> int coin25 = 0;
>
> printf ("Please, enter the amount of change: \n");
>
> if (scanf("%f", &amount) == 1)
> {
> int change = round(amount * 100);
>
> if (change > 24)
> {
> coin25 = change/25;
> amount = amount % 25.0;
The loop looks at 'change'. But you newer change "change" (:))
Do no touch 'amount' after "int change = round(amount * 100);"
line.
So haw the last line should look like? Change only this
one line:
> amount = amount % 25.0;
bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 19:05 +0100 |
| Message-ID | <a_%Fx.415206$3n4.193195@fx35.am4> |
| In reply to | #69175 |
On 03/09/2015 18:42, Alla _ wrote: <snip> > honestly, before posting here I tried > to put change after the scanf, Good. That's where it belongs. > and > received an endless program. That's not because you moved "change"'s assignment to below the scanf. It's because you don't modify "change" in the loop that depends for its termination on "change" eventually reaching a value of 24 or lower. By the way, you don't have to post essentially the same article in replies to both barteklpg and myself. Each of us can see both subthreads. You're asking for the same help twice, which is a waste of your time. Slow down, take a deep breath, and think more carefully about what you're doing. (And by the way, you're doing fine. This stuff isn't easy. You will get the hang of it if you persevere, and if you think things through logically.) -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-09-03 21:23 +0100 |
| Message-ID | <msaa4m$ju3$1@dont-email.me> |
| In reply to | #69147 |
On 03/09/2015 16:54, Alla _ wrote: > I am working on a program that should > compute the amount of coins: the user > enters amount, and, to make it simple > any amount entered is in dollars, not > cents. > > Therefore, I have to: > - prompt the user to enter the number > - convert that into cents, round the result > - compute how much of 25¢ coins are > contained is this number So far, that is not very challenging, if the input is whole dollars. So if someone enters 3 dollars, you just multiply by 4 to get the number of quarters! It's more interesting to start with dollars and cents. Even then, for simply counting quarters, you don't need a loop. The number of whole quarters is: quarters = cents / 25; There will be change of 0 to 24 cents, which is: change = cents % 25; This can be faster than a while loop too, for very large amounts. If you are using floats, they will have limited accuracy of 6 or 7 figures and it can be a few cents out for large inputs of tens of millions of dollars. This can cause problems with terminating while loops too. So aim to convert to integer values as soon as possible. (Note that ordinary C integers might be limited in range to some 2 billion cents or around $20m.) -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 21:55 +0100 |
| Message-ID | <ut2Gx.246515$i22.200097@fx05.am4> |
| In reply to | #69201 |
On 03/09/2015 21:23, Bartc wrote: > On 03/09/2015 16:54, Alla _ wrote: > >> I am working on a program that should >> compute the amount of coins: the user >> enters amount, and, to make it simple >> any amount entered is in dollars, not >> cents. >> >> Therefore, I have to: >> - prompt the user to enter the number >> - convert that into cents, round the result >> - compute how much of 25¢ coins are >> contained is this number > > So far, that is not very challenging, if the input is whole dollars. He didn't say whole dollars. He said dollars. So 1.43 dollars rather than 143 pennies. <snip stuff already discussed> -- Richard Heathfield Email: rjh at cpax dot org dot uk "Usenet is a strange place" - dmr 29 July 1999 Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-03 16:39 -0700 |
| Message-ID | <lnfv2vqdig.fsf@kst-u.example.com> |
| In reply to | #69201 |
Bartc <bc@freeuk.com> writes:
[...]
> (Note that ordinary C integers might be limited in range to some 2
> billion cents or around $20m.)
C's type int typically (on modern systems) has an upper bound of
2**31-1, but it's allowed to be a small as 65535. (POSIX requires int
to be at least 32 bits.)
Type long is guaranteed to have an upper bound of at least 2**31-1.
long long is guaranteed to have an upper bound of at least 2**63-1 --
but it wasn't introduced until C99.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-09-06 14:02 -0700 |
| Message-ID | <kfnoahfqn1y.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #69241 |
Keith Thompson <kst-u@mib.org> writes: > Bartc <bc@freeuk.com> writes: > [...] >> (Note that ordinary C integers might be limited in range to some 2 >> billion cents or around $20m.) > > C's type int typically (on modern systems) has an upper bound of > 2**31-1, but it's allowed to be a small as 65535. [snip] Of course you meant to say 32767.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-06 17:04 -0700 |
| Message-ID | <lnwpw3t7px.fsf@kst-u.example.com> |
| In reply to | #69672 |
Tim Rentsch <txr@alumni.caltech.edu> writes:
> Keith Thompson <kst-u@mib.org> writes:
>> Bartc <bc@freeuk.com> writes:
>> [...]
>>> (Note that ordinary C integers might be limited in range to some 2
>>> billion cents or around $20m.)
>>
>> C's type int typically (on modern systems) has an upper bound of
>> 2**31-1, but it's allowed to be a small as 65535. [snip]
>
> Of course you meant to say 32767.
Yes, of course.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-04 03:48 -0700 |
| Message-ID | <b94826b4-3374-4b09-a49a-b2ab6ee9b5d8@googlegroups.com> |
| In reply to | #69147 |
Hello again :)
Once again thank you so much for your help
and explanations. I am also happy to learn
from the more advanced conversation you had
within this humble thread.
Sorry for getting back with my issue.
It is so important for me to understand
why the while loop doesn't work.
I have to admit, I don't see the problem.
I will post a while loop thing below (with
my questions and explanations on how I think
about it).
But before that, please, take a look at the neat
and correct option of this program that I finalized
based on your suggestion to omit the while loop
and, just as Richard posted above, using only division:
/**
Program to compute the number of coins
contained in any number input by the user;
without the use of any loop.
**/
#include <stdio.h>
#include <math.h>
int main(void)
{
float amount;
int coin25, coin10, coin05, coin01;
printf ("Please, enter the amount of change: \n");
if (scanf("%f", &amount) == 1)
{
int change = round(amount * 100);
coin25 = change / 25;
change = change % 25;
/** after trying and testing, I've realized that,
as with my previous pyramid program, after
change = change % 25 the variable "change"
has a new value, and the compiler now
treats "change" as the remainder from the division
of initial change (same below); this I didn't
realize before.
**/
coin10 = change / 10;
change = change % 10;
coin05 = change / 5;
change = change % 5;
coin01 = change;
}
printf ("%i\n", coin25 + coin10 + coin05 + coin01);
}
As to my while loop issue (I have to understand it
once for all).
#include <stdio.h>
#include <math.h>
int main(void)
{
float amount = 0.0;
int quarters = 0;
int dimes = 0;
int change = 0;
printf ("Please, enter the amount of change: \n");
if (scanf("%f", &amount) == 1)
{
int change = round(amount * 100);
/** the amount is entered by the user, and
after rounding is copied into change;
change is not equal to zero anymore
**/
while (change >= 25)
/** change is an integer, from which I
subtract 25 until "change" is equal
to 25; hence, after this loop the
value of change should be any number
starting from 24;
also, with each iteration of the loop
I increment quarters by 1.
**/
{
quarters++;
change -= 25;
}
/** same as above, but the loop starts at the value of
change of 24 or lower, but above or equal 10
**/
while (change >= 10)
{
dimes++;
change -= 10;
}
printf ("%i\n", quarters + dimes);
}
This program wouldn't even compile now :)
Here is an error message:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-04 14:04 +0200 |
| Message-ID | <msc1c6$2dj$1@node1.news.atman.pl> |
| In reply to | #69283 |
On 04.09.2015 12:48, Alla _ wrote:
> printf ("%i\n", coin25 + coin10 + coin05 + coin01);
This return a total number of coins. If you think it does'nt matter,
can you send me a 10000 quaters, and I will send you 11000 penies?
This will get you 1000 coins for free!
;-)
>
> As to my while loop issue (I have to understand it
...
> once for all).
> while (change >= 25)
> {
> quarters++;
> change -= 25;
> }
To understand, OK, but remember, that is a stupid
way to compute division.
>
>
> /** same as above, but the loop starts at the value of
> change of 24 or lower, but above or equal 10
> **/
> while (change >= 10)
> {
> dimes++;
> change -= 10;
> }
>
> printf ("%i\n", quarters + dimes);
>
> }
>
> This program wouldn't even compile now :)
> Here is an error message:
>
> Undefined symbols for architecture x86_64:
> "_main", referenced from:
> start in crt1.10.6.o
> ld: symbol(s) not found for architecture x86_64
> collect2: ld returned 1 exit status
Can't recreate. It works (after I add last closing "}",
but its absent would not generate this error code).
The problem is during linking, it does not see you main function.
bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-04 14:23 +0100 |
| Message-ID | <YWgGx.400258$vg2.393048@fx34.am4> |
| In reply to | #69283 |
On 04/09/2015 11:48, Alla _ wrote:
> Hello again :)
Hi Alla. barteklpg has addressed most of your concerns, I think, so I'm
only going to make two points about your article:
<snip>
> coin25 = change / 25;
> change = change % 25;
>
> /** after trying and testing, I've realized that,
> as with my previous pyramid program, after
> change = change % 25 the variable "change"
> has a new value, and the compiler now
> treats "change" as the remainder from the division
> of initial change (same below); this I didn't
> realize before.
> **/
Right.
C objects have a (generally quite small) amount of memory in which they
can store one value. When you put a value in there, any previous value
is destroyed, overwritten by the new value. C objects do not enjoy
longlasting friendships with previous assignment statements. If you do this:
y = 6;
x = y;
then x will indeed take the value 6. But if you want x to have the value
42, it's no good doing this:
y = 42;
because that won't change x even slightly. x took y's value earlier on,
but that was a once-for-all-time event. It doesn't mean x automatically
updates every time y changes. No, if you want x to hold 42, you have to say:
x = 42;
Or this would work too:
y = 42;
x = y;
C's objects are much simpler than you thought. They only hold a value.
If you want them to hold a different value, you have to put that
different value there yourself.
<snip>
> #include<stdio.h>
> #include<math.h>
>
> int main(void)
> {
> float amount = 0.0;
> int quarters = 0;
> int dimes = 0;
> int change = 0;
>
> printf ("Please, enter the amount of change: \n");
>
> if (scanf("%f",&amount) == 1)
> {
> int change = round(amount * 100);
This is called "shadowing". You now have two objects called "change" -
one that's valid outside the if, and one that's valid inside. If that's
what you wanted, fine, but it probably isn't. To get rid of the
shadowing, change:
int change = round(amount * 100);
to
change = round(amount * 100);
<snip>
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.c
csiph-web