Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C Write tableDiff, a function that compares two arrays of intereger and retur

ID: 3819999 • Letter: I

Question

In C Write tableDiff, a function that compares two arrays of intereger and returns the subscripts of the first place they differe. If the arrays are the same, the function returns -1. Write it using pointers. The program should comments on (program description, function descriptions, all variable and constant declarations, ambiguous or complex sections of code and etc. use of local variables, local prototypes, and parameter passing

Have the entire program use pointers, rather than array subscripting.

Besides the table_diff function, also include a function to read in an array, and a function to print the elements of an array. Call this last function to print all of the elements of each of your arrays. All of these functions should be written using pointers, rather than array subscripts.

Use a sentinel value (e.g., -999) to indicate the end of valid input, rather than EOF, as a file cannot have two end-of-files.

The program should also test the condition and give out the error message like the statement below said:

Sets invoking appropriate error messages, e.g., too many values or invalid character data. The invalid characters and extra values will need to be flushed, else they will end up in the second array. Have the program continue as normal after flushing the bad data.

A style of how the program should look like

M=tablefll(a, max, sentinel);

N=table_fill(b,max, sentinel);

Tableprint(a, m);

Table_print(b,n);

Index=table_diff(a,b,m,n);

If(index==-1)

            Printf(“same”)

Else

            Printf(“array start to differ at index %i”, index);

Int table_diff(const int a[ ], cost b[], int m, int n

{

Cost int *ptra=a;

Const int *ptrb=b;

Const int*const endptr=a+m;

Const int*const endptrb=b+m;

While(ptra

{

Ptra++;

Ptrb++;

}

If (ptra==endptra&&ptrb==endptrb)

            Return -1;

Else

            Return ptra-a;

user input example:

3 4 5

3 4 5

answer=-1

3 4 5

3 4 6

answer=2

3 4 5

1 2 5

answer=0

1 2 3 4

1 2 3

answer=3

1 2 3

an empty list

answer=0

Explanation / Answer

#include<stdio.h>

int tablefill(int *a,int sentinel)
{int e=1;
   int c=0;
   while(e!=-sentinel)
   {
       scanf("%d",&e);
       if(e==sentinel)
       {
       break;
       }
       else
       {
           *a=e;
       a++;
       c++;
       }
   }
   return c;
}

int table_diff(const int *a, const int *b, int m, int n)
{
const int *ptra=a;
const int *ptrb=b;
if (m!=n)
{printf("in if diif ");
   if(m<n)
       return m;
   return n;
}
int i;
for ( i = 0; i < m; ++i)
{
   if(*ptra==*ptrb)
   {
      
   }
   else
   {

       return i;
   }
   ptra++;
   ptrb++;
  
}
return -1;
}
void Tableprint(int *a,int m)
{int i;
   printf("Array is ");
   for ( i = 0; i < m; ++i)
{
   printf("%d ",*a );
   a++;
}

}
int main(int argc, char const *argv[])
{int sentinel=-999;
   int a[100],b[100];
   int m=tablefill(a,sentinel);
int n=tablefill(b,sentinel);
Tableprint(a, m);
Tableprint(b,n);
int index=table_diff(a,b,m,n);
if(index==-1)
printf("same");
else
printf("array start to differ at index %d",index);
   return 0;
}

========================================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ gcc tablecomp.c
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1 2 3 -999
1 2 3 -999
Array is
1
2
3
Array is
1
2
3

same