# help with storing arrays

• 05-05-2010
tcumech
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???
• 05-05-2010
laserlight
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?
• 05-05-2010
tcumech
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??
• 05-05-2010
iMalc
Don't do this:
Quote:

} //()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.
• 05-05-2010
tcumech
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.
• 05-05-2010
tabstop
Quote:

Originally Posted by tcumech
the array is a predetermined length.

Then use that length. HINT: x[] is NOT IT.
Quote:

Originally Posted by tcumech
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
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?
• 05-05-2010
tcumech
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?
• 05-05-2010
tabstop
Quote:

Originally Posted by tcumech
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?
• 05-05-2010
MK27
Quote:

Originally Posted by tcumech
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.
• 05-05-2010
quzah
Quote:

Originally Posted by MK27
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.
• 05-05-2010
MK27
Quote:

Originally Posted by quzah
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++)  {`
• 05-05-2010
quzah
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.
• 05-06-2010
iMalc
Quote:

Originally Posted by tcumech
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.