Thread: Simply filling 1D array...help

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    55

    Simply filling 1D array...help

    Hi
    I simply want to learn about arrays in C, but am stuck trying to fill a 1D array size 100, with integers 1-100 (or 0-99, I'm not fussy!) and print the result. I know I need a loop to work on arrays, and
    I want to learn C but despair that I get stuck with a simple problem like this. I thought I would know how to do this but I still am missing a fundamental concept if I cannot get this right.
    Here is my code attempt:
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int a;
        int numarray[100];
        printf("\nProgram to fill 1D array with integers from 1-100 \n\n");
        for (a=1;a<=100;a++)
        {
            printf(" %d\n", numarray[a]);
        }
        printf("\nBut this program does not work..why am I so bad at C programming I can't see how to figure this out?! help.... \n\n");
        return 0;
    }
    I'm grateful for helpful replies, thanks

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    55
    OK.. I think I've figured it out. I need a (for) loop to fill the array & a (for) loop to print the array. So 2 for loops do the job I want eg:

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int a;
        int numarray[100];
        printf("\nProgram to fill 1D array with integers from 1-100 \n\n");
        for (a=1;a<=100;a++)
        {
            numarray[a]=a;
        }
         for (a=1;a<=100;a++)
            printf(" %d\n", numarray[a]);
        
        printf("\nBut this program did not work..(see code above)\nwhy am I so bad at C programming I can't see how to figure this out?! help.... \n\n");
        
        printf("\nGOT IT WORKING - expression to fill array and with for loop & second for loop to print it out... \n\n");
        return 0;
    }
    This does what I want/need for now but if anyone has a better way or can offer any other help with this please feel free to comment.
    Thanks & best wishes
    Last edited by richardpd; 01-23-2013 at 06:17 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (a=1;a<=100;a++)
    Yeah, arrays start at 0, not 1

    So it's
    for (a=0;a<100;a++)
    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.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The indexes of the array are in range : 0..N-1 , where N is the size of the array.

    Very common mistake. So, you case resulted in : veryCommonMistake++; (kidding ).

    So you need your loop to run from 0 until 99, that is
    Code:
    for(a = 0 ; a < 100 ; a++)
    ...
    Also, usually we use the variable i to run loops (instead of a), but this of course it's not an error.

    I would fill up the array like you, with a for loop (corrected).
    However, you can initialize an array in the same line you declare it. Especially useful when you want to set all the elements of it to zero.

    Example
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            /* No need to write 10 zero'es */
            /* They will be initialized to zero
             * automatically.               */
            int array[10] = {0};
            /* What are the rest elements of test array? */
            int test[10] = {1};
            int i;
    
            for(i = 0 ; i < 10 ; i++)
                    printf("array[%d] = %d\n", i, array[i]);
            for(i = 0 ; i < 10 ; i++)
                    printf("test[%d] = %d\n", i, test[i]);
    
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Just to add one more way, static arrays are filled with 0 by default

    Code:
    static int foo[5];
    int main(void)
    {
        for (int i=0; i < 5; i++) {
             printf("%d", foo[i]);
        }
        printf("\n");
        return 0;
    }
    Output: 00000

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Very true, but even though you use the static keyword to limit visibility, I would for sure not suggest it. When your file grows bigger and bigger, this approach is prone to conflicts. On the other hand it is nice to keep the size of the files as small as possible for re-usability. Of course, the ideal think is to keep them balanced.

    So, on one hand you have the way with the
    Code:
    static int foo[500];
    , which is prone to conflicts and on the other side, you have this
    Code:
    int foo[500] = {0};
    , which needs 4 more characters and two whitespaces to be written in comparison with the former. But, this is much more safe in my mind.

    So, I would strongly suggest you not to use the static approach.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I agree: if the intention is to zero initialise an array, then using the static keyword is the wrong tool for the job, unless you actually do need what it provides.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array filling
    By martynasj in forum C Programming
    Replies: 6
    Last Post: 11-16-2012, 04:14 PM
  2. filling an array..
    By kromagnon in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 06:53 PM
  3. nebie simply needs help creating 2 dimensional char array
    By MegaManZZ in forum C++ Programming
    Replies: 13
    Last Post: 01-14-2008, 10:13 AM
  4. Simply initializing an array AFTER defining it
    By fd9 in forum C++ Programming
    Replies: 7
    Last Post: 12-02-2005, 11:09 PM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 03:11 PM