Thread: Writing to my 2D Array

  1. #1
    Registered User
    Join Date
    Feb 2009

    Writing to my 2D Array


    Basically, I am having trouble writing to my 2D array.

    My system so far allows the user to input data in a 2D array called matrix. The user defines the size by specifying a row and column value. Then the system will as for how many gates they would like for the first column along with how many inputs for each gate. The matrix array is populated by using the convention of starting with column value 0, when the user has multiple gates the row index is incremented. e.g. 3 gates, 2 inputs for the 1s gate, 1 input for the 2nd and 3 inputs for the 3rd. Once the for loop has reached its limit and breaks out, the system asks the user would they like to move onto the next column? This does not mean increment the column by but instead by 2, and the whole process atrats again until the user requests no more moving to the next column.
    	for (i = 0; i < m; i++) {
    		for (j = 0; j < inputs0[i]; j++) {
    					printf("\nRow: %d\n", row);
    						printf("\tGate: %d, Input Element: [%d][%d]: ", position, row, column);
    							scanf("%d", &matrix[row][column]);
    Here is a snippet of the for loops, 'm' is the variable to specify the row size of the matrix. inputs0[i] stores how many inputs for each gate for that column, the poistion variabe defines what inputs are for what gates. All this works fine.

    The reason I increment by 2 is because I need the adjacent column after the column of gates, for the user to specify where the output of the gates are to be stored. If there are 3 gates in column [0], then the user will need to specify 3 index values for column [1] so that each gate can write its output to that value. Again this using the convention that the column value will stay the same, but the row value will increment.

    I have so far tried a for loop to find out how gates were specified for the column infront and then use a scanf() statement to get the input from the user. But I realised that were i was storing the values was just getting overwritten every time I entetred a new value.

    This is what I had, I didn;t expect it to work!
    for (i = 0; i < gatesColumn0; i++) {
    			printf("\nEnter the 'Row' output co-ordinate of Gate: %d: \n", position1);
    							scanf("%d", &matrix[row1][1]);
    Could anyone please show me how to get the user to input an output value for each gate so that it does not get overwriten? As you can see from my approach I am not a programmer, but I feel there must be a way to solve it.

    The whole point of the system is to be able to link gates logic together.

    I have included what I have got for my system as an attachment.

    Thankyou, if you need to me to re explain please ask, I never had a nack of explaing.


    EDIT, the appendix has an extra printf() statement above the for loop for when the user is expected to enter the row values for the output value colum.
    Last edited by qwertysingh; 04-12-2009 at 01:23 PM.

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