In C Write tableDiff, a function that compares two arrays of intereger and retur
ID: 3817199 • 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;
Explanation / Answer
#include<stdio.h>
//function to read array of values
int table_fill(int A[],int m,int s)
{
int *a=A;
int i,n=0;//variable declarations
//prompting input
printf("Enter integer values :(Max values to be entered is %d)(-999 to stop)");
i=0;
while(i<m && n!=s)//loop runs upto max value or sentinal value
{
scanf("%d",&n);//reading number
if(n==-999)break;
a[i]=n;//assigning to array
i++;//incrementing
}
return i;
}
//function to print array
void Table_print(int A[],int m)
{
int *a=A;
int i=0;
//printing values
while(i<m)
{
printf("%d ",*(a+i));
i++;
}
printf(" ");
}
//function returns the index where first different element found
int Table_diff(int A[],int B[],int m,int n)
{
int *a=A,*b=B;//arrays assigning to pointers
int index=-1;//variable to return index
int i=0;//variable to iterate the array..
int *k;//to store least length
if(m>n)//finding the least length among two arrays..
{
k=&n;//if n is lesser
}
else if(n>m)
{
k=&m;//if m is lesser
}
else
{
k=&m;//if both are equal
}
for(i=0;i<*k;i++)
{
if(*(a+i)!=*(b+i))
{
index=i;//finding the index where both arrays are different
return index;
}
}
if(m==n)
{
return index;//returning the index value
}
else
{
return *k;//returning the min length
}
}
int main()
{
//variable declarations
int max=10;//max size of the array...
int sentinel=-999;//sentinel value to stop reading
//integer arrays
int a[10];//first array
int b[10];//second array
int m = table_fill(a,max,sentinel);//calling table fill function
int n = table_fill(b,max,sentinel);
printf("Array1 :");//printing array
Table_print(a,m);
printf("Array1 :");//printing array
Table_print(b,n);
int index=0;
index = Table_diff(a,b,m,n);//finding the difference index
//printing output
if(index==-1)
{
printf("same ");
}
else
{
printf("array start to differ at index %i", index);
}
return 0;
}
output:-
Enter integer values :(Max values to be entered is 10)(-999 to stop)1
2
3
-999
Enter integer values :(Max values to be entered is 10)(-999 to stop)1
2
3
-99
-999
Array1 :1 2 3
Array1 :1 2 3 -99
array start to differ at index 3
Process exited normally.
Press any key to continue . . .
Enter integer values :(Max values to be entered is 10)(-999 to stop)1
2
3
-999
Enter integer values :(Max values to be entered is 10)(-999 to stop)1
2
3
-999
Array1 :1 2 3
Array1 :1 2 3
same
Process exited normally.
Press any key to continue . . .