Thread: Hello guys, I have a question regarding Arrays

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    196

    Hello guys, I have a question regarding Arrays

    Is it better to use
    #define at the top or should I just use

    Code:
    int myarray[10]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
    The problem when I go this route is that I get "Array not initialized or to long" something along those lines.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Definitely better to #define.

    Code:
    // To increase the array size, I'd have to make three separate changes.
    // Imagine what it's like when that value is used in several different
    // functions, and/or throughout several different source files.
    
    #include <stdio.h>
    
    int main(void)
    {
        int array[10];
        int i;
    
        for(i=0; i<10; i++)
            array[i] = i+1;
    
        for(i=0; i<10; i++)
            printf("%d\n",array[i]);
    
        return 0;
    }
    Code:
    // To increase the array size, I just change one value - the #define
    
    #include <stdio.h>
    
    #define MAX_LEN 10
    
    int main(void)
    {
        int array[MAX_LEN];
        int i;
    
        for(i=0; i<MAX_LEN; i++)
            array[i] = i+1;
    
        for(i=0; i<MAX_LEN; i++)
            printf("%d\n",array[i]);
    
        return 0;
    }
    If you could post a snippet of code, along with the exact warning/error messages you're seeing, perhaps we can figure out the problem you're running into.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by Matticus View Post
    Definitely better to #define.

    Code:
    // To increase the array size, I'd have to make three separate changes.
    // Imagine what it's like when that value is used in several different
    // functions, and/or throughout several different source files.
    
    #include <stdio.h>
    
    int main(void)
    {
        int array[10];
        int i;
    
        for(i=0; i<10; i++)
            array[i] = i+1;
    
        for(i=0; i<10; i++)
            printf("%d\n",array[i]);
    
        return 0;
    }
    Code:
    // To increase the array size, I just change one value - the #define
    
    #include <stdio.h>
    
    #define MAX_LEN 10
    
    int main(void)
    {
        int array[MAX_LEN];
        int i;
    
        for(i=0; i<MAX_LEN; i++)
            array[i] = i+1;
    
        for(i=0; i<MAX_LEN; i++)
            printf("%d\n",array[i]);
    
        return 0;
    }
    If you could post a snippet of code, along with the exact warning/error messages you're seeing, perhaps we can figure out the problem you're running into.
    Well this are the instructions to my assignment
    Hello guys, I have a question regarding Arrays-aaaa-png

    I tried using the way my teacher notes showed which included

    Code:
    int array[10]= { 1, 2 ...}
    int array[12]= { 1, 2 ... 12}
    but that never work and then I found the define function and that solved my problem

    Anyways this is my code.. The teacher said to use A, B, C as our integer names but I learned from here to use more meaningful names..hopefully I don't get knocked off points for using more then the 3 he said or for not using the names he asked for..

    Code:
    #include <stdio.h>
    #define SIZE 10
    #define SIZE1 12
    
    
    int main()
    {
        int i;
        int j;
        int array1;
        int array2;
        int myArray[SIZE];
        int myArray2[SIZE1];
        int intersection[SIZE];
        int match;
        for (i=0;i<SIZE;i++)
        {
            printf("Enter number: ");
            scanf("%d",&myArray[i]);
        }
                for(i=0;i<SIZE;i++)
                {
    
    
                printf("%d\n",myArray[i]);
                }
        for (i=0;i<SIZE1;i++)
        {
            printf("Enter number: ");
            scanf("%d",&myArray2[i]);
        }
                for(i=0;i<SIZE1;i++)
                {
                printf("%d\n",myArray2[i]);
                }
        match = 0;
    for(i=0; i<12; i++)
    {
        for(j=0; j<12; j++)
        {
            if (myArray[i] == myArray2[j])
            {
                intersection[match] = myArray[i];
                match++;
            }
    
    
        }
    }
    
    
        if (match>0)
        {
            printf("The intersection is: ");
            for(j=0; j<match; j++)
            printf("%d ",intersection[j]);
        }
        else
            printf("There is no intersection\n");
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    I also just realized I didn't use counter...damn.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Oh and here is to proof that it works
    Hello guys, I have a question regarding Arrays-see-png

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Also I have a very, very nooby question, I tend to think that my indentation is not the best. How do you guys indent one the source code starts to get huge?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Cdd101 View Post
    Also I have a very, very nooby question, I tend to think that my indentation is not the best. How do you guys indent one the source code starts to get huge?
    I'm working on a reply to your previous post - I mention indentation.

    Simple rule of thumb: every time you have code after an open brace '{', advance one tab stop. Every time you have a closing brace '}', go back one tab stop. Sometimes braces are optional - in that case, every time you have a line of code that depends on a control statement above it, indent that line of code.

    Code:
    //----------------
    if(x == y)
    {
        code 1;    // indent one level (underneath "if")
        code 2;    // indent one level (underneath "if")
    }
    
    //----------------
    if(a == b)
        code 1;    // indent one level (underneath "if")
    
    //----------------
    for(i=0; i<10; i++)
    {
        code 1;     // indent one level (underneath "for")
        code 2;     // indent one level (underneath "for")
        if(i < 6)   // indent one level (underneath "for")
            code 3;     // indent two levels (underneath "if", which is underneath "for")
        code 4;     // indent one level (underneath "for")
    }
    This might not be applicable for every situation, but it's a good method to generally follow.

    More here: SourceForge.net: Indentation - cpwiki

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You have two defines of 10 and 12; I suggest using them.

    Code:
    for(i=0; i<12; i++)
    {
        for(j=0; j<12; j++)
        {
            if (myArray[i] == myArray2[j])
            {
                intersection[match] = myArray[i];
                match++;
            }
     
     
        }
    }
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The teacher said to use A, B, C as our integer names but I learned from here to use more meaningful names
    This makes me very happy.

    I made a few notes for you, and fixed the indentation - notice how it's easier to follow with the eyes.

    Code:
    #include <stdio.h>
    #define SIZE 10
    #define SIZE1 12
    
    int main()
    {
        int i;
        int j;
        int array1;
        int array2;
        int myArray[SIZE];
        int myArray2[SIZE1];
        int intersection[SIZE];
        int match;
    
        for (i=0;i<SIZE;i++)
        {
            printf("Enter number: ");
            scanf("%d",&myArray[i]);
        }
    
        for(i=0;i<SIZE;i++)
        {
            printf("%d\n",myArray[i]);
        }
    
        for (i=0;i<SIZE1;i++)
        {
            printf("Enter number: ");
            scanf("%d",&myArray2[i]);
        }
    
        for(i=0;i<SIZE1;i++)
        {
            printf("%d\n",myArray2[i]);
        }
    
        match = 0;
    
        // Instead of the "magic number" 12, you should use your defined constant SIZE here.
        // Moreover, "i" controls "myArray", which only has 10 elements, so by using 12,
        // you're overrunning the bounds of your array
        for(i=0; i<12; i++)
        {
            // and here, you should be using SIZE1 instead of the magic number 12
            for(j=0; j<12; j++)
            {
                if (myArray[i] == myArray2[j])
                {
                    intersection[match] = myArray[i];
                    match++;
                }
            }
        }
    
        if (match>0)
        {
            printf("The intersection is: ");
            for(j=0; j<match; j++)
            printf("%d ",intersection[j]);
        }
        else
            printf("There is no intersection\n");
    
        return 0;
    }
    There is one criteria of the assignment that is missing: "Repeated elements need to be counted only once. To do so, always check in C if the element to be inserted is already present."

    Code:
    Enter number: 1
    Enter number: 2
    Enter number: 3
    Enter number: 4
    Enter number: 5
    Enter number: 6
    Enter number: 7
    Enter number: 8
    Enter number: 9
    Enter number: 10
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Enter number: 1
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    1
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    The intersection is: 1 2 2 2 2 2 2 2 2 2 2 2 2

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Well I did some changes on my code simply since I found some dead weight int on it.

    Code:
    Code:
    #include <stdio.h>#define SIZE 10
    #define SIZE1 12
    
    
    int main()
    {
        int i;
        int j;
        int A[SIZE];
        int B[SIZE1];
        int intersection[SIZE];
        int counter=0;
    
    
        for (i=0;i<SIZE;i++)
        {
            printf("Enter number: ");
            scanf("%d",&A[i]);
        }
                for(i=0;i<SIZE;i++)
                {
    
    
                printf("%d\n",A[i]);
                }
        for (i=0;i<SIZE1;i++)
        {
            printf("Enter number: ");
            scanf("%d",&B[i]);
        }
                for(i=0;i<SIZE1;i++)
                {
                printf("%d\n",B[i]);
                }
    
    
        for(i=0; i<12; i++)
        {
            for(j=0; j<12; j++)
            {
                if (A[i] == B[j])
                {
                    intersection[counter] = A[i];
                    counter++;
                }
            }
        }
        if (counter>0)
            {
                printf("The intersection is: ");
            for(j=0; j<counter; j++)
                printf("%d ",intersection[j]);
            }
        else
            printf("There is no intersection\n");
        return 0;
    }
    like

    Code:
    int array1
    int array2
    Also regarding matrixes, which is what my next assignment is over. Are there any recommended reads over that? I tend to not be able to understand my teachers notes very well.

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by Matticus View Post
    This makes me very happy.

    I made a few notes for you, and fixed the indentation - notice how it's easier to follow with the eyes.

    Code:
    #include <stdio.h>
    #define SIZE 10
    #define SIZE1 12
    
    int main()
    {
        int i;
        int j;
        int array1;
        int array2;
        int myArray[SIZE];
        int myArray2[SIZE1];
        int intersection[SIZE];
        int match;
    
        for (i=0;i<SIZE;i++)
        {
            printf("Enter number: ");
            scanf("%d",&myArray[i]);
        }
    
        for(i=0;i<SIZE;i++)
        {
            printf("%d\n",myArray[i]);
        }
    
        for (i=0;i<SIZE1;i++)
        {
            printf("Enter number: ");
            scanf("%d",&myArray2[i]);
        }
    
        for(i=0;i<SIZE1;i++)
        {
            printf("%d\n",myArray2[i]);
        }
    
        match = 0;
    
        // Instead of the "magic number" 12, you should use your defined constant SIZE here.
        // Moreover, "i" controls "myArray", which only has 10 elements, so by using 12,
        // you're overrunning the bounds of your array
        for(i=0; i<12; i++)
        {
            // and here, you should be using SIZE1 instead of the magic number 12
            for(j=0; j<12; j++)
            {
                if (myArray[i] == myArray2[j])
                {
                    intersection[match] = myArray[i];
                    match++;
                }
            }
        }
    
        if (match>0)
        {
            printf("The intersection is: ");
            for(j=0; j<match; j++)
            printf("%d ",intersection[j]);
        }
        else
            printf("There is no intersection\n");
    
        return 0;
    }
    There is one criteria of the assignment that is missing: "Repeated elements need to be counted only once. To do so, always check in C if the element to be inserted is already present."

    Code:
    Enter number: 1
    Enter number: 2
    Enter number: 3
    Enter number: 4
    Enter number: 5
    Enter number: 6
    Enter number: 7
    Enter number: 8
    Enter number: 9
    Enter number: 10
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Enter number: 1
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    Enter number: 2
    1
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    2
    The intersection is: 1 2 2 2 2 2 2 2 2 2 2 2 2
    Thanks pal! So I did the changes to the names simply because the teacher is very picky regarding the naming. He wants us to use what he says so I tend to use real names or more useful names until it's time to turn it in.

    Code:
    #include <stdio.h>#define SIZE 10
    #define SIZE1 12
    
    
    int main()
    {
        int i;
        int j;
        int A[SIZE];
        int B[SIZE1];
        int intersection[SIZE];
        int counter=0;
    
    
        for (i=0;i<SIZE;i++)
        {
            printf("Enter number: ");
            scanf("%d",&A[i]);
        }
                for(i=0;i<SIZE;i++)
                {
    
    
                printf("%d\n",A[i]);
                }
        for (i=0;i<SIZE1;i++)
        {
            printf("Enter number: ");
            scanf("%d",&B[i]);
        }
                for(i=0;i<SIZE1;i++)
                {
                printf("%d\n",B[i]);
                }
    
    
        for(i=0; i<SIZE; i++)
        {
            for(j=0; j<SIZE1; j++)
            {
                if (A[i] == B[j])
                {
                    intersection[counter] = A[i];
                    counter++;
                }
            }
        }
        if (counter>0)
            {
                printf("The intersection is: ");
            for(j=0; j<counter; j++)
                printf("%d ",intersection[j]);
            }
        else
            printf("There is no intersection\n");
        return 0;
    }
    Is that the only change you guys feel I should do to my assignment? I tend to ask questions on here a lot and this time I was determined to finish the assignment before asking on here.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Cdd101 View Post
    Also regarding matrixes, which is what my next assignment is over. Are there any recommended reads over that? I tend to not be able to understand my teachers notes very well.
    Read up on Multiple or Two Dimension arrays.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by stahta01 View Post
    Read up on Multiple or Two Dimension arrays.

    Tim S.
    Thank you so much for your help pal.

  14. #14
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Huh after I change I did, using Size and Size 1 it now crashes at the end.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can use:

    Code:
    #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
    It will work in the scope of where the array was defined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys, back again with a question
    By Velocity in forum C++ Programming
    Replies: 10
    Last Post: 10-19-2008, 02:27 PM
  2. hai guys help on 2-dim arrays
    By zerlok in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 12:58 PM
  3. OK. new easy question for you guys.
    By arnis in forum C++ Programming
    Replies: 8
    Last Post: 07-17-2003, 09:52 PM
  4. Ok. sorry to bother you guys with a newb question.
    By arnis in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2003, 11:23 AM
  5. Need to ask you guys a Question....
    By Halo in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-03-2003, 01:38 AM