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 | 20 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 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-08 18:29 +0200 |
| Message-ID | <msn2d4$epn$1@node1.news.atman.pl> |
| In reply to | #69881 |
On 08.09.2015 11:39, Tim Rentsch wrote:
> bartekltg <bartekltg@gmail.com> writes:
>
>> On 06.09.2015 22:58, Tim Rentsch wrote:
>>> bartekltg <bartekltg@gmail.com> writes:
>>>
>>>> On 03.09.2015 22:03, Ben Bacarisse wrote:
>>>>> bartekltg <bartekltg@gmail.com> writes:
>>>>> <snip>
>>>>>
>>>>> Most teachers would rather not have to explain that test too early on!
>>>>> Hence I'd suggest const int coin_value[]= {25, 10, 5, 1, 0}; and a loop
>>>>> test of coin_value[i] > 0.
>>>>> As a consequence, since I am wary about
>>>>> teaching for loop "mission creep", this would become a while loop:
>>>>>
>>>>> int i = 0;
>>>>> while (coin_value[i] > 0) {
>>>>> ....
>>>>> i += 1;
>>>>> }
>>>>
>>>> Or hardcode array length.
>>>> But your version is safer.
>>>> const int N = 5;
>>>> int coin_value[N]= {25, 10, 5,1};
>>>> do not generates a error or warning,
>>>> and the last entry is uninitialized.
>>>
>>> In C these two lines represent a constraint violation. The
>>> violations are different in C90 and C99 (C11 may be either of
>>> the two), but it's always a violation.
>>
>> Yes, I admit, I haven't the slightest idea that
>> const int N;
>> int T[N]...
>> is problematic in C.
>
> Sorry, based on your comments in other postings I got
> the impression you were more of an expert than you are
> apparently.
I'm for sure not an expert in C.
;]
> I tend to give somewhat terse replies to
> people I think are experts, saving longer explanations
> for people who look like they need more help.
Your first answer was enough.
(also Keith wrote something similar earlier).
I didn't ask for more explanation, I just admitted
I didn't known that "const variables" aren't "const" enough.
Too much c++ ;-)
> Needless
> to say sometimes I guess wrong and this seems to be
> one of those times. Hopefully it all got straightened
> out.
>
> If you don't have a copy of the C standard, let me
> suggest that you get one, especially since you seem
> more than capable of digesting it -
>
> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
Thanks.
Are you suggesting to use the standard as an advance learning
material? I heard opposite opinions.
bartekltg
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-09-08 13:15 -0400 |
| Message-ID | <msn50m$jm$1@dont-email.me> |
| In reply to | #69932 |
On 09/08/2015 12:29 PM, bartekltg wrote: > On 08.09.2015 11:39, Tim Rentsch wrote: ... >> If you don't have a copy of the C standard, let me >> suggest that you get one, especially since you seem >> more than capable of digesting it - >> >> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf > > Thanks. > > Are you suggesting to use the standard as an advance learning > material? I heard opposite opinions. The standard is an excellent reference work, and a lousy tutorial. If you're serious about programming in C, you need a reference work. Personally, I'd recommend n1570.pdf instead - it's the latest freely available version. -- James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-08 10:16 -0700 |
| Message-ID | <ln8u8gsug8.fsf@kst-u.example.com> |
| In reply to | #69932 |
bartekltg <bartekltg@gmail.com> writes:
> On 08.09.2015 11:39, Tim Rentsch wrote:
[...]
>> If you don't have a copy of the C standard, let me
>> suggest that you get one, especially since you seem
>> more than capable of digesting it -
>>
>> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
>
> Are you suggesting to use the standard as an advance learning
> material? I heard opposite opinions.
The standard certainly isn't designed as a tutorial. My suggestion (not
speaking for Tim but I suspect he'll agree) is to get a copy of a draft
of the standard, look it over, and decide for yourself how useful it is
to you. It is of course the definitive reference for the C language.
Incidentally, the C11 draft is
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
N1256 consists of the C99 standard with the three Technical Corrigenda
merged into it. It is, for most purposes, more useful than the
published C99 standard.
N1570 is a draft of C11 published *before* the ISO standard. The
differences between it and the official standard are minor. It's the
version I refer to most often, but N1256 might be more useful to you,
since support for C11 is not widespread and the C11-specific features
aren't particularly necessary for most purposes.
--
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-09 09:37 -0700 |
| Message-ID | <kfnpp1rlfbj.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #69932 |
bartekltg <bartekltg@gmail.com> writes: > On 08.09.2015 11:39, Tim Rentsch wrote: >> bartekltg <bartekltg@gmail.com> writes: >>> ]snip] >> >> If you don't have a copy of the C standard, let me >> suggest that you get one, especially since you seem >> more than capable of digesting it - >> >> http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf > > Thanks. > > Are you suggesting to use the standard as an advance learning > material? I heard opposite opinions. I wouldn't recommend the C standard to beginners. I also wouldn't recommend the C standard as a tutorial. It isn't good for either of those. I do recommend it to you, based on my impressions of your familiarity with C and/or C++ and of your general background and intellectual capabilities. There are two different modes of interest here. Just reading the C standard, as one would a book, is slow going. But doing that provides an excellent foundation for understanding how the text deals with various kinds of issues. In particular, reading parts of the text in isolation can often lead to wrong conclusions about what is meant; for better or worse, the text is written as an organic whole, not independent paragraphs. Second, after you have gone through the exercise of reading, oh, let's say at least up to the start of section 7 and through 7.1, you will be in a good position to use the text as a reference for answering specific questions that may come up. The C standard is a reference resource, but to make full use of that resource one needs a general familiarity with the text and how it is organized; hence the recommendation to first read it. When you get to the second stage, I suggest you further your C expertise by listening in on the various technical questions asked in comp.lang.c. More specifically, whenever anyone asks a C technical question, even if you're pretty sure you know the answer, take the time to look up in the C standard what it has to say on the question. I still do this routinely, and find that I learn (or re-learn) something new often enough to make it be worthwhile. After (and only after) you have consulted what the text has to say about the poser's question, only then read through the replies from other posters. If their answers agree with yours, all well and good, but if they don't, then repeat the process of consulting the text until you either agree with what they are saying or think their statements seem to be mistaken. In the second case you are in an excellent position to ask a question, which will be a benefit both to you and to the group whether or not your conclusions are right. So for what it's worth there are my suggestions, along with some of the motivations for making them.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-04 04:09 -0700 |
| Message-ID | <7163771e-9b76-4553-b81b-a5d280f3322c@googlegroups.com> |
| In reply to | #69176 |
On Thursday, September 3, 2015 at 9:53:16 PM UTC+4, bartekltg wrote:
> On 03.09.2015 19:30, Richard Heathfield wrote:
> > On 03/09/2015 18:18, Alla _ wrote:
> >
> > <snip>
> >
> >> According to assignment, I have to use
> >> loops (any loop of my choice).
> >> However, I will now, of course, try without
> >> the loop, as well.
> >
> > If there are constraints on your problem, you should mention them at the
> > outset.
> >
> > Okay, so if you're not allowed to do it the right way, you'll have to
> > work out how to do it the wrong way:
> >
> > if(scanf("%f", amount) == 1)
> > {
> > int pennies = amount * 100;
> > int quarters = 0;
> > int dimes = 0;
> > int nickels = 0;
> > while(pennies >= 25)
> > {
> > ++quarters;
> > pennies -= 25;
> > }
> > while(pennies > 10)
> > {
> > ++dimes; and so on.
>
> I'm sure that these loops teacher mentioned was a loop over "face
> value".
>
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void)
> {
> double amount;
> const unsigned int coin_value[]= {25,10, 5,1 } ;
>
> printf ("Please, enter your amount in $: \n");
>
> if (scanf("%lf", &amount) == 1)
> {
> if (amount > pow(2.0,32.0)*0.9999){
> printf ( "Not enough quarters in the universe\n" );
> }
> else
> {
> unsigned int cents = amount*100.0;
> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++)
I would be grateful if you explain what does
sizeof(coin_value)/sizeof(coin_value[0]
do; how does it work here?
> {
> int coins = cents/coin_value[i];
> cents = cents%coin_value[i];
> printf("%d x %dc\n",coins, coin_value[i]);
> }
> }
>
> }
> }
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-09-04 12:16 +0100 |
| Message-ID | <msbufb$ju7$1@dont-email.me> |
| In reply to | #69287 |
On 04/09/2015 12:09, Alla _ wrote:
>> const unsigned int coin_value[]= {25,10, 5,1 } ;
>> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++)
>
> I would be grateful if you explain what does
> sizeof(coin_value)/sizeof(coin_value[0]
> do; how does it work here?
It's what C does to compute the elements in an array. The number of
bytes in the whole thing, divided by the number of bytes in one element.
Like trying to work out how many floors in a building, by dividing the
height of the building by the height of each floor.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-04 14:11 +0200 |
| Message-ID | <msc1on$7hp$1@node2.news.atman.pl> |
| In reply to | #69287 |
On 04.09.2015 13:09, Alla _ wrote:
> On Thursday, September 3, 2015 at 9:53:16 PM UTC+4, bartekltg wrote:
>> On 03.09.2015 19:30, Richard Heathfield wrote:
>>> On 03/09/2015 18:18, Alla _ wrote:
>>>
>>> <snip>
>>>
>>>> According to assignment, I have to use
>>>> loops (any loop of my choice).
>>>> However, I will now, of course, try without
>>>> the loop, as well.
>>>
>>> If there are constraints on your problem, you should mention them at the
>>> outset.
>>>
>>> Okay, so if you're not allowed to do it the right way, you'll have to
>>> work out how to do it the wrong way:
>>>
>>> if(scanf("%f", amount) == 1)
>>> {
>>> int pennies = amount * 100;
>>> int quarters = 0;
>>> int dimes = 0;
>>> int nickels = 0;
>>> while(pennies >= 25)
>>> {
>>> ++quarters;
>>> pennies -= 25;
>>> }
>>> while(pennies > 10)
>>> {
>>> ++dimes; and so on.
>>
>> I'm sure that these loops teacher mentioned was a loop over "face
>> value".
>>
>>
>> #include <stdio.h>
>> #include <math.h>
>>
>> int main(void)
>> {
>> double amount;
>> const unsigned int coin_value[]= {25,10, 5,1 } ;
>>
>> printf ("Please, enter your amount in $: \n");
>>
>> if (scanf("%lf", &amount) == 1)
>> {
>> if (amount > pow(2.0,32.0)*0.9999){
>> printf ( "Not enough quarters in the universe\n" );
>> }
>> else
>> {
>> unsigned int cents = amount*100.0;
>> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++)
>
> I would be grateful if you explain what does
> sizeof(coin_value)/sizeof(coin_value[0]
> do; how does it work here?
>
>> {
>> int coins = cents/coin_value[i];
>> cents = cents%coin_value[i];
>> printf("%d x %dc\n",coins, coin_value[i]);
>> }
>> }
>>
>> }
>> }
>
sizeof(coin_value) is the size (in bytes) of whole array.
sizeof(coin_value[0]) is the size of the first element,
also in bytes. In c the size of all elements of a array is the same.
So sizeof(coin_value)/sizeof(coin_value[0]) if the number of elements
in the array.
One can write it clearer:
int number_of_coin_types = sizeof(coin_value)/sizeof(coin_value[0])
...
for (int i=0; i<number_of_coin_types; i++)
bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Date | 2015-09-04 19:06 +0000 |
| Message-ID | <slrn3vfsmujr0i.nb7.ike@iceland.freeshell.org> |
| In reply to | #69176 |
On 2015-09-03, bartekltg <bartekltg@gmail.com> wrote: > for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++) Is there a particular reason why coin_value[0] was chosen, and not, say, coin_value[42]? Is there something special with the element at index zaro?
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-09-04 15:12 -0400 |
| Message-ID | <55E9ED02.1030905@verizon.net> |
| In reply to | #69346 |
On 09/04/2015 03:06 PM, Ike Naar wrote: > On 2015-09-03, bartekltg <bartekltg@gmail.com> wrote: >> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++) > > Is there a particular reason why coin_value[0] was chosen, > and not, say, coin_value[42]? Is there something special with > the element at index zaro? No. It involves typing one less digit, and that's about the extent of the justification; coin_value[9] would be just as good, from that point of view. I would usually use *coin_value, which is equivalent, shorter, and less obviously arbitrary.
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-05 01:54 +0200 |
| Message-ID | <msdauu$csd$1@node1.news.atman.pl> |
| In reply to | #69346 |
On 04.09.2015 21:06, Ike Naar wrote: > On 2015-09-03, bartekltg <bartekltg@gmail.com> wrote: >> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++) > > Is there a particular reason why coin_value[0] was chosen, > and not, say, coin_value[42]? Is there something special with > the element at index zaro? Someone my ask: "Why 42. Does coin_values[42] exist? Will it blows in my face?" ;-) but it will work. I saw different, shorter version, sizeof(coin_value)/sizeof(*coin_value); bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-09-05 01:01 +0100 |
| Message-ID | <87bndhlonl.fsf@bsb.me.uk> |
| In reply to | #69378 |
bartekltg <bartekltg@gmail.com> writes: > On 04.09.2015 21:06, Ike Naar wrote: >> On 2015-09-03, bartekltg <bartekltg@gmail.com> wrote: >>> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++) >> >> Is there a particular reason why coin_value[0] was chosen, >> and not, say, coin_value[42]? Is there something special with >> the element at index zaro? > > Someone my ask: "Why 42. Does coin_values[42] exist? > Will it blows in my face?" ;-) > but it will work. > > I saw different, shorter version, > sizeof(coin_value)/sizeof(*coin_value); You can make it shorter still, though you might not like it since it uses what appears to be your least favourite character: a space! sizeof coin_value/sizeof*coin_value; (I would add spaces.) -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartek@gmail.com> |
|---|---|
| Date | 2015-09-05 02:50 +0200 |
| Message-ID | <msde8g$hpm$1@speranza.aioe.org> |
| In reply to | #69380 |
On 05.09.2015 02:01, Ben Bacarisse wrote: > bartekltg <bartekltg@gmail.com> writes: > >> On 04.09.2015 21:06, Ike Naar wrote: >>> On 2015-09-03, bartekltg <bartekltg@gmail.com> wrote: >>>> for (int i=0;i<sizeof(coin_value)/sizeof(coin_value[0]);i++) >> I saw different, shorter version, >> sizeof(coin_value)/sizeof(*coin_value); > > You can make it shorter still, though you might not like it since it > uses what appears to be your least favourite character: a space! Why? Spaces are great! I have even write somewhere a second version of the for loop from above, with two additional spaces! ;-) > > sizeof coin_value/sizeof*coin_value; Without brackets I always have to stop and think about order of operations. > (I would add spaces.) I was surprised that in C there is no macro for that. We can write one. And add parentheses. There is never enough parentheses, especial a macro. No, I'm not Lisp programmer. :) bartekltg
[toc] | [prev] | [next] | [standalone]
| From | David Thompson <dave.thompson2@verizon.net> |
|---|---|
| Date | 2015-09-20 18:35 -0400 |
| Message-ID | <atcuva9ghna3u5rmmdgn0mu7j61ooeoqj7@4ax.com> |
| In reply to | #69176 |
On Thu, 03 Sep 2015 19:53:05 +0200, bartekltg <bartekltg@gmail.com>
wrote:
<snip: homework>
> double amount;
> if (amount > pow(2.0,32.0)*0.9999){
> printf ( "Not enough quarters in the universe\n" );
> }
There are more than 4 billion quarters in the US alone, not
considering other countries, planets, systems, and galaxies.
[toc] | [prev] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-03 18:09 +0200 |
| Message-ID | <ms9rbq$3m5$1@node2.news.atman.pl> |
| In reply to | #69147 |
On 03.09.2015 17:54, 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**/
Amount in uninitialized. Propabli equal to 0.0
> int coin25 = 0;
> int change = round(amount * 100);
'Change' is computed based on uninitiated variable 'amount'.
It propably is equal to 0.
>
> printf ("Please, enter your amount in $: \n");
>
> if (scanf("%f", &amount) == 1)
You put value from input to 'amount', but 'change'
is still zero. Do you earlier write only in mathematica?
This is the same problem as in #triangle,
"int change = round(amount * 100);"
do not mean a relation between this two variables.
It mean 'take current value of amount, compute a function
and the result put in variable "change"'.
And the are independent.
You have to compute value of change _after_ you
put input into 'amount'.
> {
>
> while (change >= 24)
> {
> change = change - 25;
> coin25++;
> }
Hmmm.
Do you hear about such operation as division?
try
coin25 = change/25;
> }
> printf ("%i\n", coin25);
>
> }
>
> Thank you!
>
bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 17:11 +0100 |
| Message-ID | <8j_Fx.427801$121.161018@fx19.am4> |
| In reply to | #69152 |
On 03/09/2015 17:09, bartekltg wrote: > On 03.09.2015 17:54, Alla _ wrote: <snip> >> float amount; /** I should ask for a float value in $, >> and then convert it to amount in cents**/ > > Amount in uninitialized. Propabli equal to 0.0 > >> int coin25 = 0; >> int change = round(amount * 100); > > 'Change' is computed based on uninitiated variable 'amount'. Good spot. I completely missed that. <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] | [next] | [standalone]
| From | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-03 18:38 +0200 |
| Message-ID | <ms9t2t$5ce$1@node2.news.atman.pl> |
| In reply to | #69153 |
On 03.09.2015 18:11, Richard Heathfield wrote: > On 03/09/2015 17:09, bartekltg wrote: >> On 03.09.2015 17:54, Alla _ wrote: > > <snip> > >>> float amount; /** I should ask for a float value in $, >>> and then convert it to amount in cents**/ >> >> Amount in uninitialized. Propabli equal to 0.0 *probably >> >>> int coin25 = 0; >>> int change = round(amount * 100); >> >> 'Change' is computed based on uninitiated variable 'amount'. *uninitialized WTF, I'm writing;-) > Good spot. I completely missed that. After I have looked at the code I didn't see anything wrong, sp pasted it to, (always open;) QTc and ran. The answer: 0 was a surprise to me:) Alla_ has done identical mistake in his previous thread. It can be habit from a computer algebra system. Code in mathematica: square := side^2 side := 6; Print[square] side = 100; Print[square] it will returns: 36 10000 bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-09-03 10:08 -0700 |
| Message-ID | <07ead014-6486-43d1-80c5-a1b6e35e629e@googlegroups.com> |
| In reply to | #69152 |
On Thursday, September 3, 2015 at 8:09:44 PM UTC+4, bartekltg wrote:
> On 03.09.2015 17:54, 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**/
>
> Amount in uninitialized. Propabli equal to 0.0
>
> > int coin25 = 0;
> > int change = round(amount * 100);
>
> 'Change' is computed based on uninitiated variable 'amount'.
> It propably is equal to 0.
>
> >
> > printf ("Please, enter your amount in $: \n");
> >
> > if (scanf("%f", &amount) == 1)
>
> You put value from input to 'amount', but 'change'
> is still zero. Do you earlier write only in mathematica?
> This is the same problem as in #triangle,
>
> "int change = round(amount * 100);"
>
> do not mean a relation between this two variables.
> It mean 'take current value of amount, compute a function
> and the result put in variable "change"'.
> And the are independent.
>
I am not sure I understand what the difference is.
I intended exactly as you say: 'take current value
of amount,compute a function and the result put in
variable "change"'.
Then, within the loop, decrement 'change' with
each iteration of the loop.
Do you say that this is incorrect?
> You have to compute value of change _after_ you
> put input into 'amount'.
>
>
> > {
> >
> > while (change >= 24)
> > {
> > change = change - 25;
> > coin25++;
> > }
>
> Hmmm.
> Do you hear about such operation as division?
>
> try
> coin25 = change/25;
>
> > }
> > printf ("%i\n", coin25);
> >
> > }
> >
> > Thank you!
> >
>
> bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-09-03 18:20 +0100 |
| Message-ID | <gj%Fx.4216$Bw.963@fx17.am4> |
| In reply to | #69165 |
On 03/09/2015 18:08, Alla _ wrote: > On Thursday, September 3, 2015 at 8:09:44 PM UTC+4, bartekltg wrote: <snip> >> "int change = round(amount * 100);" >> >> do not mean a relation between this two variables. >> It mean 'take current value of amount, compute a function >> and the result put in variable "change"'. >> And the are independent. >> > I am not sure I understand what the difference is. > I intended exactly as you say: 'take current value > of amount,compute a function and the result put in > variable "change"'. > Then, within the loop, decrement 'change' with > each iteration of the loop. > Do you say that this is incorrect? It's about the order you do things in, as bartekltg says here: >> You have to compute value of change _after_ you >> put input into 'amount'. You're doing this: a) change = round(amount * 100); b) get amount from user You have to do it the other way round: a) get amount from user b) change = round(amount * 100); The order of operations is important. There is no magical relationship between change and amount. When you write: change = round(amount * 100); the current value of "amount" is multiplied by 100, and the result passed to the round() function, and the return value from round() is copied into "change". And that's IT - we're done. It won't happen *again* when the value of amount is altered in some way. So you have to get the value of "amount" right, and *then* you can assign it to "change". So do your scanf first (if you really must use scanf). Then, and NOT before, you can give a meaningful value to "change". It's like taking a photograph. You can point your camera at round(amount * 100), and "change" is the photograph that you get. But photographs don't automatically update themselves just because the thing they portray has been altered in some way - otherwise, all the snaps your parents took of you as a child would now look like you as an adult! (This would make life very difficult in some ways.) Just as your childhood photographs don't reflect your growth, so "change" doesn't reflect any new value that "amount" is given. So *first* set up the "amount" value using scanf or whatever, and *then* do the change = round(amount * 100) thing. -- 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 | bartekltg <bartekltg@gmail.com> |
|---|---|
| Date | 2015-09-03 19:32 +0200 |
| Message-ID | <msa07n$8it$1@node2.news.atman.pl> |
| In reply to | #69165 |
On 03.09.2015 19:08, Alla _ wrote:
> On Thursday, September 3, 2015 at 8:09:44 PM UTC+4, bartekltg wrote:
>> On 03.09.2015 17:54, 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**/
>>
>> Amount in uninitialized. Propabli equal to 0.0
>>
>>> int coin25 = 0;
>>> int change = round(amount * 100);
>>
>> 'Change' is computed based on uninitiated variable 'amount'.
>> It propably is equal to 0.
>>
>>>
>>> printf ("Please, enter your amount in $: \n");
>>>
>>> if (scanf("%f", &amount) == 1)
>>
>> You put value from input to 'amount', but 'change'
>> is still zero. Do you earlier write only in mathematica?
>> This is the same problem as in #triangle,
>>
>> "int change = round(amount * 100);"
>>
>> do not mean a relation between this two variables.
>> It mean 'take current value of amount, compute a function
>> and the result put in variable "change"'.
>> And the are independent.
>>
> I am not sure I understand what the difference is.
How?
Try to contemplate the difference between these two
main(){
int a=0;
int b=0;
b=a*a;
a=2;
printf("%d\n",b);
}
and
main(){
int a=0;
int b=0;
a=2;
b=a*a;
printf("%d\n",b);
}
The first returns 0, the second return 4.
> I intended exactly as you say: 'take current value
> of amount,compute a function and the result put in
> variable "change"'.
But the value of 'amount' was 0.
You computed the 'change' and _later_ ask user
about amount. This is wrong order.
It is like you first click 'send' button and then
write email.
Just change the order:
....
printf ("Please, enter your amount in $: \n");
if (scanf("%f", &amount) == 1)
{
int change = round(amount * 100); //***IMPORTANT PART***///
while (change >= 24)
{
change = change - 25;
coin25++;
}
}
....
> Then, within the loop, decrement 'change' with
> each iteration of the loop.
But change is 0. Always. Because you first computed
'change' based on 'amount', and then read input ant put
in ich 'amount'.
> Do you say that this is incorrect?
The main mistake is earlier.
This loop is only a _very_ ineffective algorithm
to compute integer division.
Put 21474830 as input. It computes it quite long time.
But not this is your problem.
bartekltg
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-09-03 12:18 -0700 |
| Message-ID | <lnsi6vqpkh.fsf@kst-u.example.com> |
| In reply to | #69152 |
bartekltg <bartekltg@gmail.com> writes:
> On 03.09.2015 17:54, Alla _ wrote:
[...]
>> int main(void)
>> {
>> float amount; /** I should ask for a float value in $,
>> and then convert it to amount in cents**/
>
> Amount in uninitialized. [Probably] equal to 0.0
[I corrected a typo in the quoted text.]
Don't assume that an uninitialized float object is *probably*
equal to 0.0. The value is completely arbitrary, and might not
even be a valid number.
(If an object is defined outside any function, or with the "static"
keyword", then it's implicitly initialized to zero.)
--
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]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | comp.lang.c
csiph-web