Thread: Outputting a list of squared Doubles

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

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

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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);
                    }
         }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    OK I did the fix and it seems to still be eating my memory away. Nothing gets outputted either.
    Last edited by dnguyen1022; 01-19-2009 at 12:17 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Post new code and tell us the value that you input.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Look at my first reply. Have you fixed that issue?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    oh...sorry I overlooked that. Got it now, now I have to split up the numbers into columns and rows

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dnguyen1022 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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.
    Last edited by dnguyen1022; 01-19-2009 at 01:00 PM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dnguyen1022 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    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?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dnguyen1022 View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Alright, I know how to deal with it if its a negative number. Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with outputting a list of squared numbers
    By dnguyen1022 in forum C++ Programming
    Replies: 28
    Last Post: 01-19-2009, 09:06 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM