English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

The functions to be performed are:
(1) Display the array in 2-D format
(2) Display a particular element
(3) Display a particular row
(4) Display a particular column

2006-11-01 19:23:03 · 8 answers · asked by Parul P 1 in Computers & Internet Programming & Design

how to print dis 2-d array n display either row or column of the array?
I am using c++, but want a general answer that can be fit to java also..if possible.

2006-11-01 19:48:03 · update #1

8 answers

2d array from 1d array??
if it is that, you must know the dimensions of 2d array. Lets assumme its m * n. Then in your 1d array first n-elements are actually a[1][n] elements, and likewise your a[x][y] element would be orginal_a[x+n*y]th element.
An original_a[x] element would be a[n/x][n%x] element.

To display this array in 2d format use following code.

for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
printf("%d",original_a[i*j]);
printf("\n");
}

This will simply divide the array in n elements and print the n elements in one row.

Displaying a row is simple.
lets assumme that you want to disapy xth row.
for(int i = 0;i<=n;i++)
printf("%d ",original_a[x*n+i]);

To display a particular column y

for(int i = 0;i<=m;i++)
printf("%d ",original_a[y + i*n]);

2006-11-01 19:54:20 · answer #1 · answered by manoj Ransing 3 · 3 0

u r question is a bit confusing but as far as i understood
1)to display array in 2-d format first of all create an array suppose of i rows and j coloumns then use two for gettin the values and for printing the values

2) for displayin an element like element of 3rd row 2nd just print the value at a[3][2] where a is u r array

3) for displaying a row fix the i and run with incrementin j and print all values

4)for displayin particular column fix j and increment i an d print all values

2006-11-02 05:55:49 · answer #2 · answered by navjot 1 · 0 0

Its simple u can take a 1-D array whose each element is a pointer to the first element of other 1-D arrays. Like this u can make a 2-D array of any size u want and u will be able to access the elements easily. Use linked list. I think u got everything.

2006-11-02 09:33:21 · answer #3 · answered by Napster 2 · 0 0

for 2-D array u need two loops, one inside the other

lets say loops with index i & j.

then A[i][j] represents an element of the array.

to print a particular row/column make either of i or j fixed and vary the other.

2006-11-02 03:39:06 · answer #4 · answered by Demo 3 · 0 0

String Z[][] = null;
String [] A = {"1","2","3","4","5"};
String [] B = {"7","8","9","10","11"};

Z = [A.length][B.length];
for (int i = 0; i for( int j = 0; j Z[i][j]= A[i]+","+B[j];
}

String element = Z[0][1];

String row = null;
for (int i = 0; i row += A[i];
}

==========
depending on the pgm language, this is kinda like java pseudo code above

2006-11-02 03:40:55 · answer #5 · answered by Anonymous · 0 0

Agree with Manoj

2006-11-02 04:03:44 · answer #6 · answered by Vaibhav 4 · 0 0

Stop asking for help and start reading your book.

2006-11-02 03:29:40 · answer #7 · answered by Huey L 3 · 0 1

What programming language you are you using? Please add in details

2006-11-02 03:28:08 · answer #8 · answered by jack 6 · 0 1

fedest.com, questions and answers