Is the checksum being computed according to the requirement ?

  • Follow


This is not a homework problem.

So the problem requires me to compute a checksum in a signed char variable =
that is initialized to -1. As each character is read from standard input, i=
t is added to the checksum. Any overflow from the checksum variable is igno=
red. When all of the characters have been written, the checksum is then wri=
tten as a decimal integer, which may be negative. Follow checksum with a ne=
w-line.

For example,
Hello world!
102

Here is the code


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

int main(){
 =20
  int ch;
  int at_beginning =3D 1;
  int line =3D 0;
  signed char checksum =3D -1;

  while( (ch=3Dgetchar())!=3D EOF){
   =20
    if(at_beginning =3D=3D 1){=20
    =20
      at_beginning =3D 0;
      line+=3D1;
      printf("%d Output: ", line);
     =20
    }
   =20
    checksum  =3D checksum + ch;
    putchar(ch);

    if(ch =3D=3D '\n'){   =20
      at_beginning =3D 1;
      printf("Checksum: %d\n", checksum);
      checksum =3D -1;
    }
  }

  return EXIT_SUCCESS;
}
0
Reply John 1/31/2011 1:50:22 AM

On 01/31/11 02:50 PM, John wrote:
> This is not a homework problem.
>
> So the problem requires me to compute a checksum in a signed char variable that is initialized to -1. As each character is read from standard input, it is added to the checksum. Any overflow from the checksum variable is ignored. When all of the characters have been written, the checksum is then written as a decimal integer, which may be negative. Follow checksum with a new-line.
>
> For example,
> Hello world!
> 102

Why don't you write some test cases?

-- 
Ian Collins
0
Reply Ian 1/31/2011 1:59:05 AM


On 1/30/2011 6:50 PM, John wrote:
> This is not a homework problem.
>
> So the problem requires me to compute a checksum in a signed char variable that is initialized to -1. As each character is read from standard input, it is added to the checksum. Any overflow from the checksum variable is ignored. When all of the characters have been written, the checksum is then written as a decimal integer, which may be negative. Follow checksum with a new-line.

Since an overflow of a signed integer type results in behavior and possible 
value not defined by C, you need to replace "ignore overflow" with si\omething 
specific.

-- 
Thad
0
Reply Thad 2/4/2011 2:25:10 PM

John wrote:
> This is not a homework problem.
> 
> So the problem requires me to compute a checksum in a signed char variable that is initialized to -1. As each character is read...it is added to the checksum. Any overflow from the checksum variable is ignored.

I think that an addition expression involving 'signed char' can overflow 
if the addition exceeds 'SCHAR_MAX'.  I'm not sure about a standard way 
to tell the compiler "ignore overflow for a 'signed char' addition."

If you are using getchar(), I think that's like using getc(stdin), which 
I think is fairly similar to using fgetc(stdin).  Since fgetc() gets an 
'unsigned char' and then converts it to an 'int' and returns that, 
perhaps the problem you are dealing with actually includes:

"Compute a checksum using an 'unsigned char' object where that object is 
initialized to all-bits-set (all one bits for the value bits), which 
happens to be the two's complement representation for -1."

I think that an addition expression involving 'unsigned char' cannot 
overflow, so your "ignored" criteria would be satisifed by using that 
type, instead.

> 
> For example,
> Hello world!
> 102
> 
> Here is the code
> 
> 
> #include<stdio.h>
> #include<stdlib.h>
> 
> int main(){

Or maybe:

   int main(void)

>   
>   int ch;
>   int at_beginning = 1;
>   int line = 0;
>   signed char checksum = -1;
> 
>   while( (ch=getchar())!= EOF){
>     
>     if(at_beginning == 1){ 
>      
>       at_beginning = 0;
>       line+=1;
>       printf("%d Output: ", line);
>       
>     }
>     
>     checksum  = checksum + ch;

The above could overflow, I think.  Perhaps if you'd instead declared:

   unsigned char ch;
   unsigned char checksum = UCHAR_MAX;

Then that might work for you, as 'checksum' would be all-bits-set (well, 
value bits), and further assuming that the problem really wants that 
when the problem statement includes mention of "-1".

Hope this helps! :)
0
Reply sha0.miller (876) 5/8/2011 4:37:16 AM

On 5/8/2011 11:26 PM, pete wrote:
> Shao Miller wrote:
>
>> I think that an addition expression involving 'unsigned char' cannot
>> overflow, so your "ignored" criteria would be satisifed by using that
>> type, instead.
>
> I've never heard of a case where INT_MAX equals UCHAR_MAX,
> but it is allowed,
> and if INT_MAX does equal UCHAR_MAX,
> then (UCHAR_MAX + 1) is undefined.
>

Yikes; oh no! :)  That'd be at least CHAR_BIT - 1 bits of padding for an 
'int'!

What about:

   checksum = (unsigned long int)checksum + (unsigned long int)ch;

?  Would that be portable in a standard sense?

Thanks. :)
0
Reply sha0.miller (876) 5/8/2011 6:50:05 AM

Shao Miller wrote:
 
> I think that an addition expression involving 'unsigned char' cannot
> overflow, so your "ignored" criteria would be satisifed by using that
> type, instead.

I've never heard of a case where INT_MAX equals UCHAR_MAX,
but it is allowed,
and if INT_MAX does equal UCHAR_MAX,
then (UCHAR_MAX + 1) is undefined.

-- 
pete
0
Reply pfiland (6613) 5/9/2011 4:26:01 AM

Shao Miller wrote:
> 
> On 5/8/2011 11:26 PM, pete wrote:
> > Shao Miller wrote:
> >
> >> I think that an addition expression involving
> >> 'unsigned char' cannot
> >> overflow, so your "ignored" criteria
> >> would be satisifed by using that type, instead.
> >
> > I've never heard of a case where INT_MAX equals UCHAR_MAX,
> > but it is allowed,
> > and if INT_MAX does equal UCHAR_MAX,
> > then (UCHAR_MAX + 1) is undefined.
> >
> 
> Yikes; oh no! :)  That'd be at least
> CHAR_BIT - 1 bits of padding for an 'int'!
> 
> What about:
> 
>    checksum = (unsigned long int)checksum + (unsigned long int)ch;
> 
> ?  Would that be portable in a standard sense?

Yes.
With both checksum and ch being unsigned char,
you could get away with only using either of the two casts.

-- 
pete
0
Reply pfiland (6613) 5/9/2011 12:14:26 PM

Shao Miller <sha0.miller@gmail.com> writes:

> John wrote:
>> This is not a homework problem.
>>
>> So the problem requires me to compute a checksum in a signed char variable that is initialized to -1. As each character is read...it is added to the checksum. Any overflow from the checksum variable is ignored.
>
> I think that an addition expression involving 'signed char' can
> overflow if the addition exceeds 'SCHAR_MAX'.  I'm not sure about a
> standard way to tell the compiler "ignore overflow for a 'signed char'
> addition."

That's not quite correct -- at least it glosses of over some important
details.  Technically, an addition expression involving signed char
can't overflow because it can't happen -- the operands will be promoted
in to int.  Of course, in the slightly odd (but not at all unheard of)
situation where char and int are the same size, the effect is as you
describe, only using INT_MAX rather than SCHAR_MAX.

However, in the much more common situation where char is much smaller
than int, the addition can not overflow.  What goes wrong is the
conversion of the result back to signed char.  This is not undefined
behaviour even when the value is too large.  It's very close to UB
(from a portability point of vew) but it is implementation defined.

> If you are using getchar(), I think that's like using getc(stdin),
> which I think is fairly similar to using fgetc(stdin).  Since fgetc()
> gets an 'unsigned char' and then converts it to an 'int' and returns
> that, perhaps the problem you are dealing with actually includes:
>
> "Compute a checksum using an 'unsigned char' object where that object
> is initialized to all-bits-set (all one bits for the value bits),
> which happens to be the two's complement representation for -1."
>
> I think that an addition expression involving 'unsigned char' cannot
> overflow, so your "ignored" criteria would be satisifed by using that
> type, instead.

Yes, but only for the technical reason that the operands will promote,
so there is no actual addition using unsigned char.  unsigned char will
most often promote to int, and this can overflow in some outlandish
cases (imagine 16 bit ints and 15 bit chars).  The most portable
solution is to convert (with a cast) the unsigned chars to unsigned int.
The arithmetic then won't overflow (let's ignore the terrible corner
case where unsigned int promotes to int!) and the conversion back to
unsigned char is well-defined.

<snip>
-- 
Ben.
0
Reply ben.usenet (6515) 5/9/2011 12:55:40 PM

7 Replies
151 Views

(page loaded in 0.322 seconds)

Similiar Articles:













7/9/2012 1:17:46 PM


Reply: