Thread: Problem with passing even ints into new array

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    Problem with passing even ints into new array

    Im working writing a program that outputs to a dos console. The even output comes out as a big negative number.

    The code is here:
    Code:
    int * even (int *list, int size, int & esize)
    //Pre: size > 0
    //Post: return base address of new dynamic array containing even values
    // from list array and set esize to the size of the new array
    {
    
    // Solving for the number of the Even Numbers
    for (int i=0; i<size; i++)
    {
    if (list[i] % 2 == 0)
    esize++;
    }
    int *leven = new int[esize];
    
    //adding the even numbers into the array to be returned
    
    for (int j = 0; j < size; j++)
    {
    if (list[j] % 2 ==0 )
    {
    for(int k = 0; k < esize; k++)
    list[j] = leven [k];
    }
    }
    return leven;
    
    }
    Thanks for any help

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You're never actually assigning anything to the elements of leven. And this loop does nothing but assign leven[esize-1] (which is uninitialized) to each even element of list:
    Code:
    for(int k = 0; k < esize; k++)
    list[j] = leven [k];

    Also, this function seems to assume that esize is zero, which might cause problems if you inadvertently pass it a reference to a variable that is not initialized or initialized to a different value.

    Also, using dynamic arrays is usually best to be avoided in C++. A safer choice is std::vector.
    "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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent properly please!
    NOTE TO SELF: Need to learn how to use std::copy >_<
    Last edited by Elysia; 12-05-2007 at 01:38 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > memcpy(list, list1, *size1);
    A useful short-cut for POD data like an array of int's, but if this were a more general thing capable of dealing with any data, then the programmed loops would be better.

    It would be essential if it were a class where the assignment operator had been specified.

    Besides, you forgot to scale the amount to copy by sizeof(int)
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, always the subtle bugs. Don't have time to test either, right now...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    This is VERY wrong!
    Code:
    				leven += list[j];
    That actually modifies the pointer value!
    To write to the array you should use leven[foo] = bar; where foo is the index and bar is the value.
    Then once you've written to leven[foo] you'll need to add one to foo so that the next item doesn't overwrite it. But if it is not even you don't add one because you wont be writing to array in that case.

    Secondly, if you're going to list pre and post conditions then don't forget the obvious ones like say "list != NULL".

    Thirdly, whilst there are some delete[] statements in your program, you're not doing a very consistent job of properly releasing the memory you obtain in every case, and hence still have leaks. You would be much better off simply using std::vector, since this is C++

    Elysia, In the C++ world we use std::copy, not memcpy.
    Last edited by iMalc; 12-05-2007 at 12:59 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMalc View Post
    Elysia, In the C++ world we use std::copy, not memcpy.
    Didn't realize it was C++. Too much C >_<
    Otherwise I'd suggested using vector a long time ago. Am I half-asleep or something?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    got it. Danced around the answer for a while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM