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 class template called genericStack for storing any data type in a static stack. Your...
Define a class template called genericStack for storing any data type in a static stack. Your class template must include a constructor, a destructor, push, pop, peek, isFull, and isEmpty functions (no display function is required). Write a simple main function in your program that demonstrates the class template with a stack of strings.
Create your own data type using a C++ structure. Assign values to a variable of that...
Create your own data type using a C++ structure. Assign values to a variable of that data type. Retrieve values from the variable of that data type. Description: In this lab you will choose the statements to create a program that: Creates a brand new data type called "Monster". Once created, creates a variable of type Monster and assign values to the variable and then display the contents. This lab will again take the form of a guided multiple-choice quiz...
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.
This project requires the student to create a record (in C++ called a struct). The record...
This project requires the student to create a record (in C++ called a struct). The record should contain several fields of different data types and should allow the user to search the records to find the student with the highest grade. In this project the student should gain an understanding of the nature of records with multiple data types, how to save the records, search the records and retrieve them.  The special problems of maintaining records of multiple data types on...
1) Define a C struct that can be used to represent an ingredient in a recipe....
1) Define a C struct that can be used to represent an ingredient in a recipe. You must include the ingredient, the amount to use, and the unit of measurement. When allocated, the struct must be self-contained; do not rely on information being stored anywhere else in memory. 2) Define a C function that will print an array of ingredients to the standard output stream, one per line. You must use the struct definition from the first part. 3) Define...
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...
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...
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 {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT