Thread: Data into an Array

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    10

    Data into an Array

    I am writing a programme at the moment where i need to
    calculate a range of values and put them into an array. however the values are all co-ordinates (X&Y) for a curve and so i am using
    a loop to produce the values so i cant just enter them all in one
    go, i would have to enter them bit by bit.
    IS this right and if so, how do i do it, if not and theres a much simpler way, what is it?!?
    Thanks in advance.


    Toph

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seems like a loop is a reasonable way to go.
    Give it a shot and see what happens
    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
    Registered User
    Join Date
    Jan 2004
    Posts
    10
    ok, so how do i go about doing that?
    Arrays are something which ive never quite grasped! could you
    give me an example or direct me to a tutorial or something?

    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    An array is a collection of data of a given type. Each element in the array is at a given index. An array element is the individual data segment or piece in the given array. An array index is the position in the array that a given element is at. Think of houses on a street. Each one is at a specific spot on the street. Thus, we could index them, or give them a number so we can remember where they are. Indexes start at 0 and go through the size of the array minus one.

    Here is an example:
    Code:
    #include <stdio.h>
    int main( void )
    {
        int houses[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int myhouse = 3;
    
        printf("My house is at index 3. So...\n");
        printf("houses[3] contains the value %d", houses[myhouse] );
    
        return 0;
    }
    What do you think the value you get for houses[3]?
    Were you right?

    I'll leave working with loops as an exercise to the reader. But there is a brief example of how arrays work.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  3. for loop with sort issues
    By redmondtab in forum C Programming
    Replies: 10
    Last Post: 10-09-2006, 10:36 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM