In C . DO NOT USE BREAK. Write tableDiff, a function that compares two arrays of
ID: 3820486 • Letter: I
Question
In C . DO NOT USE BREAK. 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.
Have your 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.
For the function to print us the code style below
Double *ptr=a
Double*endptr=a+num;
While(ptr
Print(“%f ”, *ptr++);
For the Function to read use the code below style
Int r;
Double value
While((r=scanf(“%lf”, &value))==1 && ptr
*ptr++==value:
If(r==1)
Printf(“entered more than %i values”, max);
Else if (r !=EOF)
Printf(“invalid character data entered”)
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.
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;
}
Run your program using the following sets of input:
1. 3 4 5
3 4 5
2. 3 4 5
3 4 6
3. 3 4 5
1 2 5
4. 1 2 3 4
1 2 3
5. 1 2 3
an empty list
6. 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.
As before, your grade will be based on the correctness of your output, as well as the proper use of the following:
1. meaningful variable names
2. indentation
3. blank lines and spacing
4. comments on the following:
- program description
- function descriptions
- all variable and constant declarations
- ambiguous or complex sections of code
5. the correct use of local variables, local prototypes, and parameter passing
6. format and appearance of output
7. structured code
Explanation / Answer
Answer:
#include<stdio.h> /* Header file for standard input and standard output (printf(), scanf()) functions */
#include<stdlib.h>/* Header file for exit(0) */
void readarray(int p[],int k) /* readarray() is used to take the elements into array, p[] is array and k is array size */
{
int i;
for(i=0;i<k;i++) /* loop iterates for k times */
{
scanf("%d",&p[i]); /* Takes the elements of the array one-by-one and repeated for k times */
}
}
int table_diff(int a[],int b[],int m) /* function definition with two integer arrays as arguments and one integer variable */
{
int *p,*q,i; /* p and q are pointer variables */
p=a; /* address of first array a is stored in pointer variable p */
q=b; /* address of second array b is stored in pointer variable q */
for(i=0;*p==*q&&i<m;i++) /* if the content at address p (first array's first element address) and address q (second array's first element address) are same and index i is less than m (total elements) */
{
p++; /* icrements the address to its next location */
q++; /* icrements the address to its next location */
}
if(i==m) /* if the index i reaches m, then all the elements of both the arrays same and returns -1 */
return -1;
else /* if any mismatch occurs in the array elements of both the arrays then it returns the index value i where it gets mismatch */
return i;
}
int main()
{
int a[20],b[20],m,n,i; /* Variable Declaration */
printf("Enter the number of elements of array a:");
scanf("%d",&m); /* Taking the size of first array */
printf("Enter the number of elements of array b:");
scanf("%d",&n); /* Taking the size of second array */
if(m!=n) /* Checking whether the sizes of two arrays are same or not */
{
printf("The arrays are not same in size");
exit(0); /* exited from the program */
}
else
{
printf("Enter the elements of array a:");
readarray(a,m); /* function call to readarray() with the arguments a (array name) and m (size of the array) */
printf("Enter the elements of array b:");
readarray(b,n); /* function call to readarray() with the arguments b (array name) and n (size of the array) */
i=table_diff(a,b,m); /* function call to table_diff() with the arguments a (array name), b (array name), and m (size of the array) */
if(i==-1) /* if the return value of table_diff() is -1 then it prints the following message */
printf("The two arrays are same");
else /* if the return value of table_diff() is not -1 (if it is i) then it prints the following message */
printf("The two arrays are not same, and the difference is found at %d index",i);
}
return 0;
}