Home>

I would like to know how to write the contents of a given function based on the following conditions.

Program requirements

1. Definition of structure fraction (with int type member variables numer and denom)

2. Create a function mul that multiplies fractions

・ The arguments of the function mul are the variables a and b of the structure fraction type.
-The return value of the function mul is the result of multiplication a * b in the meaning of a fraction (however, it is not necessary to divide it)

3. Create a print function to display the value of the structure fraction type

-The argument of the function print is the variable a of the structure fraction type.
-The return value of the function print is none.
Example: If 1/3, display as 1/3.

4. Prepare a variable of the appropriate structure fraction type in the main function, and check whether the function mul works properly using the print function.

Applicable source code
#include<stdio.h>
typedef struct fraction {
    int numer;
    int denom;
};
int mul () {
}
void print () {
}

int main () {
}

I've worked on the code myself, but I don't understand how to put structure arguments because there are multiple parts that I don't understand.

  • Answer # 1

    Declaration only for the time being

    /* 1. Definition of structure fraction
    (Has int type member variables numer (numerator) and denom (denominator)) * /
    typedef struct fraction {
        int numer;
        int denom;
    } fraction;
    / *
    2. Create a function mul that multiplies fractions
    -The argument of the function mul is the variable a and b of the structure fraction type.
    -The return value of the function mul is the result of multiplication a * b in the meaning of a fraction (however, it is not necessary to divide it) * /
    fraction mul (fraction a, fraction b);
    / *
    3. Create a print function to display the value of the structure fraction type
    ・ The argument of the function print is a variable a of the structure fraction type.
    -The return value of the print function is none.
    Example: If 1/3, display as 1/3. * /
    void print (fraction a);