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...
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 Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
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) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
Write a C program that calculates the average grade for a specified number of students from...
Write a C program that calculates the average grade for a specified number of students from each student's test1 and test2 grades. The program must first ask the user how many students there are. Then, for each student, the program will ask the user for the test1 grade (grade #1) and test2 grade (grade #2). The program should be able to handle up to 100 students, each with 2 grades (test1 and test2). Use a two-dimensional float array to store...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT