Thread: functions and arrays

  1. #1
    Unregistered
    Guest

    Question functions and arrays

    I am new to programming and have been giving a question:

    write a function DisplayArray with two parameters-an array of integers and the numnber of elements in the array. The function its to print each element of the array on a seperate line. Write a program to test the fuction.

    Im having a few problems understanding this as i thought in order to use arrays you had to define the size of it at the begging of the code?
    can anyone pleaseee help me understand how to do this!!

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> to define the size of it at the begging of the code?

    True, but think about it, inside your output function, how will you know how many elements were declared?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Unregistered
    Guest
    well thats where my problem is im at a loss how to tackle this problem any hints will be gratefully accepted. I have been learning alot from the help i get on this board :0)

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    void DisplayArray(int Array[],size_t Count)
    {
    int i;
    for(i=0;i<Count;++i)
    {
    /* print Array[i] */
    /* print '\n' */
    }
    }
    Whats so hard about that???
    or this....
    Code:
    int main()
    {
    size_t Size=10;
    int Array[Size]={1,2,3,4,5,6,7,8,9,10};
    DisplayArray(Array,Size);
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    I've never understood the point of those size_t thingies. What is the underlying type of one?

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    unsigned int.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Unregistered
    Guest
    firstly the question doesnt say you have 10 arrays it has a parameter to answer this and secondly that displays them in increases of one what if they were say mixed up numbers?

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> firstly the question doesnt say you have 10 arrays

    ... and SC's solution does not have 10 arrays.

    >>> secondly that displays them in increases of one what if they were say mixed up numbers?

    ... so try it and find out?

    Come on! You ask for hints, you got hints, you've even got the solution, what more is it that you want?!?!?!?!?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You can lead a horse to water but.....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM