2D array, help needed

This is a discussion on 2D array, help needed within the C Programming forums, part of the General Programming Boards category; Hi, I am writing a code where I need a 11 cross 11 matrix (M) with all the elements zero, ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    2D array, help needed

    Hi,

    I am writing a code where I need a 11 cross 11 matrix (M) with all the elements zero, except
    M[5][5] = 1. I wrote the following code

    Code:
    #include<stdio.h>
    main()
    {
       int i,j;
       int M[10][10];
    
       for(i=0;i<=10;i++)
         {
           for(j=0;j<=10;j++)
             {
                 M[i][j]=0;
             }
          }
    
    M[5][5] = 1;
    }

    The problem is that the last line, M[5][5]=1 changes the all the elements in the 6th column to 1. I want only the 6th element in the 6th column to be 1. Can somebody tell me how to correct this mistake ?

    Thanks,
    banny
    Last edited by banny; 06-12-2008 at 11:20 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Did you see this picture when you posted the code?

    Did you read this link before posting code?
    << !! Posting Code? Read this First !! >>
    Attached Images Attached Images  
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by banny View Post

    The problem is that the last line, M[5][5]=1 changes the all the elements in the 6th column to 1. I want only the 6th element in the 6th column to be 1. Can somebody tell me how to correct this mistake ?

    Thanks,
    banny
    No it doesn't. Why do you think it does?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, banny.

    You need to do something:

    Highlight your code and click on the symbol for code tags. On this forum, it's the "#" sign icon, second from the right.

    That allows your code to be indented, so it looks like code.

    and then go ahead and properly indent your code (probably with spaces since the board editor uses the tab key for "jumping out" of the editor.

    You can and should edit you first post, and put code tags around it. Just to avoid getting off on the wrong foot on the forum.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by banny View Post
    Hi,

    I am writing a code where I need a 11 cross 11 matrix (M) with all the elements zero, except
    M[5][5] = 1. I wrote the following code

    Code:
    #include<stdio.h>
    main()
    {
       int i,j;
       int M[10][10];
    
       for(i=0;i<=10;i++)
         {
           for(j=0;j<=10;j++)
             {
                 M[i][j]=0;
             }
          }
    
    M[5][5] = 1;
    }

    The problem is that the last line, M[5][5]=1 changes the all the elements in the 6th column to 1. I want only the 6th element in the 6th column to be 1. Can somebody tell me how to correct this mistake ?

    Thanks,
    banny
    If you need a matrix 11 X 11, then make it 11 X 11, not 10 X 10. Your elements will then number from M[0][0] to M[10][10], so 11 rows, and 11 columns.

    Code:
    int M[11][11] = { { 0 } }; sets all array to 0
    
    M[5][5] = 1;  //assigns one to the 6th row, the sixth column, of M[][]
    Your code looks much better wrapped in code tags and indented. Thanks, banny.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    http://cpwiki.sourceforge.net/Implicit_main
    Note also that arrays go from 0 to n - 1, if you define an array of array[n].
    So in an array of array[10], index 10 is out-of-bounds.
    Further, putting 10 is the array creates 10 elements, not 11.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Thanks

    Thanks for you help. My code works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connection between 2D array and list vector?
    By glo in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2008, 01:53 PM
  2. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 01:01 AM
  3. malloced 2d array passed to qsort
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 11-25-2005, 12:55 PM
  4. When the memory needed for an array increases...
    By Xzyx987X in forum C Programming
    Replies: 3
    Last Post: 04-03-2004, 01:19 AM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM

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