# Outputting a list of squared Doubles

• 01-19-2009
dnguyen1022
Outputting a list of squared Doubles
Some of you may have seen the program in which I outputted a list of squared numbers starting from 0 up until that given number. This time I am trying to do the same thing with a list of doubles. I believed the concept was about the same but I was proven wrong when my program sucked up every last bit of my memory. I think my problem may be where I am using my loops and how it deals with the doubles. Thanks for the help!

main
Code:

```#include "squareddoubleheader.h" #include <conio.h> #include <iostream> #include <vector> using std::cin; using std::cout; using std::vector; int main(){         cout << "What is the double you wish to square? ";     double doub;     cin >> doub;         vector<double> doubled;     squareddouble(doub,  doubled);         for(int i = 0; i <= doub; i++){             cout << i << "      " << doubled[i];             }                 cout << "Press enter to continue..";     _getch();         return 0;     }```
source
Code:

```#include "squareddoubleheader.h" #include <vector> using std::vector; void squareddouble(double doub, vector<double>& squaredoub){     for(double d = 0.0; d <= doub; doub++){                 squaredoub.push_back(d*d);                 }     }```
• 01-19-2009
anon
Your error is here
Code:

```void squareddouble(double doub, vector<double>& squaredoub){     for(double d = 0.0; d <= doub; doub++){```
However, you probably should keep the values that are used as indices integral. Only cast the integral type to double when calculating the square:

Code:

```void squareddouble(int count, vector<double>& squaredoub){     for(int d = 0; d <= count; d++){                 squaredoub.push_back(static_cast<double>(d)*d);                 }     }```
• 01-19-2009
dnguyen1022
OK I did the fix and it seems to still be eating my memory away. Nothing gets outputted either.
• 01-19-2009
anon
Post new code and tell us the value that you input.
• 01-19-2009
dnguyen1022
Yes sir.

source
Code:

```#include "squareddoubleheader.h" #include <vector> using std::vector; void squareddouble(double doub, vector<double>& squaredoub){     for(int d = 0; d <= doub; doub++){                 squaredoub.push_back(static_cast<double>(d)*d);                 }     }```
main
Code:

```#include "squareddoubleheader.h" #include <conio.h> #include <iostream> #include <vector> using std::cin; using std::cout; using std::vector; int main(){         cout << "What is the double you wish to square? ";     double doub;     cin >> doub;         vector<double> doubled;     squareddouble(doub,  doubled);         for(int i = 0; i <= doub; i++){             cout << i << "      " << doubled[i];             }                 cout << "Press enter to continue..";     _getch();         return 0;     }```
I've tried 1, 1.11, 1.0, 10, 3.211...all produce the same result of nothingness.
• 01-19-2009
anon
Look at my first reply. Have you fixed that issue?
• 01-19-2009
dnguyen1022
oh...sorry I overlooked that. Got it now, now I have to split up the numbers into columns and rows
• 01-19-2009
dnguyen1022
So given a double, how would I determine its width without the decimals? For example 100.1111, should be a width of 3 for 100. or 1.1 should just be 1.
• 01-19-2009
matsp
Quote:

Originally Posted by dnguyen1022
So given a double, how would I determine its width without the decimals? For example 100.1111, should be a width of 3 for 100. or 1.1 should just be 1.

The log10 method we described before works for (positive) integers. So as long as you have a set number of decimals, your width of the whole number can be calculated from that.

--
Mats
• 01-19-2009
dnguyen1022
Actually how would I use this function? Do I just simply replace arr with doub?

Code:

```for (unsigned i = 0; i != sizeof(arr)/sizeof(arr[0]); ++i) {         std::cout << arr[i] << ' ' << static_cast<unsigned>(std::log10(arr[i])) + 1 << '\n';     }```
And how do I declare the array?

Code:

```for (unsigned i = 0; i != sizeof(doub)/sizeof(arr[0]); ++i) {         cout << arr[i] << ' ' << static_cast<unsigned>(log10(arr[i])) + 1 << '\n';     }```
That is what I did but its giving me an arr not declared error.
• 01-19-2009
matsp
Quote:

Originally Posted by dnguyen1022
Actually how would I use this function? Do I just simply replace arr with doub?

Code:

```for (unsigned i = 0; i != sizeof(arr)/sizeof(arr[0]); ++i) {         std::cout << arr[i] << ' ' << static_cast<unsigned>(std::log10(arr[i])) + 1 << '\n';     }```
And how do I declare the array?

That code is simply to show the lenght in digits of each number in arr[]. You have a vector with your squared numbers, so you need to figre the length of those. The part that does static_cast<unsigned>(std::log10(...) is what you need - the rest is just to walk through the testing array - which is not what you want to do.

--
Mats
• 01-19-2009
dnguyen1022
Oh, okay so I was able to get the length, but its giving me 1 less than the actualy length which I believe is why there is a + 1 in the example code. Is this correct?
• 01-19-2009
matsp
Quote:

Originally Posted by dnguyen1022
Oh, okay so I was able to get the length, but its giving me 1 less than the actualy length which I believe is why there is a + 1 in the example code. Is this correct?

Yes - and if you want to have space for "." between the integer and fraction part, you may need to add another 1.

Note also that log() is invalid for negative numbers.

--
Mats
• 01-19-2009
dnguyen1022
Alright, I know how to deal with it if its a negative number. Thanks for your help!