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

Who can help me withthis program... please... This algorithm will find the great

ID: 3618793 • Letter: W

Question

Who can help me withthis program... please...

This algorithm will find the greatest common divisor (gcd) of twopositive integers by repeatedly dividing two numbers, generating aquotient and remainder. The algorithm goes as follows:

1. Initially get the two numbers from the input file to determinethe GCD of, call them
num1 and num2.
2. Declare another two variables bigand small and make big the larger of the two
values and small, the smaller of thetwo.
3. Divide big by small to get the remainder r.
4. If r = 0, then the required GCD isequal to small, and we are done.
5. Otherwise, set big = small andsmall = r, and go to step 3.
This algorithm is guaranteed to end in a finite number of steps,since the remainder found in step 3 is getting continually smaller,and hence must eventually get to zero and stop.


Write a C program to find the Greatest Common Divisor of 2 numbersusing the above algorithm. Your program will take as inputtwo integer values from an input file and will output the GCD ofthese two numbers to an output file.

Your program must use a value-returningfunction with a do while loopto find the GCD of two numbers. Step 2 - 5 in thealgorithm must be implemented in this function (not in the mainfunction)
The main function reads pairs ofnumbers from an input file until end-of-file isencountered.

Note: The Endfile controlled loops are set up assentinel loops with a priming read and aread at the bottom of the loop. Use awhile loop to read all the values from thedata file and set it up in such away that it prevents going into aninfinite loop on wrong type of data.

Format of the data file is as follows: Name this file gcd.dat
140
72
31
109
85
105
432 64
Output is as follows: Name this file gcd.out
4 is the Greatest Common Divisor of 140 and 72.
etc. Who can help me withthis program... please...

This algorithm will find the greatest common divisor (gcd) of twopositive integers by repeatedly dividing two numbers, generating aquotient and remainder. The algorithm goes as follows:

1. Initially get the two numbers from the input file to determinethe GCD of, call them
num1 and num2.
2. Declare another two variables bigand small and make big the larger of the two
values and small, the smaller of thetwo.
3. Divide big by small to get the remainder r.
4. If r = 0, then the required GCD isequal to small, and we are done.
5. Otherwise, set big = small andsmall = r, and go to step 3.
This algorithm is guaranteed to end in a finite number of steps,since the remainder found in step 3 is getting continually smaller,and hence must eventually get to zero and stop.


Write a C program to find the Greatest Common Divisor of 2 numbersusing the above algorithm. Your program will take as inputtwo integer values from an input file and will output the GCD ofthese two numbers to an output file.

Your program must use a value-returningfunction with a do while loopto find the GCD of two numbers. Step 2 - 5 in thealgorithm must be implemented in this function (not in the mainfunction)
The main function reads pairs ofnumbers from an input file until end-of-file isencountered.

Note: The Endfile controlled loops are set up assentinel loops with a priming read and aread at the bottom of the loop. Use awhile loop to read all the values from thedata file and set it up in such away that it prevents going into aninfinite loop on wrong type of data.

Format of the data file is as follows: Name this file gcd.dat
140
72
31
109
85
105
432 64
Output is as follows: Name this file gcd.out
4 is the Greatest Common Divisor of 140 and 72.
etc.

Explanation / Answer

#include <stdio.h>
int gcd(int,int);
int main()
{ FILE* in;
FILE* out;
int n1,n2;
int input_status; /* value returned by fscanf */
char input[10];
in= fopen("gcd.dat","r");
   out= fopen("gcd.out","w");
    input_status=fscanf(in,"%d",&n1);  
   while(input_status != EOF)
      {if(input_status==0)
           {printf("invalid data entered--program aborted ");
            return 1;
            
            }
      input_status=fscanf(in,"%d",&n2);
          if(input_status==0)
           {printf("invalid data entered--program aborted ");
            return 1;
            }
      printf("%d is the Greatest CommonDivisor of %d and %d. ",gcd(n1,n2),n1,n2);
       input_status=fscanf(in,"%d",&n1);
       }
        fclose(in);
        fclose(out);
        return 0;
       
}

int gcd(int num1, int num2)    
{int big,small,r;
if(num1>num2)
     {big=num1;
     small=num2;
     }
else
     {small=num1;
     big=num2;
     }
do
{r=big%small;
   if(r==0)
      return small;
   big=small;
   small=r;
   }while (r!=0);
}