Skip to content

Instantly share code, notes, and snippets.

@Chamauta
Last active December 22, 2021 16:10
Show Gist options
  • Save Chamauta/75fe4848a0cdc8e3b7bb470ae6073676 to your computer and use it in GitHub Desktop.
Save Chamauta/75fe4848a0cdc8e3b7bb470ae6073676 to your computer and use it in GitHub Desktop.
Deitel_Chapter_06_Exercise_13
/*6.13 (Rounding Numbers) An application of function floor is to round a value to it's nearest
integer. The statement
y = floor(x + .5);
rounds the number x to the nearest integrand and assigns the result to y. Write a program that reads
several numbers and uses the preceding statement to round each of these numbers to the nearest
integer. For each number processed, print both the original number and the rounded number.*/
// =============================================== 06-13 =============================================== //
#include <iostream>
#include <iomanip>
// function definitions
void displayPrompts(int& totalEntriesRef)
{
std::cout << "Enter the total number of entries then press Enter: "; // 1st prompt
std::cin >> totalEntriesRef; // stream input into totalEntries
std::cout << "Enter " << totalEntriesRef << " doubles separated by spaces : ";
}
double nearest(double doubleNumber)
{
return floor(doubleNumber + 0.5);
}
void displayTableHeader()
{
std::cout << "\n"
<< std::left << std::setw(5) << "Entry"
<< std::right << std::setw(10) << "Number"
<< std::right << std::setw(20) << "Nearest Integer"
<< std::endl;
}
void displayResults(int myCount, double myNumber, double myNearestInteger)
{
// display entry count in table
std::cout << std::left << std::setw(5) << myCount;
// display number entered by user without trailing zeros or unnecessary decimal point (see link below)
std::cout << std::right << std::setw(10) << std::noshowpoint << myNumber;
// display rounded double to nearest integer
std::cout << std::setw(20) << myNearestInteger << std::endl;
}
// main
int main()
{
double number;
double nearestInteger;
int totalEntries;
int count{ 1 }; // iterator used in while loop
displayPrompts(totalEntries); // note that totalEntries is passed by reference and will be defined in this function
while (true) // loop to display table with all entries
{
std::cin >> number; // (a double) entered by user in function displayPrompts
// count increments after each cin stream read from buffer.
if (count == 1) // print table header on first pass
displayTableHeader();
if (count >= 1) // round to nearest integer, output into a table and increment count
{
nearestInteger = nearest(number); // call function nearest to return the nearest integer; assign to nearestInteger
displayResults(count, number, nearestInteger); // pass by value count, number and nearestInteger
++count; // increment count after each cin stream pass;
}
// end when done
if (count > totalEntries)
{
std::cout << std::endl;
break; // exit loop and end program
}
}
} // end of Rounding Numbers exercise
// see: https://www.tutorialspoint.com/cpp_standard_library/cpp_noshowpoint.htm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment