Writing to a 2D array

This is a discussion on Writing to a 2D array within the C Programming forums, part of the General Programming Boards category; Hi, I'm slightly stuck. I have got the user to input a value which is then stored at a hard ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Writing to a 2D array

    Hi, I'm slightly stuck. I have got the user to input a value which is then stored at a hard coded index within the 2D array.

    eg,
    Code:
    printf("Enter the 1st input value: ");
    	scanf("%d", &array[1][2]);
    After all the input values have been entered, the system applies the requested logic to the values. This results in a single interger output.

    Now, this is where I'm stuck, is there a way of writing that value to an index location in the 2D array that the user specifies.

    ie, where would you like store the value? Then user inputs the row and column index number. Usig scanf() this becomes the location of the value.

    Any help would be great, thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably need to store the value in a temporary variable - unless you know the position, in which case you can of course use the same style of code you already have, but using the index the user provided [after you have validated that it's a valid position of course and not outside of the 2D array].

    --
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    Code:
    int x, y;
    printf("Enter row: ");
    scanf("%d", &x);
    printf("Enter column: ");
    scanf("%d", &y);
    my2darray[x][y] = myvalue;
    Unless I'm misinterpreting the question.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    19

    Fixed

    Fixed, thankyou for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  2. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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