Why won't this code compile??????

This is a discussion on Why won't this code compile?????? within the C Programming forums, part of the General Programming Boards category; I have no clue why my code won't compile....I am trying to use 2d arrays to read in 20 numbers ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    11

    Why won't this code compile??????

    I have no clue why my code won't compile....I am trying to use 2d arrays to read in 20 numbers in pairs and have them print out each pair in a new line....

    Code:
    #include <stdio.h>
    int main (int argc, char *argv[]) 
    {
    
       int array[10][2];
       int i=0;
       int j=0;
       
       printf("Enter 10 integers: \n");
       
       for(i=0; i<10; i++)
       {
          scanf("%d", &array[i]);
       for(j=0;j<2;j++)
       {
          scanf("%d", &array[j]);
       }
       }
       
       for(i=0;i<10;i++)
       {
          printf("%d", array[i]);
       for(j=0;j<2;j++)
       {
          printf("%d\n", array[j]);
       }
       }
    
    return 0;
    }
    I know that a nestled loop is needed but i am not totally sure that i am doing this properly...
    Any help is appreciated...

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by comp1911 View Post
    I have no clue why my code won't compile....
    So the compiler just says "sorry, I won't compile this code, no idea why?". I would try a different compiler -- at least one that generates more informative errors, and maybe even line numbers with them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by MK27 View Post
    So the compiler just says "sorry, I won't compile this code, no idea why?"
    Lolz.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    As hinted at, if you know there is a problem, which is certainly the case when it doesn't compile, then the compiler tells you the problem. So, tell us what the problem is, or what the compiler says the problem is. We're already here to help, and it only makes it harder if you make us find the problem and solution.

    For one thing, you have a 2D array, but you're treating it as a 1D array.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    This is what i get when i compile it
    Code:
     gcc -Wall -Werror -o sort2d sort2d.c
    cc1: warnings being treated as errors
    sort2d.c: In function ‘main’:
    sort2d.c:13: error: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int (*)[2]’
    sort2d.c:16: error: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int (*)[2]’
    sort2d.c:22: error: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    sort2d.c:25: error: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    im not quite sure what this means

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, a 2D array has rows and columns. Which row and column might this refer to:
    Code:
    &array[i]
    I see a 1D array, or else a pointer to an entire row of a 2D array. Not something that you would put a single digit into.

    I am also sure indenting your nested loop better will help you conceptualize:
    Code:
       for(i=0; i<10; i++)
       {
            scanf("%d", &array[i]);
            for(j=0;j<2;j++)
            {
                   scanf("%d", &array[j]);
            }
       }
    Last edited by MK27; 04-25-2010 at 08:11 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    I worked around with my code and i changed my nestled loop a bit (i placed it right after the first loop) and this is what i ended up with...
    Code:
    #include <stdio.h>
    int main (int argc, char *argv[]) 
    {
    
       int array[10][2];
       int i=0;
       int j=0;
       
       printf("Enter 20 integers: \n");
       
       for(i=0; i<10; i++){
          for(j=0;j<2;j++) /* I placed the second loop right after the first one */
       {
             scanf("%d", &array[i][j]);
           }
       }
       
       for(i=0;i<10;i++){
          for(j=0;j<2;j++)   {
             printf("%d \n", array[i][j]);
           }
       }
    
    return 0;
    }
    This program compiled and worked. However after entering 20 integers it just printed out 20 numbers on a new line for each number. That is not what i had wanted. I wanted the program to print out the first pair of numbers and then the second pair on the second line..
    Eg Enter 20 numbers:
    12 43 23 54 75 34..................................
    Result should look like:
    12 43
    23 54
    75 34
    ..
    ..
    Is it because i am still not using my arrays properly????

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well you are printing each element on a new line. "\n" in your printf. What you want is probably to print a space between the elements in that inner for printf statement, and then have another printf at the end of each outer for iteration that just prints a new line.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Technically (programming is technical, this is important), you still have one loop inside another one (they are "nested"), not one after another one. That's fine and good, but it is important to use the correct nomenclature.

    Anyway, here's how I'd try the printing:
    Code:
       for(i=0;i<10;i++){
                 printf("%d %d\n", array[i][0],array[i][1]);
       }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Quote Originally Posted by claudiu View Post
    Well you are printing each element on a new line. "\n" in your printf. What you want is probably to print a space between the elements in that inner for printf statement, and then have another printf at the end of each outer for iteration that just prints a new line.
    I followed that logic and it did end up working just fine...
    but when i tried
    Code:
     for(i=0;i<10;i++){
                 printf("%d %d\n", array[i][0],array[i][1]);
       }
    it ended up printing the pair of numbers before going on to the next pair....not sure why...
    However if i had to print just the elements of the first array (array[i]) how would i go about doing that???
    I tired adding this to the end of my code:
    Code:
    for(i=0;i<10;i++){
       printf("%d", array[i]);
       }
    But it did not compile properly...

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    this is what i got from the complier...
    Code:
    cc1: warnings being treated as errors
    sort2d1.c: In function ‘main’:
    sort2d1.c:29: error: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The first array is not array[i] it's array[0]. If you just want to print that, then print the values of array[0][0] and array[0][1]. That corresponds to column 0 and 1 of row 0 of your array matrix.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by comp1911 View Post
    if i had to print just the elements of the first array (array[i]) how would i go about doing that???
    You have 10 arrays of two ints each:
    Code:
       int array[10][2];
    So the first one would be:
    Code:
    array[0][0];
    array[0][1];
    It's not clear to me what you are trying to do. I enter the numbers 1-20 in order at the prompt, how do you want those numbers printed? Based on what you said in post #7, what's in #9 and #10 should work. What does that do?
    Last edited by MK27; 04-25-2010 at 09:36 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    This is what the program does:
    Enter 20 numbers:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    Then this is what it prints:
    1 2
    3 4
    5 6
    7 8
    9 10
    11 12
    13 14
    15 16
    17 18
    19 20
    So my code works fine for that.
    But now what i want to do is at the end of the program just to print out the values of array [i] which is all the ones in the left hand side...and array [j] which is on the right hand side.
    So if i could make that work the last part of the program should look like:
    1
    3
    5
    7
    9
    11
    13
    15
    17
    19

    2
    4
    6
    8
    10
    12
    14
    16
    18
    20

    However this what i am having trouble with...
    i tried to place a loop at the end of the program
    Code:
    for(i=0;i<10;i++){
       printf("%d", array[i]);
      }
    for(j=0;j<10;j++){
       printf("%d", array[j]);
      }
    but then the complier states
    Code:
    cc1: warnings being treated as errors
    sort2d1.c: In function ‘main’:
    sort2d1.c:29: error: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Array [i] and array[j] are not what you describe them to be. Array[i] is a pointer to the i-th ROW not the first COLUMN. The first column is Array[0][i], and the second column is array[1][i].

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. obvious code does not compile
    By arash in forum C Programming
    Replies: 10
    Last Post: 07-23-2009, 05:42 PM
  2. Code won't compile
    By monkles in forum C Programming
    Replies: 3
    Last Post: 05-28-2009, 02:45 PM
  3. Compile Errors in my Code. Can anyone help?
    By DGLaurynP in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 10:36 AM
  4. This code won't compile in Red Hat Linux 9
    By array in forum Linux Programming
    Replies: 7
    Last Post: 04-27-2004, 07:30 AM
  5. How do they compile code for an OS ?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 03-28-2002, 12:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21