8. Probably because solving poblems by computer can generate such intense frusta
ID: 3533342 • Letter: 8
Question
8. Probably because solving poblems by computer can generate such intense frustation, computer science courses seem to generate more than their share of plagiarism. In several universities, the situation has gotten so bad computer science department have had to develop software to help detect cases of academic misconduct. The usual approach taken in such programs is to compare the structure of two programs, ignoring differences that are easy for students to chang, such as the names of variables and procedures. g
Consider, for example, the two program fragments shown below, each of which sums the elements in an integer array:
Student1.dat
int Total (int array[], int n)
{
int i, sum;
sum = 0;
for (i=0; i < n; i++){
sum +=array[i];
{
return(total);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Student2.dat
int Sum(int list[], int nl)
{
int j, total;
total= 0;
for (j=0; j < n; j++){
total +=list[i];
{
return (total);
}
The names of the functions and many of the variables are different, but the two programs are otherwise exactly the same. In code, samples, this short, it is entirely possible that the programs were created indepentendly, but one would start to get suspicious if the structural similarity went to page after page.
Write a program that uses two instances of the scannerADT to perform a line-byline comparison of two input files that "match" Two lines are defined as matching if their corresponding tokens match all the way across. Two tokens match if either of following is true:
The tokens are same string.
The tokens both begin with a letter.
For example, the tokens "sum" and total" match because both begin with a letter. In the student1.dat and student2.dat sample files, every line matches perfectly under this definition, so the program should report that 100 percent of the lines match
Explanation / Answer
#include #include #include #define MAX 10000 main( int argc, char *argv[] ) { FILE *fp1, *fp2, *fp3; void filecomp( FILE *, FILE * ); void fileconv( FILE *, FILE * ); if( argc != 4 ) { fprintf( stderr, "need two files " ); exit( 1 ); } fp3=fopen( argv[3], "w" ); fp2=fopen( argv[2], "r" ); if( fp2 == NULL ) { printf( "cannot open file2 " ); exit( 1 ); } if( fp3 == NULL ) { printf( "cannot open file3 " ); exit( 1 ); } fileconv( fp2, fp3 ); fclose( fp2 ); fclose( fp3 ); fp1=fopen( argv[1], "r" ); if( fp1 == NULL ) { printf( "cannot open file1 " ); exit( 1 ); } fp3=fopen( argv[3], "r" ); if( fp3 == NULL ) { printf("cannot open file3 "); exit( 1 ); } filecomp( fp1, fp3 ); fclose( fp1 ); fclose( fp3 ); exit( 0 ); } void fileconv( FILE *fp2, FILE *fp3 ) { char c; while( (c=fgetc(fp2)) != EOF ) { if( c == '.' ) { c='/'; fputc(c,fp3); } else fputc( c, fp3 ); } } void filecomp( FILE *fp1, FILE *fp3 ) { char line1[MAX], line2[MAX]; char *s1, *s2; int ctr, octr, a=0, b=1; int i,count1=0, count2=1, count3=0, count4=0; while( ((s1=fgets(line1,MAX,fp1)) != NULL) ) { count1++; while( (s2=fgets(line2,MAX,fp3)) != NULL ) { i=strcmp( s1, s2 ); if( i == 0 ) { count3++; } count2++; } if( count3==0 ) printf("line %d of file1 does not match lines of file3 ", count1 ); count2=1; count3=0; fseek( fp3, 0, SEEK_SET ); } if( (s2=fgets(line2,MAX,fp3)) != NULL ) printf( "both files have ended " ); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.