Thread: Replacing Integers

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    void zeros ( int x[ ], int start, int end )
    Maybe?
    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.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Have you thought of a loop that sets elements start to end to 0?
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    3
    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. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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)
    Last edited by robatino; 05-03-2007 at 02:56 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by frodo6 View Post
    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';
    }
    Things to ask yourself:

    - 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.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    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. #8
    Registered User
    Join Date
    May 2007
    Posts
    3
    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. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Pass by reference
    By jrice528 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2007, 01:02 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM