Question

In: Computer Science

You will need to write a library of functions that uses pointers. Your functions must follow...

You will need to write a library of functions that uses pointers. Your functions must follow the headers below.

int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr);

Create a function to do integer division that gives the division result and remainder via output parameters, quo_optr and rem_ptr.  

Additionally, the function must return an int representing the success/failure of the function. The function should return 0 if it succeeds and -1 if there is a divide by zero error. (Note that you should not actually do the division if there will be a divide by zero error. Simply return -1.)

Solutions

Expert Solution

(I have written the total code to show how the code is working. If you need only the intDivide() function neglect the main function which I have given)

(Here the results of division will be in int only because we are performing integer division. Normally when we divide 5/2 the result will be 2.5. But here the result will be 2. Because the datatype int will neglect the values after decimal part.)

Code Explanation:

In the function first check if denominator is not equal to 0. If yes then calculate numerator / denominator and assign to *quo_pte
Then calculate numerator % denominator and assign to *rem_pte then return 0
If the If condition fails then return -1.

intDivide() function code:

int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr)
{
    if(denominator!=0)
    {
        *quo_ptr = numerator / denominator;
        *rem_ptr = numerator % denominator;
        return(0);
    }
    else
    {
        return(-1);
    }
}

code Implementation with main function:

#include <stdio.h>
int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr);
int main()
{
    int numerator, denominator, quo_ptr, rem_ptr;
    printf("Enter numerator: ");
    scanf("%d",&numerator);
    printf("Enter denominator: ");
    scanf("%d",&denominator);
    int res = intDivide(numerator, denominator, &quo_ptr, &rem_ptr);
    if(res == 0)
    {
        printf("division result is: %d\n",quo_ptr);
        printf("remainder result is: %d",rem_ptr);
    }
    else
    {
        printf("division by zero error");
    }
        
    return 0;
}

int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr)
{
    if(denominator!=0)
    {
        *quo_ptr = numerator / denominator;
        *rem_ptr = numerator % denominator;
        return(0);
    }
    else
    {
        return(-1);
    }
}

Sample O/P1:

Enter numerator: 23                                                                                                             
Enter denominator: 5                                                                                                            
division result is: 4                                                                                                           
remainder result is: 3   

Sample O/P2:

Enter numerator: 8                                                                                                              
Enter denominator: 0                                                                                                            
division by zero error 

Code Screenshot:

Sample O/P1 screenshot:

Sample O/P2 screenshot:

(If you still have any doubts regarding this answer please comment I will definitely help)


Related Solutions

We will write a simple program that simply uses the three standard library functions. The following...
We will write a simple program that simply uses the three standard library functions. The following is a high level description of the main program (main.cpp). In this program, please do the following: Include the C-string library. #include <cstring> Declare the following C-style string: char line[] = "ls -l -a | wc -c >> myfile";. Declare a variable of type size_t named "len". Use the builtin strlen function to compute the length of the variable "line". Store the result in...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (forget-n L1 N) that returns the elements of L1 except for the first N. If N is negative, return all elements. If N exceeds the length of L1 return the empty list....
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem needs to use DrRacket software. Racket Language. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarification....
You must write each of the following scheme functions. You must use only basic scheme functions,...
You must write each of the following scheme functions. You must use only basic scheme functions, do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1. (first-n...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function (join-together L1 L2) that takes a sorted list (ascending) of integers L1 and a sorted list of elements L2 and returns a sorted list containing all elements of both L1 and L2. See...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarificaton. (indices '(a b c a e f a) 'a')...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function named (list-replace ALIST SYM VAL) that accepts a list of elements and returns that list where all SYM's (a single symbol) have been replaced by the VAL (some scheme value). The replacement must occur even within nested lists. For example: (list-replace '(a...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
In this homework you will implement a Library class that uses your Book and Person class...
In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out. Book.java You will need to modify your Book.java from homework 2 in the following ways: field: dueDate (private)             A String containing the date book is due.  Dates are given in the format "DD MM YYYY", such as "01 02 2017"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT