Building an array of ints

This is a discussion on Building an array of ints within the C++ Programming forums, part of the General Programming Boards category; I'm trying to build an array of integers, in which the number of elements of the array is determined by ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    12

    Building an array of ints

    I'm trying to build an array of integers, in which the number of elements of the array is determined by the length of a string parameter (which is an integer). The intent is to build a list starting from 1, then 4,16,64,256,1024, etc, until it becomes greater than the number read in on the string.

    Code:
    string num(string a) {
        int list1[a.length()^2/2];  
        list1[0] = 1;
        for (int y=1; y < 2*a.length(); y++) {
            list1[y] = 4^y;
            cout << "list1 contains:  " << list1[y] << endl;
            }    
    
    
         }
    The first problem is that the printout gives values of 5,6,7,0,1,2,3 before crashing, not the powers of four which I wanted it to.

    Any ideas on how to tackle this?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    ^ isn't the power operator, it's the bitwise exclusive-or operator.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    ^ isn't a power operator, it means bitwise exclusive-OR. I don't think XOR was what you wanted, so look into the pow function from <cmath>.
    Code:
    #include <cmath>
    
    std::pow(x, y); // x to the yth power
    Code:
    int list1[a.length()^2/2];
    Keep in mind that this isn't standard C++. An array in C++ can only have a constant integral size; your size isn't constant, so the code may not work on other compilers. The standard way to do it is
    Code:
    int *list1 = new int[std::pow(a.length(), 2.0) / 2];
    But then you need to remember to free the memory because it won't be released automatically.
    Code:
    delete [] list1;
    Even better would be a standard vector object:
    Code:
    #include <vector>
    
    std::vector<int> list1(std::pow(a.length(), 2.0) / 2);
    Kampai!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int list1[a.length()^2/2];
    Even if it were the power-of operator, division would likely get a higher precedence, so they'd end up with:
    Code:
    int list1[a.length()^0];
    Which would give them:
    Code:
    int list1[1];
    Which really wouldn't be what they wanted.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>Even if it were the power-of operator, division would likely get a higher precedence, so they'd end up with

    Since when does 2/2=0?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by quzah
    Even if it were the power-of operator, division would likely get a higher precedence
    ^ has a higher precedance than * in math. On my TI82 calculator: 3^2/2 gives 4.5, not 3.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Remember the order of operations, PEMDAS?
    Please Excuse My Dear Aunt Sally

    Parentheses Exponents Multiplication Division Addition Subtraction
    Last edited by fuh; 01-30-2005 at 05:32 PM.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Remember that C++ has its own operator precedence: http://www.cppreference.com/operator_precedence.html


    This means that even if you wrote your own number class and implemented ^ as the exponential operator, then Quzah's demonstration would be correct... Well, hopefully it would divide correctly, but other than that, it'd follow Quzah's demonstration.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    if you wrote your own number class and implemented ^ as the exponential operator
    Yes, *IF*. But ^ is not the exponential operator so it would be wrong. It's like overloading - as the multiplication operator and claiming that "2 times 3 plus 1" is 0 instead of 7.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by JaWiB
    >>Even if it were the power-of operator, division would likely get a higher precedence, so they'd end up with

    Since when does 2/2=0?
    Yeah... well about that... There was this thread about integer division...no, I won't go there again. (For a while anyway.)

    Oh, wait! I overloaded the division operator to to modulus! That's it!

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    12
    I appreciate all the help and replies so far, but I've got another related question (of course )

    Edit: Answered my own question.
    Last edited by Lone; 01-31-2005 at 12:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading ints specified in a string into an array
    By Curtster in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 02:19 AM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. array of ints
    By barim in forum C Programming
    Replies: 4
    Last Post: 01-01-2005, 04:21 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Replies: 4
    Last Post: 11-18-2001, 06:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21