Thread: Need some help with arrays!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    Need some help with arrays!

    Hi guys,

    I am currently working on a c program, that involves arrays. So far, I have definted the array as follows:

    Code:
    #define N 999
    
    
    float info[N];
    
    int main()
    {
         FILE* data;
         printf ("Enter the address of the data file:  \n");
         scanf ("%s", experimentdata);
         data = fopen(experimentdata, "r");
    
        for (count=0; count<=N; count++)
        {
            fscanf(data, "%f", &info[count][1]);
        }
    There involves a file input as well, but my question is is there a way to define the size of the array after scanning the file instead of manually declaring it in the beginning? For example, we have 1000 value text files to work with, but what if the file was 999 or 1001? I'm presuming it has something to do with scanning the file, and then somehow putting it in place of N? Thanks for the help in advance.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You'll have to read the file in little chunks and expand the storage bin basically.

    You would manage the size of the array yourself using malloc and realloc. There isn't anything too different about N, except that it will now be a variable instead of a constant.

    Be sure that you don't access an element that doesn't exist though.

    for ( count = 0; count < N; count++ )

    That is the convention.
    Last edited by whiteflags; 04-24-2008 at 09:16 PM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Sorry, I am fairly new to C, could you explain a bit more regarding the changing of N from a constant to a variable? Also, is it possible to change the read line from a count function to one that also varies?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's very ok to make the array bigger than you need for the data. We aren't in the days of 640K maximum user memory, here.

    When you create the array, you can also give each element a value, very easily:

    Code:
    int array[N] = { 0 };
    
    and then read in the data with a while loop:
    
    int i = 0;
    while(array[i++])  {
       //add your processing code, here.
    
    }
    The while loop ensures that only your data will be processed. Note that it will need changing if your data may contain a zero value (perhaps replace the zero's with -1?).

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Perhaps a short example would help. Lets pretend that I wanted to read a file very similar to yours. I would do a few things:

    I would need to declare storage space. An array is a perfectly good structure, but since we don't know how much items we will have, and because arrays aren't resizeable, we will have to simulate our array.

    Simulating an array is something that a pointer is very good at doing. So to start with you will need to declare some space, perhaps
    Code:
    /** This function returns the result of a malloc call for an array of
     ** this_many floats.  Not recommended for everyday use.
    **/
    float * make array ( int this_many )
    {
       float * acquired = malloc( sizeof *acquired * this_many );
       return acquired;
    }
    And then you start reading your numbers until you're about to run out of array space. This is why it is important to keep track of the array size and why the size must not be constant: At that point, you're going to have to ask realloc to make a bigger array like
    Code:
    /** 
     ** This function attempts to reallocate more space and points a pointer
     ** at it if possible.  If allocation fails, the pointer and the size remain
     ** unchanged.
     ** If the received size (old_space) is <= zero, four items will be allocated.
     ** This function doesn't handle bad pointers with care.
     **
     ** Returns the size of the array.
     ** Not recommended for everyday use.
     **/
    int dbl_space ( float ** that, const int old_space ) 
    {
        int amount = old_space > 0 ? old_space * 2 : 4;
        /** Realloc needs two things to work, the old pointer and the new, expected size.
         ** Point a pointer to the data that realloc returns.
         **/
        float * acquired = realloc( *that, sizeof(float) * amount );
        if ( acquired != NULL ) {
            /** This way if it succeeds you can assign your new storage room without a
             ** much trouble.
             **/
            *that = acquired;
        } else {
            /** Or do whatever may be appropriate in the opposite case, like exiting. **/
            return old_space;
        }
        return amount;
    }
    The only way you would successfully make an array bigger is if you knew what the old size was, because the new size you pass to realloc is a calculation that you would need to make.

    Once you've finished reading from the file, it will be your responsibility to use the array properly. Then when you are done with the array, free( ) it so that you don't experience a memory leak. This is a very important step that people can forget to do. Memory leaks can result in overconsumption of your computer's memory due to the fact that the substantial space that the array took up won't be returned to the OS for recycling. This can have a lasting effect on the performance of the user's machine and force a reboot.

    I may show my example program later, but hopefully you'll find a solution yourself now.
    Last edited by whiteflags; 04-24-2008 at 11:14 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM