Question

In: Computer Science

(C++ ONLY) You will define your own data type. It will be a struct called Fraction....

  1. (C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator.

  2. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form.

  3. You will write a function called print that takes a Fraction as a parameter and prints the fraction in the form "x/y" where x is the numerator and y is the denominator.
  4. You will write a main function. The main function will prompt the user to enter a numerator and denominator. The program will print the fraction as it was entered by the user, and it will also print the fraction in its reduced form.

    Here is an example run of the program, where the user input is shown in blue:

     Enter numerator: 5
     Enter denominator: 10
    
     Entered: 5/10
     Reduced: 1/2
    
    Please show it as 1 header file and 2 implementation files

Solutions

Expert Solution

here is the required solution

here is the code

#include <iostream>
using namespace std;
//structure defination
struct fraction
{
int numerator;
int denominator;
};
//function to reduce fraction
struct fraction reduce(fraction f)
{
for(int i=f.numerator;i>1;i--)
{
if(f.numerator%i==0 && f.denominator%i==0)
{
f.numerator=f.numerator/i;
f.denominator=f.denominator/i;
}
}
return f;}
//function to print fraction
void print(fraction f)
{
cout<<f.numerator<<"/"<<f.denominator<<"\n";
}

int main()
{ //declaration of variable
struct fraction f,freduced;
//user input
cout<<"Enter numerator:";
cin>>f.numerator;
cout<<"Enter denominator:";
cin>>f.denominator;
//call the reduce fraction
freduced=reduce(f);
//print the result
cout<<"Entered:";
print(f);
cout<<"Reduced:";
print(freduced);

return 0;
}


Related Solutions

Define a struct pet with properties name, age, weight, and type. Use appropriate data types for the different properties.
use C source code to complete the following:Define a struct pet with properties name, age, weight, and type. Use appropriate data types for the different properties. You may assume that the strings will never be longer than 19 chars. Don’t forget to add an extra char for the NULL character that terminates the strings.
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
Write a program in C language 1- Define a struct for students with the aforementioned attributes,...
Write a program in C language 1- Define a struct for students with the aforementioned attributes, test it by populating one initialized struct variable with arbitrary input and take screenshots of the output. 2- For each student, struct add an array of number grades for a class the students are enrolled in such as S E 185. Then write functions which find the max, average, and minimum score for a specified assignment identified by a number, for example, Assignment 0...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
Information Technology (IT) Security Define your chosen type of security in your own words. Does the...
Information Technology (IT) Security Define your chosen type of security in your own words. Does the term shrinkage relate to your type of security? If so, how? (If not, please omit this portion from your response.) What challenges and risks exist for this type of security? How are the risks mitigated? Tracked? Eliminated? What types of internal and external factors exist? What key strategies are used to protect assets, personnel, and infrastructure? Please be sure to outline prevention and response...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions. Please keep all of your code in one file for this week. We will separate...
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20];...
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20]; int age; char country[20]; char Position[20]; double Salary; double Rating; }; First, create an array of BarcelonaPlayer structures. Now, write a function that takes an array of BarcelonaPlayer structures as input and find out the highest paid player among all the players. void highestPaidPlayer(struct BarcelonaPlayer *pl, int size); Create another function that finds all the players from Argentina. void findPlayers(struct BarcelonaPlayer *pl, int size);
In this assignment you will build a small C# project that uses… • A struct •...
In this assignment you will build a small C# project that uses… • A struct • A method with a reference parameter • A while-loop • A switch statement and block instructions: Inside the StringHandler struct add a public void method named Abbreviate. The Abbreviate method must take a string parameter that is passed by reference. This method will take the name of a month (January, February, etc.) as its input and convert it to a 3-letter abbreviation of the...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT