J
John
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
Here is the code
#include<stdio.h>
#include<stdlib.h>
int main(){
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;
putchar(ch);
if(ch == '\n'){
at_beginning = 1;
printf("Checksum: %d\n", checksum);
checksum = -1;
}
}
return EXIT_SUCCESS;
}
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
Here is the code
#include<stdio.h>
#include<stdlib.h>
int main(){
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;
putchar(ch);
if(ch == '\n'){
at_beginning = 1;
printf("Checksum: %d\n", checksum);
checksum = -1;
}
}
return EXIT_SUCCESS;
}