Thread: Advice on Accelerated C++ problem 4.2

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    112

    Advice on Accelerated C++ problem 4.2

    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;
    }

  2. #2
    Guest
    Guest
    The uniform initialization syntax is correct, though you'll find it's used far less often than plain assignment in the context you've given. setw looks to take an int, but you don't need anyone to tell you that. Your question regarding templates is vague – who knows what you may have in mind! If you read the basics on function templates (likely the first thing you'll read on the topic), you can probably answer this yourself early on.

    You might wanna rename that DoubleNumber function, as it squares the value.
    Last edited by Guest; 07-26-2016 at 03:17 AM.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    I added in using a template for SquareNumber. Coming from using mainly c for years, I'm finding this to be so cool.

    Code:
    // IntDoubling.cpp : Defines the entry point for the console application.//
    
    
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    
    
    using std::cout;
    using std::setw;
    
    
    template <typename T>
    T const& SquareNumber(T const& a);
    int GetNumWidth(int numToCount, int base = 10);
    
    
    template <typename T>
    T const& SquareNumber(T const& a)
    {
        return a * a;
    }
    
    
    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(SquareNumber(maxNum)) };
    
    
        for(int i = 0; i <= maxNum; ++i)
        {
            cout << "Int Num: " << setw(maxNumW) << i << " doubled is " << 
                setw(maxNumDubW) << SquareNumber(i) << std::endl;
        }
    
    
        for (int i = 0; i <= maxNum; ++i)
        {
            double y{ i * 1.0 };
            cout << "Double Num: " << setw(maxNumW) << y << " doubled is " <<
                setw(maxNumDubW) << SquareNumber(y) << std::endl;
        }    
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exercise 3-3 in Accelerated C++
    By quarks in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2013, 03:01 AM
  2. help with a exercise of Accelerated C++
    By roelof in forum C++ Programming
    Replies: 14
    Last Post: 06-11-2010, 09:45 AM
  3. Exercise 5.10 (ACCELERATED C++)
    By pantera in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2010, 08:46 AM
  4. Help with Accelerated C++ exercise
    By s_siouris in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2010, 03:10 PM
  5. reading Accelerated C++
    By Akkernight in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2008, 11:12 AM

Tags for this Thread