1. ## Replacing Integers

Hey guys, I'm pretty new to C and I've been having some difficulty in trying to write a program that will fill an integer array (of size n) with zeros, but only between a certain range of values in the array (for example, positions 20-25).

This is the general form the function is supposed follow:
Code:
`void zeros(int x[], int n)`
Any help is appreciated!

2. void zeros ( int x[ ], int start, int end )
Maybe?

3. Have you thought of a loop that sets elements start to end to 0?

4. Yes, I have an idea or two... but I'm not sure if this is the right direction

Code:
```void zeros(int x[], int n)
{
int i;

for( i = 0; x[i] != '\0'; ++i)
x[i] = '0';
}```

5. You need two extra arguments to specify the start and end points of the range, something like
Code:
`void zeros(int x[], int n1, int n2)`
and to handle the special case of a range with zero length, n2 should correspond to one-past-the-end. Another choice is to have a function with pointers to the beginning and one-past-the-end of the range, like
Code:
`void zeros(int *startp, int *endp)`
Edit: The second version is more natural since it only requires two arguments and it corresponds to the first version if you call it by
Code:
`void zeros(x+n1, x+n2)`

6. Originally Posted by frodo6
Yes, I have an idea or two... but I'm not sure if this is the right direction

Code:
```void zeros(int x[], int n)
{
int i;

for( i = 0; x[i] != '\0'; ++i)
x[i] = '0';
}```

- Why are you controlling your loop with a check for '\0' on an int array?
- Where are your start and end variables?

Salem gave you a good prototype for your function. See what you can make from it.

7. If you want to modify only a range, why not use a range:

while ((ArrayIndex >= StartingPoint) && (ArrayIndex < EndingPoint))

The loop continues until ArrayIndex is outside of the range specified.

8. ok well i am close, but i am still not getting the 0's in the appropriate positions of my array. here is the code i used..

Code:
```#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void zeros( int x[], int n)
{
int i;
for( i = 0; i < n; ++i)
{
x[i] = -10 + 21*(rand()/(RAND_MAX+1.0));
}
while((i >= 20) && (i < 25))
{
int i;
x[i] = 0;
}
}
void print( int x[], int n)
{
int i;

for( i = 0; i < n; ++i)
printf( " %i", x[i]);
putchar( '\n');
}
#define N 30
int main( void)
{
int y[N];
zeros(y,N);
print(y,N);
return 0;
}```
can anyone tell me what i did wrong?

9. You're missing the whole point. You CAN'T do what you're trying to do with the function arguments you're using. Either you add an extra integer argument, or you use two pointer arguments like I suggested. The function can't work if you don't pass it the information it needs.