Thread: Simple 2 dimensional array problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Simple 2 dimensional array problem

    Hi there everyone, i've just joined the forum as i'm teaching myself c in my lunch hour at work and i'm sick of getting frustrated!

    I've set myself a problem to tackle and i'm going to try and get a working solution and as i learn more, maybe optimise the code a bit as no doubt it will be quite crude.

    I've hit a sticking point with a 2 dimensional array.

    Basically i want to have a 9x9 matrix or array which looks like this:

    012345678
    012345678
    012345678
    012345678
    012345678
    012345678
    012345678
    012345678
    012345678

    Obviously my code is pretty crude but i'm just learning!

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    // **********************
    //initialise variables
    int originalarray[8][8]; // 9x9 matrix
    int x, y, c;
    //***********************
    
    
    for (x = 0; x <= 8; x++) // Goes through rows
    {
    	for (y = 0; y <= 8; y++) //Goes through columns
    	{
    	//scanf( "%d", &c ); // reads character input
    	originalarray[x][y] = y; // makes the integer value of the matrix equal to the column number
    	}
    }
    
    for (x = 0; x <= 8; x = x++) //Goes through rows
    {	printf("\n"); //print newline
    		for (y = 0; y <= 8; y = y++) //Goes through columns
    		{
    		printf("%d", originalarray[x][y]); // prints each value of the matrix in turn
    		}
    }
    
    
    }
    So thats my code, the scanf bit is commented out as i was originally going to read in from the keyboard but i was trying to make it simlpler because it is failing.

    Anyway, my output is this:
    (don't know if i should put this in code tags)


    Code:
    012345670
    012345670
    012345670
    012345670
    012345670
    012345670
    012345670
    012345670
    Segmentation fault (core dumped)
    As you can see there are only 8 rows and the end column is just zeros!
    I would be very grateful if anyone could help here. Maybe i'll post the rest of my little problem up as i get more done so you can laugh at me and/or help optimise my code!

    Thank you in advance for your time.
    Simon.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How large is your array? This is the one I mean:
    Code:
    int originalarray[8][8];

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Wow that was quick!

    I'm sorry i don't understand...

    The array called

    Code:
    int originalarray[8][8]
    should be an 81 term 9x9 matrix. Each term will only be single digit integers. (0-9)

    Is that ok? Sorry if i didn't answer your question!
    Si

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And 8 gives you that how? I think you are doing the usual mistake of thinking that the number in the [] is the maximum index, rather than the number of elements.

    Note that char array[8] will give you 8 elements, numbered 0..7.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Ah Excellent!

    Thats great thanks!

    I was thinking that the two numbers were inclusive of zero. Hence i've now done

    Code:
    originalarray[9][9]
    and it works fine!

    Thank you Mats for a very quick response!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Byte Array problem
    By OldGit in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-20-2009, 05:45 AM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 07:23 PM