Question

In: Computer Science

Write a program that calculates the floor of a decimal number as defined here. Basically the...

Write a program that calculates the floor of a decimal number as defined here. Basically the Floor of any decimal number x, is the nearest whole number less than or

equal to x. ( Code Should Be In C++)

Requirements

1) You must implement a function to calculate the floor (you cannot use C++'s floor function). Name it floorAndError). It will have a return value and a single parameter

2) The program will ask the user to input a number (let's call this variable userInput) in main

3) The number will be passed to floorAndError by Reference and no other variables should be passed in

4) The function should change the userInput variable to the nearest whole number less than or equal to the original value of userInput that was inputted by the user. Therefore if the user inputted 4.3 into the variable userInput, after the function runs userInput will have the value 4.0. (note if the user enters -4.3 the floor is actually -5, think about how you might do this)

5) The function should return how much the value was changed by, i.e., the difference the new value of userInput and the old value of userInput. SO if the user inputted 4.3 the function will return a value equal to 4.0-4.3= -.3

6) The program should define the function prototype above main and the function definition after main

7) All the program input and output should occur in main

8) The function comments must have a pre condition and post condition both at the function prototype and the function definition

9) All the rest of the code should include comments too

Solutions

Expert Solution

CODE IN C++:

#include <iostream>

using namespace std;
//method to calculate power
int pow(int x, int y){
if(y==0)
return 1 ;
return x * pow(x,y-1);
}
float floatAndError(float num){
int number = (int)num;
int multiplier = pow(10,6);
number = number * multiplier;
number = number / multiplier;
if(number<0)
number = number - 1 ;
cout<<"The floor of your number is:"<<number<<endl;
float change = num - number;
return change ;
}
int main()
{
//decalring variable
float number;
//taking input from the user
cout << "Enter a floating point number:";
cin >> number;
//displaying the result
cout<<"the relative change of number with its floor:"<<floatAndError(number)<<endl;
return 0;
}
OUTPUT:


Related Solutions

IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel....
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations,...
Write a program that calculates the occupancy rate for each floor of a hotel. (Use a...
Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations, the program...
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
Write a program that calculates the balance of a savings account at the end of a...
Write a program that calculates the balance of a savings account at the end of a three month period. It should ask the user for the starting balance and the annual interest rate. A loop should then iterate once for every month in the period, performing the following: Ask the user for the total amount deposited into the account during that month. Do not accept negative numbers. This amount should be added to the balance. Ask the user for the...
Write a program that calculates the balance of a savings account at the end of a...
Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following: Ask the user for the amount deposited into the account during the month. (Do not accept negative numbers.) This amount should be added...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT