Thread: help with storing arrays

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Exclamation help with storing arrays

    not too sure what is going wrong here:

    write a function that removes the odd integer values from the array and stores all remaining even numbers back into the original array. So far, I have:


    Code:
    void oddDel (int x[] );
    
         int i;
         char b[], temp;
    
              for ( i=0; i < x[]; i++)   {
    
                   b = x[i];
                   temp = b;
    
                        if ( temp % 2 == 0)    {
    
                           x[] = temp;
    
                  } 
                  }
                  }

    where am i going wrong???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tcumech
    where am i going wrong???
    In many places. For starters, you want to define a function, but this is only a function declaration:
    Code:
    void oddDel (int x[] );
    A function definition would look like:
    Code:
    void oddDel(int x[])
    {
        /* function body here */
    }
    Next, you need to read up on how to use arrays. Beyond that, what is the idea that you have to solve this problem?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    I guess i forgot to add the open curly bracket:

    Code:
    void oddDel (int x[] )    {
    
         int i;
         char b[], temp;
    
              for ( i=0; i < x[]; i++)   {
    
                   b = x[i];
                   temp = b;
    
                        if ( temp % 2 == 0)    {
    
                           x[] = temp;
    
                  }    //()if
                  }    //()for
                  }    // ()void
    "Beyond that, what is the idea that you have to solve this problem? "

    I think that I have an array of some positive integers, store each indvudal value one at a time into b and then into temp, use the mod operator to discen even values form odd values and store only the even numbers back into the array x[]. Is this not what I have done??

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't do this:
    } //()if
    } //()for
    } // ()void
    If you have to label them to know what they match with then you're doing it horribly wrong. Your function is either too long or your brackets are not lined up properly. In this case it is the later.

    Why do you think that x[] will have anything to do with the length of an array? Did you know that there actually isn't any way for that code to know how long the array is? You actually need to pass that in as well.
    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"

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    the array is a predetermined length. I need not know how long the array is, only to discard all odd values and store all even values back into the original array. Don't attempt to get your rocks off by punching holes in my program, I am simply looking for constructive criticism and a bit of help. If you are replying only to point out minor conflicts in code preference, don't bother replying at all. That is like an adult bullying a kid. I obviously know nothing about c programming and need help getting my program to perform the task at hand, not to learn how to comment.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by tcumech View Post
    the array is a predetermined length.
    Then use that length. HINT: x[] is NOT IT.
    Quote Originally Posted by tcumech View Post
    I need not know how long the array is
    That's probably the stupidest thing you could possibly say. Of course you need to know how long the array is, otherwise you won't know when to stop.

    Quote Originally Posted by tcumech View Post
    I am simply looking for constructive criticism and a bit of help.
    If this is how you react when you get it, then quite frankly that puts the lie to the claim.

    EDIT: And some more random points that you will need to fix if you want this to work: You will have to decide whether your array should be of ints or of chars. What is the purpose of b and temp? What is b doing for you that temp is not doing? What are you supposed to do with all those "empty" spaces (i.e., if you remove five odd values from your array, what should go in those five spaces? Do you zero them out? Do you move things forward to fill in the gaps? If you do fill in the gaps, do you need to keep track of the new "size" of your array?
    Last edited by tabstop; 05-05-2010 at 02:04 PM.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    The array has a zero in it and the function should be ternminated when the 0 is reached...all even values should, i guess, be to the left of the zero.

    nice philosophy...next?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by tcumech View Post
    The array has a zero in it and the function should be ternminated when the 0 is reached...all even values should, i guess, be to the left of the zero.

    nice philosophy...next?
    So why in the name of all that is good aren't you checking for a 0?

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tcumech View Post
    nice philosophy...next?
    If it helps having more people tell you that you "obviously know nothing about c programming" (your own words), then I'm here. No one is trying to be mean, just it gets very tedious having to repeatedly re-assure people about their ego and feelings.

    But you are not totally unjustified in being paranoid either, so let's go thru this line by line.

    Code:
    void oddDel (int x[] )    {
    
         int i;
         char b[], temp;
    It is much more normative to refer to pointers as *x and *b rather than x[] and b[]. And, in fact, "char b[]" should generate a compiler error. What compiler are you using?

    Code:
              for ( i=0; i < x[]; i++)   {
    What on earth is the value of x[] supposed to represent here? I guarantee it is not what you want or think.

    You cannot determine the length of an array in C this way. You need to supply the length of the array, one of two ways:
    Code:
    void oddDel (int x[10]);  // fixed length
    void oddDel (int *x, int len);  //dynamic length
    That's enough. You need to resolve this issue or you are wasting your own and everyone else's time. Don't get upset.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    You cannot determine the length of an array in C this way. You need to supply the length of the array, one of two ways:
    Code:
    void oddDel (int x[10]);  // fixed length
    void oddDel (int *x, int len);  //dynamic length
    You can do it a third way, but it relies on you providing it accurate data. All of the string functions expect you to pass a series of zero or more characters + one character whose value is zero. This is called a nul terminator. Basically, you can find the "end" of an array by setting aside one value that you want to be the "end". Then stop when you find it.

    It doesn't actually tell you the size of the allocated space, so it's still up to you to actually pass it valid data. But that is another way for finding the end of an array. This sounds like it's what the OP is trying to do -- but they're not actually checking for their terminator value.


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

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by quzah View Post
    You can do it a third way [...] This sounds like it's what the OP is trying to do
    You're right, I missed that. However, that only makes this worse:
    Code:
     for ( i=0; i < x[]; i++)   {
    Maybe:
    Code:
     for ( i=0; x[i] != '\0' ; i++)   {
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well considering his first loop won't even compile, I'm not sure how they could possibly think it was right. I hate that people don't look at warnings and errors.


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

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by tcumech View Post
    the array is a predetermined length. I need not know how long the array is, only to discard all odd values and store all even values back into the original array. Don't attempt to get your rocks off by punching holes in my program, I am simply looking for constructive criticism and a bit of help.
    Help is exactly what you got. Sometimes a question is just a question, and isn't meant to be purely rhetorical. If we can understand how your thought processes led to the program that was produced, then we have a better chance of understanding how to help.
    Believe me, I have no intention of simply punching holes in your program. It'd be like a chess expert trying to beat the pants off a one year old who is playing their first game. It's not like it would be much of a challenge.
    We're just doing our best to tell you what you're doing wrong, how to fix it, and what habbits you should change in order to avoid shooting yourself in the foot later.

    It would be very helpful now, and exetremely useful for us and yourself, if you could please give some example input here and example output, that shows how you understand the problem at hand? It also serves as an example that we can relate to in pointing out how to go about the task.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and storing values
    By atlas07 in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2010, 08:46 PM
  2. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  3. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  4. Storing integers from file into arrays???? please help
    By adzza69 in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2004, 12:28 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM

Tags for this Thread