Thread: Passing arrays

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    19

    Passing arrays

    Hey all, I'm at the part of my course where I am being taught arrays, and passing them to functions. I have been asked to produce a program that has 2 arrays that the user enters 10 numbers in and then gives the total for each array.

    So far i've come up with this :
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define NUMBER_ELEMENTS 10
    
    void f_enter ();
    void f_display ();
    
    void main () 
    {
    
    float number[NUMBER_ELEMENTS];
    
    f_enter (number, NUMBER_ELEMENTS);
    f_display (number, NUMBER_ELEMENTS) ;
    
    exit(0);
    }
    
    void f_enter (int input[], int size)
    {
    	int i = 0 ;
    
    	for (i = 0; i < 10; i ++)
    	{
    		printf("Enter number %d for first array: ", i + 1);
    		scanf("%d", &input[i]);
    		fflush(stdin);
    	}
    }
    
    void f_display (int display[], int number_elements)
    {
    	int i = 0, total = 0 ;
    
    	for (i = 0; i < 10; i ++)
    	{
    		total = total + display[i];
    	}
    
    	printf("total is %d ", total);
    }
    This does it for one array, but I really just copied some other code from a book that did something similar and it somehow works. I really don't see how the array is being passed and used in the display function, or how I would do this for a second array.

    Is there anyone who can help explain whats going on in the code and how its being passed between f_enter and f_display.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void f_enter (int input[], int size)
    {
    	int i = 0 ;
    
    	for (i = 0; i < 10; i ++)
    What's the point of passing size if you don't use it? The same goes for your other function. Also: 'fflush( stdin )' is undefined behavior, and 'void main' is incorrect. Also, your function prototypes should match your function definitions.


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

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    19
    Well done, you really helped me with the problem there. And since I am a total beginner and already explained I simply copied sections of code to try and make a working program (which it does work) I know there are some parts that aren't needed or right... but i got it working, when I have no clue how passing arrays work.

    and what do you mean about fflush, I was mearly clearing the buffer.
    Last edited by renvaar; 11-19-2009 at 05:59 PM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by renvaar View Post
    what do you mean about fflush, I was mearly clearing the buffer.
    Cprogramming.com FAQ > Why fflush(stdin) is wrong

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    19
    Well thats what my programming teacher tells me to do, and so I'd rather do what she says and pass the course than refuse to do it and fail.

    And great guys, so far 2 posts nit picking at code written by a beginner, and no help what at all about what I was asking.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by renvaar View Post
    Well done, you really helped me with the problem there.
    I quoted the section of code that specifically showed you what was wrong. Here, since you're being a grump, let's add some color:
    Code:
    void f_enter (int input[], int size)
    {
    	int i = 0 ;
    
    	for (i = 0; i < 10; i ++)
    Why are you using 10, when you should be using size? That's my point. You need to look at each function as its own little world. It only knows about what's between the opening { and the closing }. The exception is global variables, and functions which it knows about ahead of time. But, you need to look at your function and think "why does this function know what 10 is"? ... It doesn't. It shouldn't rather. You should instead be using size, since that's what your function is supposed to be knowing about:
    Code:
    f_enter( somearray, thatarraysize );
    See? Your function shouldn't know, without using 'thatarraysize', what the real size of the array is. You shouldn't hard code that in, because what happens if 'thatarraysize' is bigger or smaller than 10? Your program won't work right.

    PS: Don't be such a whiner.


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

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    19
    I wasn't whining, but you are telling me about something I know is wrong, and changed shortly after posting the code. And ignoring what I actually need help with, which is understanding whats going on. As a beginner I want to understand the program, not the tiny little mistakes I make as a beginner (but leave the program still working)

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by renvaar View Post
    Hey all, I'm at the part of my course where I am being taught arrays, and passing them to functions. I have been asked to produce a program that has 2 arrays that the user enters 10 numbers in and then gives the total for each array.

    So far i've come up with this :
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    #define NUMBER_ELEMENTS 10
    
    void f_enter ();
    void f_display ();
    
    void main () 
    {
    
    float number[NUMBER_ELEMENTS];
    
    f_enter (number, NUMBER_ELEMENTS);
    f_display (number, NUMBER_ELEMENTS) ;
    
    exit(0);
    }
    
    void f_enter (int input[], int size)
    {
    	int i = 0 ;
    
    	for (i = 0; i < 10; i ++)
    	{
    		printf("Enter number %d for first array: ", i + 1);
    		scanf("%d", &input[i]);
    		fflush(stdin);
    	}
    }
    
    void f_display (int display[], int number_elements)
    {
    	int i = 0, total = 0 ;
    
    	for (i = 0; i < 10; i ++)
    	{
    		total = total + display[i];
    	}
    
    	printf("total is %d ", total);
    }
    This does it for one array, but I really just copied some other code from a book that did something similar and it somehow works. I really don't see how the array is being passed and used in the display function, or how I would do this for a second array.

    Is there anyone who can help explain whats going on in the code and how its being passed between f_enter and f_display.

    Renvaar, I'll just ask you to recall that the nicest coaches, are not the best coaches. Sometimes being "stood tall", is a very good thing - although it may not feel like it at that exact moment.

    Arrays are continuous memory addresses. The name of the array always gives us the address of the first element of the array - number has exactly the same address as number[0], and that address can not be changed.

    Now, for a function to work on the array, it has to have access to the array - that's clear, right? The mechanic can't fix your car, until he gets together with your car, somehow.

    Instead of moving every array (and many times arrays are very large), into each function that needs it, we can just send the function the address of the array - it's very efficient.

    Whenever your program calls a function, it can send several parameters to that function. In your case, the program sends the name of the array "number", which is the address of the base of the array.

    An extra twist is that every function parameter can be given an alias. It's like you joined the French Foreign Legion - you can change your name right away. That's what your program does in f_enter(). It's the same array, the same address as number[], but it has an alias for a name.

    Which will make no sense to you now, but for debugging big programs, it's *VERY* helpful.

    The squared brackets after the array name, are OK because it's the only pair of brackets for that array. If number was a 2 dimension array: number[][], then the compiler would throw an error.

    If you want to see that number is really just an address being passed, replace int number[], with int *number (in the parameter list), and see if it compiles OK or not. (it will).

    In this function you have something we don't like - a "magic number". Sometimes they're OK, but usually, they goof up a program, as soon as you change something. Here, looking at the code, I'd see the define statement at the top of the code, and say "OK, I need to change the define to 20, and all will be well.

    But it won't work right, now! Because of the magic number 10, in this function:

    Code:
    void f_enter (int input[], int size)
    {
    	int i = 0 ;
    
    	for (i = 0; i < 10; i ++)
    	{
    		printf("Enter number %d for first array: ", i + 1);
    		scanf("%d", &input[i]);
    		fflush(stdin);
    	}
    }
    Where did this 10 come from? It just arrived out of thin air, like magic!

    What we want is i < NUMBER_ELEMENTS, instead of i < magic number 10, or i < size, but NOT i < 10.

    Having said that, I use magic numbers in programs, myself. Like right now, I'm programming a chess problem, and I use 64 for the number of squares, frequently. But I'm pretty certain that the classic chessboard will continue to have 64 squares for another few hundred years, see?

    Numbers that are subject to change, should never be magic numbers.

    fflush(stdin), will let you down - even my old turbo C, which was made to use fflush(stdin), very well, doesn't handle it correctly. Since scanf() skips over whitespace in the input stream (the keyboard buffer in this case), you shouldn't need it, anyway.

    And yes, int main() is correct, and we strongly advise you to use it, always. It may not mean anything to you now, but later, if you continue to program, it will - and you'll be glad you learned good programming habits, right from the start.
    Last edited by Adak; 11-19-2009 at 07:19 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    What we want is i < NUMBER_ELEMENTS, instead of i < magic number 10, or i < size, but NOT i < 10.
    You don't even want that. You want i < size, because there's no point in having an argument to a function if you never use that argument. You passed it the size, so you should be using that size to control your array access.

    Anyway, the real question was never clear. You apparently (OP) have a book, so read the section on arrays again. Not the part where they just start using them, but the part where they actually tell you what they are.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  4. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM