Mega Search
23.2 Million


Sign Up

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

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


Vote for best question.
Score: 0  # Vote:  0
Date Posted: 9-Sep-2003, at 9:41 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?
> 
> /*
> 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;

I'm no expert, but I do notice one thing.  You've declared check as type
int, but have not assigned it anything (like 0) before adding (ch%7) to it. 
So ch%7 gets added to some arbitrary, unknown number.  At least, from what
I can tell.  FWIW, fixing that doesn't make the progam work correctly, but
it's too late for me to try and figure it out :).



-trey



Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Sep-2003, at 11:13 PM EST
From: trey
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
Bill Reed  wrote in message
news:t93tlvcq9htcacrd9vq3uacqd8j9mbj1m2@4ax.com...
> 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.
> */

Very clever, but so simple (I mean the idea, not necessarily understanding
the problem description). I'm not sure I would have thought of this method
if confronted with a huge number. This seems more like a maths problem than
a C problem.

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

Since it doesn't really look like a C-language problem, I'm inclined to
explain what to do, but we aren't supposed to do your homework for you (even
in maths). Think about what you need to do to keep a running remainder as
each digit comes in.

Another hint:
Suppose the first two digits are 87. The remainder of 8 divided by 7 is 1.
The remainder of 87 divided by 7 is 3. So, what do you need to do with the 1
after you read in the '7' so that you'll end up with a remainder of 3?

>     digits++;
>   }
>   //printf("%c", check);
>   if (check == digits-1)
>     printf("VALID\n");
>
>   return 0;
> }
>

DW




Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 1:22 PM EST
From: David White
 
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?
> 
> /* 
> 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.


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 9-Sep-2003, at 7:43 PM EST
From: James Connell
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
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


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 4:05 AM EST
From: Bill Reed
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
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:
> 
> 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.

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.

Second, I probably _would_ read the digits all at once, and split them 
apart later -- I'm reasonably certain it really makes the job simpler, 
with one minor proviso.

The proviso is that we assume the combined number isn't TOO lengthy -- 
specifically, that it will fit into some type we have available.  Even 
if we read each digit separately, it really only gains us one digit; if 
the number gets a lot longer than the one you've give above, we're still 
left without a type guaranteed to hold the number, which makes life more 
difficult pretty quickly.

Assuming the full set of digits will always fit into a long, we can use 
code like this:

	char *names[2] = { "Invalid", "Valid" };

	long digits, ticket_number, check_digit;

	scanf("%ld", &digits);

	ticket_number = digits / 10;
	check_digit = digits % 10;

	printf("%s\n", names[ticket_number % 7 == check_digit]);

If we know we have long long available, that gives much greater range 
quite easily.

Lacking that, it's possible to use floating point types to hold integers 
up to some particular size, typically 53 bits for a double (float.h has 
the specifics, if needed).  In this case, getting the remainder isn't as 
straightforward, but it's not terrible either (hint: multiplying the 
fractional part by 7 isn't a particularly good way to recover it).

-- 
    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:13 AM EST
From: Jerry Coffin
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
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:
> >
> > 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.
>
> 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.

> Second, I probably _would_ read the digits all at once, and split them
> apart later -- I'm reasonably certain it really makes the job simpler,
> with one minor proviso.

I'm sure it's simpler to do it one at a time.

> The proviso is that we assume the combined number isn't TOO lengthy --

It can be a hundred million digits - no problem.

> specifically, that it will fit into some type we have available.  Even
> if we read each digit separately, it really only gains us one digit; if
> the number gets a lot longer than the one you've give above, we're still
> left without a type guaranteed to hold the number, which makes life more
> difficult pretty quickly.

Right, so we do it one digit at a time and throw the digit away. Ignoring
the check for the last digit, only one variable, capable of holding a value
up to 6, needs to be maintained from one character to the next.

[snip]

DW




Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 3:20 PM EST
From: David White
 
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:
> 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.

You don't need an array.

DW




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

[ ... ]

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

What exactly do you mean by "done right"?  Are you talking about doing 
basic long division on the stored digits?  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.

If you want to work with large integers, you'd be _much_ better off 
looking up one of the (many) bignum libraries around -- a decently 
written one will do the division in (at least) 32-bit chunks, so it'll 
be on the order of 8 times as fast as your idea of doing it on a digit-
wise basis.

[ ... ]

> It can be a hundred million digits - no problem.

What exact sort of drugs are you on (or is it that you've just quit 
taking the drugs necessary to control your hallucinations)?

Someday when you're back on contact with reality, look at the size of an 
airline ticket, and try to make a more accurate estimate of how many 
digits it'll hold.

For that matter, consider another bit of practicality: assume that 
worldwide it's possible to print airline tickets at a rate of a million 
per second.  Try to figure up how long it would take to issue enough 
airline tickets to use up all the 64-bit numbers (hint: if you come up 
with an answer under half a million years, you're wrong!)

The fact is, there is absolutely no point in this application having 
hundreds of millions of digits, or even hundreds of digits.  Planning 
for such a thing is simply utterly pointless and stupid, even at best.

-- 
    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 6:10 AM EST
From: Jerry Coffin
 
Re: [C] Reading and arithmetic with getchar()  
News Group: alt.comp.lang.learn.c-c++
Jerry Coffin  wrote in message
news:MPG.19c86e6bc801698a9896ab@news.west.earthlink.net...
> In article ,
> no@email.provided says...
>
> [ ... ]
>
> > > 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.
>
> What exactly do you mean by "done right"?  Are you talking about doing
> basic long division on the stored digits?

You don't need to store any digits.

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

> If you want to work with large integers, you'd be _much_ better off
> looking up one of the (many) bignum libraries around -- a decently
> written one will do the division in (at least) 32-bit chunks, so it'll
> be on the order of 8 times as fast as your idea of doing it on a digit-
> wise basis.

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.

> [ ... ]
>
> > It can be a hundred million digits - no problem.
>
> What exact sort of drugs are you on (or is it that you've just quit
> taking the drugs necessary to control your hallucinations)?
>
> Someday when you're back on contact with reality, look at the size of an
> airline ticket, and try to make a more accurate estimate of how many
> digits it'll hold.
>
> For that matter, consider another bit of practicality: assume that
> worldwide it's possible to print airline tickets at a rate of a million
> per second.  Try to figure up how long it would take to issue enough
> airline tickets to use up all the 64-bit numbers (hint: if you come up
> with an answer under half a million years, you're wrong!)
>
> The fact is, there is absolutely no point in this application having
> hundreds of millions of digits, or even hundreds of digits.  Planning
> for such a thing is simply utterly pointless and stupid, even at best.

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

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

DW




Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 10-Sep-2003, at 5:08 PM EST
From: David White