Mega Search
23.2 Million


Sign Up

Make a donation  
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++

"Bill Reed"  wrote in message
news:nh8tlv8ld3797698h5d5tkmcclbifr5v55@4ax.com...
> On Tue, 09 Sep 2003 19:43:56 -0800, James Connell 
> wrote:
>
> >Bill Reed wrote:
> >
> >> I am totally confused on this one. I've reviewed the material that
> >> pertains to this exercise a number of times. I don't even know where
> >> to start. The code below is basically my pathetic thoughts as
> >> expressed through the keyboard. If anyone can get me back on my feet I
> >> would be very grateful. Oh, BTW, does anyone have a kleenex?
> >>
> >> /*
> >> Airline tickets are assigned lengthy identifying numbers, such as
> >> 47715497443. To be valid, the last digit of the number must match
> >> the remainder when the other digits--as a group--are divided by
> >> 7. (For example, 4771549744 divided by 7 yields a remainder of 3.)
> >> Write a program that checks whether or not an airline ticket
> >> number is valid:
> >>
> >> Enter ticket number: 47715497443
> >> VALID
> >>
> >> Hint: Don't attempt to read the number in a single step. Instead,
> >> use getchar to obtain its digits one by one. Carry out the
> >> division on its digits one at a time, being careful not to include
> >> the last digit in the division.
> >> */
> >>
> >> #include 
> >>
> >> int main(void)
> >> {
> >>   char ch;
> >>   int digits = 0;
> >>   int check;
> >>
> >>   printf("Enter ticket number: ");
> >>   while ((ch = getchar()) != '\n') {
> >>   //printf("%c", ch);
> >>     check += ch % 7;
> >>     digits++;
> >>   }
> >>   //printf("%c", check);
> >>   if (check == digits-1)
> >>     printf("VALID\n");
> >>
> >>   return 0;
> >> }
> >>
> >> Thanks,
> >> Bill
> >>
> >
> >well that won't work - for a couple of reasons
> >1: getchar reads in a character not a number - "1" is not (nessesarily)
> >the same as 1.
> >2: you must divide the whole number (minus the last one) at the same
> >time - 12342 (1%7)+(2%7)+(3%7)+(4%7) does not = 2 , 1234 % 7 == 2.
> >
> >read the chars into an array one at a time saveing the last one read
> >till next is checked for '\n'. when you get a '\n' convert the last true
> >char read to an int and save it, then convert the array to a number.
> >
> >there are a couple of pit falls - the lenght of an int is one.
> >good luck.
>
> Thanks to everyone. I did think of an array when I first read the
> exercise but the author hasn't covered arrays yet (next chapter) so
> I'm assuming he knows there's another way to do the exercise. Thanks
> for your feedback--I don't know any programmers personally so it helps
> to be able to ask questions here.

Bill, consider some better practices. Firstly, you need to be able to store
and compute with the number entered. If it is very large you may need to use
bigger than an int, and I'd suggest making it unsigned as well (you won't
have negative ticket numbers).
Consider checking the entire number read in against some maximum so as not
to try using data that won't fit your variable. (Of course, using long long
will really help!)
Consider using  input_number/10 and input_number%10 to separate off the
start of the number and the last digit from the initial input number. The
rest of the work becomes trivial.
You don't need array.
-- 
Gary



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 9:39 AM EST
From: Gary Labowitz
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
"David White"  wrote in message
news:mxy7b.2728$d6.136244@nasal.pacific.net.au...
> Jerry Coffin  wrote in message
> news:MPG.19c852f3e1f520949896a5@news.west.earthlink.net...
> > In article , nomail@no.way
> > says...
> >
> > [ ... ]
> >
> > > Airline tickets are assigned lengthy identifying numbers, such as
> > > 47715497443. To be valid, the last digit of the number must match
> > > the remainder when the other digits--as a group--are divided by
> > > 7. (For example, 4771549744 divided by 7 yields a remainder of 3.)
> > > Write a program that checks whether or not an airline ticket
> > > number is valid:
<>
> > IMO, this hint is highly suspect at best. First of all, it directly
> > contradicts the requirements given above.  The requirement says to do
> > the division on the digits grouped together as a number, but the hint
> > says to do the division one digit at a time -- the two will NOT give the
> > same results.
>
> They will if done right.
>
Explain, please. If the modulo were 9 then the digits would calculate
correctly one at a time.
For 7 to be used it would require the number to in octal. Hmmm... would you
consider it "right" to convert the decimal number to octal, do the one digit
at a time modulo, and then check the digit?
The statement of the problem doesn't say what to do if the check digit is 8
or 9; presumable that can't happen!
-- 
Gary



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 9:43 AM EST
From: Gary Labowitz
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
"David White"  wrote in message
news:M6A7b.2733$d6.136359@nasal.pacific.net.au...
>
> #include 
>
> int main()
> {
>     int k = 0;
>     int c = 0;
>     while((c = getchar()) != '\n')
>     {
>         k = (k*10 + (c - '0')) % 7;
>     }
>     printf("\nRemainder=%d\n", k);
>     return 0;
> }

You got it.  I get it.
-- 
Gary



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 9:49 AM EST
From: Gary Labowitz
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
On Wed, 10 Sep 2003 17:08:40 +1000, "David White" 
wrote:

>The following test program finds the remainder of an integer of any length
>divided by 7. For simplicity it does not check for non-digit characters or
>for the last digit (as required by the exercise):
>
>#include 
>
>int main()
>{
>    int k = 0;
>    int c = 0;
>    while((c = getchar()) != '\n')
>    {
>        k = (k*10 + (c - '0')) % 7;
>    }
>    printf("\nRemainder=%d\n", k);
>    return 0;
>}

I would never have come up with the expression in the while loop. And
after an hour I am giving up on discarding the last digit. I won't
figure this out either so I'll come back to it in the future. I'm
somewhat frustrated in that I can't find the solution in the text. No
reflection on the author, I'm sure it's me.

Thanks,
Bill


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 2:10 PM EST
From: Bill Reed
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
Bill Reed wrote:

> I am totally confused on this one. I've reviewed the material that
> pertains to this exercise a number of times. I don't even know where
> to start. The code below is basically my pathetic thoughts as
> expressed through the keyboard. If anyone can get me back on my feet I
> would be very grateful. Oh, BTW, does anyone have a kleenex?

Here you go.

#include 

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'gbzmlpu@cbobk.pbz' | rot13

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 3:06 PM EST
From: Tom Zych
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
Bill Reed wrote:

> I would never have come up with the expression in the while loop. And
> after an hour I am giving up on discarding the last digit. I won't
> figure this out either so I'll come back to it in the future. I'm
> somewhat frustrated in that I can't find the solution in the text. No
> reflection on the author, I'm sure it's me.

Off the top of my head, I'd say that instead of immediately
computing the remainder mod 7 of all digits read so far, we should
instead save the current digit and compute the remainder of all
previous digits. Then when you hit the newline, compare your running
result to the last digit. Something like:

mod7 = 0
prev = 0
while ((curr = read) != \n)
    curr -= '0' // convert from char to value
    mod7 = (mod7 * 10 + prev) % 7
    prev = curr
check mod7 vs prev

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'gbzmlpu@cbobk.pbz' | rot13

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 3:16 PM EST
From: Tom Zych
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
On Wed, 10 Sep 2003 15:06:58 GMT, Tom Zych  wrote:

>Bill Reed wrote:
>
>> I am totally confused on this one. I've reviewed the material that
>> pertains to this exercise a number of times. I don't even know where
>> to start. The code below is basically my pathetic thoughts as
>> expressed through the keyboard. If anyone can get me back on my feet I
>> would be very grateful. Oh, BTW, does anyone have a kleenex?
>
>Here you go.
>
>#include 

In my condition you want me to go someplace else to find a kleenex?

#include "kleenex.h"

Thanks, :)
Bill


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 10:20 AM EST
From: Bill Reed
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
Bill Reed wrote:

> >#include 
> 
> In my condition you want me to go someplace else to find a kleenex?
> 
> #include "kleenex.h"

Two problems with this:
1. It only works if you already have a kleenex. <> sends you to a
(reasonably accessible) place that should have one.
2. kleenex.h isn't a kleenex, just a description of what a kleenex
does and how to use it. "kleenex" is the actual kleenex.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'gbzmlpu@cbobk.pbz' | rot13

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 3:40 PM EST
From: Tom Zych
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
Tom Zych  wrote:

>Bill Reed wrote:
>
>> >#include 
>> 
>> In my condition you want me to go someplace else to find a kleenex?
>> 
>> #include "kleenex.h"
>
>Two problems with this:
>1. It only works if you already have a kleenex. <> sends you to a
>(reasonably accessible) place that should have one.
>2. kleenex.h isn't a kleenex, just a description of what a kleenex
>does and how to use it. "kleenex" is the actual kleenex.

*LOL* :D

Irrwahn
--
What does this red button do?

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 6:15 PM EST
From: Irrwahn Grausewitz
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
In article , 
no@email.provided says...

[ ... ]

> You don't need to store any digits.

Despite your cleverness, you've yet to show this.
 
> > If so, it can be made to
> > work, but this is about as _wrong_ of a way as is humanly possible --
> > specifically, this is approximately the _slowest_ method known to man
> > for carrying out this particular operation.
> 
> It's a simple exercise that gave constraints on how to solve the problem.
> Execution time is irrelevant.

Says who?

> It's an exericse - a problem for a student to solve. A bignum library would
> be completely inappropriate. A solution that satisfies the problem
> description is ridiculously easy.

Apparently it's not quite so easy as you imply, since your posted 
"solution" isn't even close to solving the problem.

[ ... ]
 
> That's four paragraphs now on a throwaway line I mentioned only as
> indication of what the algorithm can handle if necessary.

You're running into the same basic problem many people do when they 
start to learn a few cute algorithms: they want to apply those 
algorithms everywhere, whether they're applicable or not.

> The following test program finds the remainder of an integer of any length
> divided by 7. For simplicity it does not check for non-digit characters or
> for the last digit (as required by the exercise):

So far, you haven't solved the original problem at all.  Despite this, 
your code is longer and more complex than mine, which did solve the 
original problem.  By the time you modify your's so it does solve the 
real problem, it's likely to be at least half again as big, and maybe 
twice as big.

Question: does it provide any useful capabilities to compensate for its 
extra complexity?

Answer: NO!

Your original statement was wrong: it's not easier to process the input 
one digit at a time.  While you've shown a way it COULD be done, the way 
you've shown clearly leads to MORE complex code.

Bottom line: the algorithm you advocate is clever and I congratulate you 
for it.  Unfortunately, for the job at hand it leads to complexity 
without utility.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 4:21 PM EST
From: Jerry Coffin