Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What code to use in c program when you want to display the elements in a specific row using two dimensional array??

+2 votes
asked Aug 26, 2021 by Valentine Clerigo (140 points)
The program lets the user enter a number of a row and it should display it. How can I access that certain row using two-dimensional array in c language?

1 Answer

+1 vote
answered Sep 21, 2021 by Peter Minarik (55,660 points)
edited Sep 21, 2021 by Peter Minarik

First of all, the row and column are arbitrary properties of an array. You decide what you call a row and what you call a column.

If I want to declare a two dimensional array in C, I could do something like this:

ChessPiece board[8][8];

So we're playing chess here. 8 x 8 board. But is the first number a row or a column? Well, there is no good answer. No wrong either. It's totally up to you. As long as you handle things consistently, it doesn't matter. You can call the first index row or the second. The decision is yours.

If I'd use the first index to be the row information and the second to be the column information, then I would access the chess piece on the board in row 2 column 5 (column E if you're an avid chess player) like this:

board[1][4] = Pawn;

And there you go, there is now a Pawn on row 2 column 5. Please note that I used 1 and 4 respectively as indices are 0-based in C (and many other programming languages) while in mathematics (and real life problems), we typically use 1-based indexing.

Again, I could have chosen to have the first index the column and the second to be the row. As long as I keep to my rules, all is fine.

Even I can index a one dimensional array as if it would be a two dimensional:

#include <stdio.h>

#define ROW_SIZE    8
#define COLUMN_SIZE 8

typedef enum
} ChessPiece;

//ChessPiece board[ROW_SIZE][COLUM_SIZE];
ChessPiece board[ROW_SIZE * COLUMN_SIZE];

ChessPiece GetChessPiece(unsigned int row, unsigned int column)
    return board[row * COLUMN_SIZE + column];

void SetChessPiece(unsigned int row, unsigned int column, ChessPiece piece)
    board[row * COLUMN_SIZE + column] = piece;

void PrintBoard()
    for (int row = 0; row < ROW_SIZE; row++)
        for (int column = 0; column < COLUMN_SIZE; column++)
            switch (GetChessPiece(row, column))
                case Empty:     printf("."); break;
                case Pawn:      printf("P"); break;
                case Rook:      printf("R"); break;
                case Knight:    printf("N"); break;
                case Bishop:    printf("B"); break;
                case King:      printf("K"); break;
                case Queen:     printf("Q"); break;
                default:        printf("?"); break; // error case

int main()
    //board[1][4] = Pawn;
    SetChessPiece(1, 4, Pawn);
    return 0;

Note: It doesn't mean you should use one dimensional arrays always. :) This is just to show that rows and columns are really the decisions of the programmer.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.