Skip to content

Instantly share code, notes, and snippets.

@Chamauta
Created December 22, 2021 17:30
Show Gist options
  • Save Chamauta/411986be6c47eabbf103e52f7f32cc1b to your computer and use it in GitHub Desktop.
Save Chamauta/411986be6c47eabbf103e52f7f32cc1b to your computer and use it in GitHub Desktop.
Deitel_Chapter_06_Exercise_14
/*6.14 (Rounding Numbers) Function floor can be used to round a number to a specific decimal
place.The statement
y = floor(x * 10 + 0.5) / 10;
rounds x to the tenths position(the first position to the right of the decimal point).The statement
y = floor(x * 100 + 0.5) / 100;
rounds x to the hundredths position(the second position to the right of the decimal point).Write
a program that defines four functions to round a number x in various ways:
a) roundToInteger(number)
b) roundToTenths(number)
c) roundToHundredths(number)
d) roundToThousandths(number)
For each value read, your program should print the original value, the number rounded to the
nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth
and the number rounded to the nearest thousandth.*/
// =============================================== 06-14 =============================================== //
#include <iostream>
#include <iomanip>
#include <cmath>
// function definitions
void displayPrompt(int& totalEntriesRef )
{
std::cout << "Enter the number entries: "; // 1st prompt
std::cin >> totalEntriesRef; // set variable limit
std::cout << "Enter " << totalEntriesRef << " numbers separated by spaces: ";
}
void displayTableHeader()
{
std::cout << std::endl;
std::cout << std::right << std::setw(60) << "Rounded to the nearest";
std::cout << std::endl;
std::cout << std::setfill(' ') << std::setw(22) << " ";
std::cout << std::setfill('=') << std::setw(50) << " ";
std::cout << std::setfill(' '); // unstick setfill('=') above
std::cout << "\n"
<< std::left << std::setw(5) << "Count"
<< std::right << std::setw(10) << "Number"
<< std::right << std::setw(14) << "Integer"
<< std::right << std::setw(14) << "Tenths"
<< std::right << std::setw(14) << "Hundreths"
<< std::right << std::setw(14) << "Thousandths"
<< std::endl;
}
// the function below replaces the four functions suggested in the problem statement
double roundingFunction(double n, int power)
{
return floor(n * pow(10, power) + 0.5) / pow(10, power);
}
void dispalyResults(int myCount, double myNumber)
{
std::cout << std::noshowpoint;
std::cout << std::left << std::setw(5) << myCount;
std::cout << std::right << std::setw(10) << myNumber;
for (int i = 0; i <= 3; i++) // iterates powers of ten for roundingFunction
std::cout << std::setw(14) << roundingFunction(myNumber, i);
std::cout << std::endl;
}
// main
int main ()
{
int totalEntries; // number of inputs made by user
int count{ 1 }; // an iterator
double number; // number entered by user
displayPrompt(totalEntries);
while (true)
{
std::cin >> number; // stream input by user into variable number
if (count == 1) // print table header
displayTableHeader();
if (count >= 1) // calculate rounding
{
dispalyResults(count, number);
++count; // increments count after each cin stream from buffer;
}
if (count > totalEntries)
{
std::cout << std::endl;
break; // break out of loop and end program
}
}
} // end of exercise_06-14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment