Question

In: Computer Science

this is a C++ class Topics Numeric Input (Whole Numbers) Assignment Finding Quotient Finding Remainder Numeric...

this is a C++ class

Topics

Numeric Input (Whole Numbers)

Assignment

Finding Quotient

Finding Remainder

Numeric Output (Whole Numbers)

Description

Write a program that will convert a distance entered in inches to a distance in yards, feet and inches.

The program will ask the user to input a distance in inches as a whole number. It will convert the distance entered into yards, feet and inches. Then it will display the distance in yards, feet and inches.

For example, the program will convert 5o inches into 1 yard, 1 ft and 2 inches. See the Testing section for test data.

Requirement

Do this exercise using only three variables. See Sample Code section below.

Test Data

Use the test data in the Test Run below.

Test Run

(User input is shown in bolds).

Enter distance in inches:

140

The distance of 140 inches is the same as the distance of 3 yards, 2 feet and 8 inches

Submit

Final one line output

Source code

Discussion

There is a difference between decimal division and integer division.

They both use the same symbol “/ “.

But the meaning of the symbol “/ “ is different depending upon the context it is used in.

Decimal Division:

If one or both the operands around the division symbol are decimal variables or constants, a decimal division is performed.

In a decimal division, the answer is a decimal number. The remainder is irrelevant.

Integer Division:

If both the operands around the division symbol are whole number variables or constants, an integer division is done.

In an integer division, the whole number quotient is the answer, the remainder is ignored.

To find remainder in an integer division, use the remainder operator ( % ).

The remainder operator works only when both the operands are whole number variables or constants.

Remember, in an integer division, the / operator returns the quotient and the % operator returns the remainder.

Examples:

14/4;   //Integer division. Answer 3.

14/4.0; //Decimal division. Answer 3.5

14.0/4.0; //Decimal division. Answer 3.5

14 % 4; //Remainder operator. Answer 2

14 % 4.0; //Illegal. Remainder operator not available for decimal values.

14.0 % 4; //Illegal

double d = 14/4.0;       //d is 3.5

double d = 14.0/4.0;    //d is 3.5

double d2 = 14/4;        //d2 is 3

int i = 14/4;                  //i is 3

double x = 14;

double z = x / 4; //z is 3.5

Sample Code

int inches, feet, yards;

cout << "Enter distance in inches" << endl;

cin >> inches; // assume user enters 50

// Convert inches into feet and inches.

//The quotient operation below will return feet

feet = inches/12; //This integer division computes quotient. So feet is now 4. Remainder is ignored.

//The remainder operation below will return the inches.

inches = inches%12; //This computes the remainder. So, inches is now 2. Quotient is ignored.

//Conver feet into yards and feet.

//Repeat the above process.

//Find yards (from the feet above) by using quotient operator ( / ).

yards = feet / 3; //yards is now 1

//Find the remaining feet (from the feet above) by using remainder operator (%).

feet = feet % 3; //feet is now 1

Solutions

Expert Solution

Thanks for the question.


Here is the completed code for this problem.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer.

Thanks
===========================================================================

#include <iostream>
using namespace std;


int main(){
  
   int inches, feet, yards;
   cout << "Enter distance in inches: ";
   cin >> inches; // assume user enters 50
  
   int starting_inches=inches;
  
   // 36 inches = 1 yard
   yards = inches/36;
   inches = inches%36;
  
   // 12 inches = 1 feet
   feet = inches/12;
   inches = inches%12;
  
   cout<<starting_inches<<" inches is "<<yards<<" yards "<<feet<<" feet(s) and "<<inches<<" inches."<<endl;

}

=====================================================================


Related Solutions

Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should...
Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Candy Calculator [50 points] The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 *...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
Please use C++ 1. A Sequence of numbers and a sum is given as input. List...
Please use C++ 1. A Sequence of numbers and a sum is given as input. List all combinations of numbers that can add upto the given Sum. User input:- Enter numbers: -   1, 3,7,9,11 Enter a sum :- 20 Output: 11+9 = 20 1+3+7+9 = 20 2. Print absolute prime numbers between a given range:- Enter Range - 2,99 For eg Prime numbers are:- Prime numbers ==> 2,3,5,7,11,13,17,19,23 Absolute Prime numbers ==> 2,3,5,7,11,23 3. Write an encoder and decoder program,...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT