Question

In: Computer Science

3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design...

3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design in C book. All input should be read from a file and output should be written to a file. Make sure that you design a function greatest_common_divisor() which calculates and returns the greatest common divisor of two integer numbers. Also, develop functions to read data from a file and to write data to a file. Problem Statement: The greatest common divisor (gcd) of two integers is the product of the integers' common factors. Write a program that inputs two numbers and implements the following approach to finding their gcd. We will use the numbers -252 and 735. Working with the numbers' absolute values, we find the remainder of one divided by the other. 252 / 735 = 0 R252 Now we calculate the remainder of the old divisor divided by the remainder found. 735 / 252 = 2 R231 We repeat this process until the remainder is zero. 252 / 231 = 1 R21 231 / 21 = 11 R0 The last divisor (21) is the gcd.

Solutions

Expert Solution

Below is the code in C for the calculation of GCD by reading two integers from the file and then writing back the result to file. One function is built for the reading of input, one for writing the output and one for calculating the GCD. Sample output is attached at the end, and also attached the sample input and output file.


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int gcd(int a, int b){                // gcd function following the logic given in question.
    if(a==0) return b;
    return gcd(b%a, a);
}

void read_file(int *first, int *second, char *str){   // reading from file function.
    FILE * pFile;
    int number;
    pFile = fopen (str,"r");        // opening file in read mode.
    
    int idx = 1;
    while (!feof (pFile)) {                 // reading till the end of  the file.
        fscanf(pFile, "%d", &number);
        if(idx%2==1){                          // logic to appropriately updating the two variables for gcd calculation.
            *first = number; 
            idx++;
        }
        else{
            *second = number;
        }
    }
    fclose (pFile);                   // closing the file.
}

void write_file(int result, char *str){
     FILE *fptr;
     fptr = fopen(str, "w");              //opening the file in write mode.
     if (fptr != NULL){                  // since only one integer to write, 'if' is used, otherwise loop can be used. 
         fprintf(fptr, "%d", result);
     }
     fclose(fptr);                       // closing the file.
}

int main()
{
    
    int first, second;
    printf("reading from file...\n");
    read_file(&first, &second, "input.txt");              // calling reading function by providing two integers address and file name as a string.
    
    printf("first = %d ",first);
    printf("second = %d \n",second);
    
    int gcd_ans = gcd(abs(first), abs(second));           // calling gcd function. 
    printf("gcd = %d\n",gcd(abs(first), abs(second)));
    
    printf("writing result to file...\n");
    write_file(gcd_ans, "integers.txt");             // calling write function.
     
    
    return 0;
}

Sample Output:-

Input File:-

Output File:-


Related Solutions

Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the...
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in the lecture notes. The solution presented in the notes uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this assignment, standard counting semaphores will be...
programming in python Design a program that initializes a list of 5 items to zero (use...
programming in python Design a program that initializes a list of 5 items to zero (use repetition operator). It then updates that list with a series of 5 random numbers. The program should find and display the following data: -The lowest number in the list - Average of the numbers stored in the list
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C code. You must use Java with Threads instead of Pthreads.A brief overview is below. This program should work as follows: The user will enter on the command line the sleep time, number of producer threads, and the number of consumer threads. One example of the Java application from the command line could be “java ProducerConsumer...
Programming Problem Design a program in java that can perform three different arithmetic operations based on...
Programming Problem Design a program in java that can perform three different arithmetic operations based on the user’s input. The three operations are 1. Summation of integers from 1 to m 2. Factorial of a given number n (n!) 3. Finding the leftmost digit of a given integer (note: you have to use int) This program should prompt the user a menu including four options, ask the user for one option, and perform the corresponding computation. This process will repeat...
Chapter 12 Problem 3 (Excel functions needed) Consider the project contained in Problem 7 in Chapter...
Chapter 12 Problem 3 (Excel functions needed) Consider the project contained in Problem 7 in Chapter 11 (California Health Center). a. Perform a sensitivity analysis to see how NPV is affected by changes in the number of procedures per day, average collection amount, and salvage value. Remember supplies vary with number of procedures. b. Conduct a scenario analysis. Suppose that the hospital's staff concluded that the three most uncertain variables were number of procedures per day, average collection amount, and...
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
Relating to the design disciplines identified in Chapter 3, Section 3.4, a listing of typical program...
Relating to the design disciplines identified in Chapter 3, Section 3.4, a listing of typical program tasks is provided. In each of the following disciplines, identify those tasks that can be accomplished using computerized methods. Include some specific examples. (a) Reliability engineering tasks (b) Maintainability engineering tasks (c) Human-factors engineering tasks (d) Logistics and supportability tasks (e) Quality engineering tasks (f) Value/cost engineering tasks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT