Array troubles

This is a discussion on Array troubles within the C Programming forums, part of the General Programming Boards category; Hi there i'm working on an assignment at the moment now as far as getting a full answer i am ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    36

    Array troubles

    Hi there i'm working on an assignment at the moment now as far as getting a full answer i am not looking for. I would like some hints or maybe a reference to something that i could learn off of to complete a question i'm stuck on.

    a. Write the definition of the function setZero that initializes any one dimensional array of type int to 0.

    Maybe i am not following the question properly, but from my understanding I have a function prototype setZero. That does not necessarily need an input or output.

    It also says any one dimensional array and therefore i would need the user to create or input a value for the number of elements in this 1D array. The initializing to zero is the easy part. Getting a user to be able to create the size of an array through input is the confusing part.

    Heres my general idea.

    Code:
    void setZero(void)
    {
    	int i, size;
    	printf("Enter how many elements you would like in an array");
    	scanf("%d", &size);
    	for(i=0;i<size;i++)
    	{
    		int zeroArray[i] = 0;
    		printf("zeroArray[%d] = %d\n", i, zeroArray[i]);
    		
    	}
    }

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    Why &size? & is the address of the int variable, which you'd use when you want to create a pointer to the variable. You're not using pointers here.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    its how you use the function scanf.

    it takes a variable type %d is an integer and then reads the address of your variable mine was size. Then asks the user to input a value to be assigned in that addresses memory location.

    Other then that any help?
    Last edited by Annihilate; 11-20-2010 at 02:14 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,463
    Code:
    void setZero(void)
    {
    	int i, size;
    	printf("Enter how many elements you would like in an array");
    	scanf("%d", &size);
    	for(i=0;i<size;i++)
    	{
    		int zeroArray[i] = 0;
    		printf("zeroArray[%d] = %d\n", i, zeroArray[i]);
    		
    	}
    }
    Everything up to your scanf is fine. Putting the zeroArray declaration inside the loop body means it's scope (lifespan) is only one iteration of the loop. Every time through the loop, you declare an array of ints of length i, initialize it to zero and printf an "element" that is one past the last actual element in the list (remember, int array[3] has valid indexes of 0..2). It gets destroyed when that iteration of the loop is done.

    What you need to do is malloc enough memory for size ints, and then loop through the array, setting each value to zero. Take a look at this site: Dynamically Allocating Arrays.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,463
    Quote Originally Posted by cnewbie1 View Post
    Why &size? & is the address of the int variable, which you'd use when you want to create a pointer to the variable. You're not using pointers here.
    C normally does a "pass by value", so if he didn't use the &, it would pass whatever garbage value was in size (since it was never initialized) to scanf. In pass by value, the "size" variable in scanf is a copy of the size variable in setZero (there is no actual variable called size, scanf uses variable argument lists, but that's it's own can of worms). Changing that copy wouldn't change the original, so if scanf tried to store the value read, the calling function would never "see" the new value. Thus, you need to use "pass by reference" to make sure that size has the value that was read in after returning from the scanf call, you pass in the address of size. Read up on the subject here: http://www.comp.nus.edu.sg/~esim/faq/node12.html.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Okay so i followed what you were saying. Now this is what i would like to solve.

    Code:
    printf("Enter how many elements you would like in an array");
    	scanf("%d", &size);
    	int zeroArray[size];

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,463
    Maybe i am not following the question properly
    That's okay, I didn't follow the question properly either. Read your problem very carefully:

    a. Write the definition of the function setZero that initializes any one dimensional array of type int to 0.
    You don't need to declare the array or allocate space for it. You want a function that simply takes an array and a length, and sets each element to zero. Something like:
    Code:
    void setZero(int array[], int length)
    {
        int i;
    
        for (i = 0; i < length; i++) {
            // set array[i] to zero
        }
    }
    from my understanding I have a function prototype setZero. That does not necessarily need an input or output.
    A prototype doesn't have a function body. It's used simply to describe the function signature (parameters and return). Your function does need "input" (parameters), but not output (return value). A prototype for your function would look like this:
    Code:
    void setZero(int array[], int length);

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Code:
    void setZero(int array[], int length)
    {
        int i;
    
        for (i = 0; i < length; i++) {
            // set array[i] to zero
        }
    }
    I find this understandable. This was close to what i original had. Although with this function body. On the function call i would need to initialize an array of some variable of unknown size. Such that

    Code:
    zeroArray(anyArray[], size);
    now this looks good. While i put this in the body lets say my array was alpha[20], but i want the size to be 30. The program will allot memory for 30 spaces but my stack will now become corrupt. So i would need the user to input the size of my array such that anyArray[size] where size would be user input. But C will not allow this for the size of the array must already be a set number and not a user input. This is what i am having trouble with.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,463
    So we know you have the right setZero function, but now we need to dynamically allocate space for your array. You can do this outside the function, by using malloc:
    Code:
    int *array = malloc(size * sizeof(int));
    Then pass array into your zeroArray function to initialize it. Remember to use free to free the memory when you're done.

    You can keep your printf and scanf statements to get the requested size for the array, but they belong outsize the setZero function.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Hmm weird i have not been taught malloc in my class yet and i don't believe I was suppose to use it for this assignment. Ill read into the use of and how it works though. Thanks for your help i finished my program a different way.

    This thread can be closed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. alotta Array troubles
    By caroundw5h in forum C Programming
    Replies: 13
    Last Post: 01-02-2004, 05:27 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21