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

In C: The code below has int main() in the bottom of the code, how to move it to

ID: 3821641 • Letter: I

Question

In C: The code below has int main() in the bottom of the code, how to move it to the top?

include

int table_fill(int A[],int m,int sentinel)   //function to read array of values
{
int *a=A;
int i;
int n=0;   //variable declarations

printf("Enter integer values :(Max values to be entered is %d)(-999 to stop)");   //prompting input
i=0;
while(i {
scanf("%d",&n);           //reading number
if(n==-999)break;
a[i]=n;                   //assigning to array
i++;                   //incrementing
}
  
return i;
}

void Table_print(int a[],int m)       //function to print array
{
   int *ptr = a;
   int *endptr = a + m;
   while( ptr < endptr)
   {
       printf("%d ",*ptr++);               //printing values
  
   }
   printf(" ");                       //printing new line  
  
}
  
int table_diff(const int a [], const int b[], int m, int n) //function returns the index where first different element found
{
   const int *ptra=a;
   const int *ptrb=b;
   const int * const endptra=a+m;
   const int * const endptrb=b+m;
   while(ptra < endptra &&ptrb        {
       ptra++;
       ptrb++;
       }
   if(ptra==endptra&&ptrb==endptrb)
   return-1;                       //return -1 if both array are the same              
   else
   return ptra-a;
}

int main()
{

int max=10;               //max size of the array...
int sentinel=-999;       //sentinel value to stop reading
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);       //calling table fill function
  
//printf("Array1 :");                       //printing array 1
Table_print(a,m);                              
  
//printf("Array2 :");                       //printing array 2
Table_print(b,n);
  
int index=0;
index = table_diff( a , b, m, n);       //finding the difference index
  

if(index==-1)
{
printf("same ");                       //printing output for same
}
else
{
printf("array start to differ at index %i", index);       //printing output for different
  
}
  
return 0;
}

The second question is how to change the format of table_file above to look like the code below:(DO NOT USE BREAK)

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”)

Explanation / Answer

#include<stdio.h>
//adding protypes....to move the main to top
int table_fill(int A[],int m,int sentinel) ;
void Table_print(int a[],int m);
int table_diff(const int a [], const int b[], int m, int n);

int main()
{

int max=10;               //max size of the array...
int sentinel=-999;       //sentinel value to stop reading
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);       //calling table fill function

//printf("Array1 :");                       //printing array 1
Table_print(a,m);                            

//printf("Array2 :");                       //printing array 2
Table_print(b,n);

int index=0;
index = table_diff( a , b, m, n);       //finding the difference index

if(index==-1)
{
printf("same ");                       //printing output for same
}
else
{
printf("array start to differ at index %i", index);       //printing output for different

}

return 0;
}

int table_fill(int A[],int m,int sentinel)   //function to read array of values
{
int *a=A;
int i;
int n=0;   //variable declarations

printf("Enter integer values :(Max values to be entered is %d)(-999 to stop)");   //prompting input
i=0;
while(i>=0) {
scanf("%d",&n);           //reading number
if(n==-999)break;
a[i]=n;                   //assigning to array
i++;                   //incrementing
}

return i;
}

void Table_print(int a[],int m)       //function to print array
{
   int *ptr = a;
   int *endptr = a + m;
   while( ptr < endptr)
   {
       printf("%d ",*ptr++);               //printing values

   }
   printf(" ");                       //printing new line

}

int table_diff(const int a [], const int b[], int m, int n) //function returns the index where first different element found
{
  
   const int *ptra=a;
   const int *ptrb=b;
   const int * const endptra=a+m;
   const int * const endptrb=b+m;
   while(ptra < endptra &&ptrb )       {
       ptra++;
       ptrb++;
       }
   if(ptra==endptra&&ptrb==endptrb)
   return-1;                       //return -1 if both array are the same            
   else
   return ptra-a;
   return 0;
}