I'm starting to practice using C++, and have read some info on using C++11 and am trying to use it a little bit.
I tried to follow the intent of the problem and did not use pow from <cmath>.
I plan on learning how to use template, and will turn the double number into a template to tackle other data types for problem 4.3.
I was hoping to make sure i'm using uniform intialization correctly, using the right data type for setw and if my template idea would work.
Code:
// IntDoubling.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using std::cout;
using std::setw;
int DoubleNumber(int numToDouble);
int GetNumWidth(int numToCount, int base = 10);
int DoubleNumber(int numToDouble)
{
return numToDouble * numToDouble;
}
int GetNumWidth(int numToCount, int base)
{
int number_of_digits{ 0 };
do {
++number_of_digits;
numToCount /= base;
} while (numToCount);
return number_of_digits;
}
int main()
{
int maxNum{ 100 };
// get width of max num
int maxNumW{ GetNumWidth(maxNum) };
// get width of max num ^ 2
int maxNumDubW{ GetNumWidth(DoubleNumber(maxNum)) };
for (int i = 0; i <= maxNum; ++i)
{
cout << "Num: " << setw(maxNumW) << i << " doubled is " <<
setw(maxNumDubW) << DoubleNumber(i) << std::endl;
}
return 0;
}