473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

two dimensional arrays

hi,
i have simple problem plz solve. i want to print the following output. in 2d arrays.
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13

can anybody solve this prblm send. this question answer. plz
i will b grateful 2 u.
Jul 31 '07 #1
15 2121
Meetee
931 Expert Mod 512MB
hi,
i have simple problem plz solve. i want to print the following output. in 2d arrays.
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13

can anybody solve this prblm send. this question answer. plz
i will b grateful 2 u.
Hi,

You can take 4x4 two dimensional array and insert values in such a manner that a[0][0]=1 .....a[4][4]=13. Have you tried some code?. As I can help you in that code. I cannot write full code according the rule of this forum.

Regards
Jul 31 '07 #2
JosAH
11,448 Expert 8TB
Basically you're zig-zagging through that matrix; every time you hit a 'wall' you
change the y-direction. A single loop and a few if-statements can do the job:

Expand|Select|Wrap|Line Numbers
  1. +   +---+   +
  2. |   |   |   |
  3. +   +   +   +
  4. |   |   |   |
  5. +   +   +   +
  6. |   |   |   |
  7. +---+   +---+
  8.  
kind regards,

Jos
Jul 31 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Also, be aware that your 2D array is in memory this way:

1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13

You could treat it as a 1D array and just display 4 numbers per line.

C++ really only has 1D arrays. An int [4] array has 4 elements where each element is an int. An int [4][4] is still an array of 4 elements but each element is an array of 4 int.

Arrays are in memory contiguously as element 0 1 2 3.
Jul 31 '07 #4
Hi,

You can take 4x4 two dimensional array and insert values in such a manner that a[0][0]=1 .....a[4][4]=13. Have you tried some code?. As I can help you in that code. I cannot write full code according the rule of this forum.

Regards
here i have written some code plz help me

# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

the output of this coming like this

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

plz help me.
Aug 1 '07 #5
Also, be aware that your 2D array is in memory this way:

1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13

You could treat it as a 1D array and just display 4 numbers per line.

C++ really only has 1D arrays. An int [4] array has 4 elements where each element is an int. An int [4][4] is still an array of 4 elements but each element is an array of 4 int.

Arrays are in memory contiguously as element 0 1 2 3.
here i have written some code plz help me

# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

the output of this coming like this

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

plz help me.
Aug 1 '07 #6
Also, be aware that your 2D array is in memory this way:

1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13

You could treat it as a 1D array and just display 4 numbers per line.

C++ really only has 1D arrays. An int [4] array has 4 elements where each element is an int. An int [4][4] is still an array of 4 elements but each element is an array of 4 int.

Arrays are in memory contiguously as element 0 1 2 3.

here i have written some code plz help me

# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

the output of this coming like this

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

plz help me.
Aug 1 '07 #7
Basically you're zig-zagging through that matrix; every time you hit a 'wall' you
change the y-direction. A single loop and a few if-statements can do the job:

Expand|Select|Wrap|Line Numbers
  1. +   +---+   +
  2. |   |   |   |
  3. +   +   +   +
  4. |   |   |   |
  5. +   +   +   +
  6. |   |   |   |
  7. +---+   +---+
  8.  
kind regards,

Jos
here i have written some code plz help me

# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

the output of this coming like this

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

plz help me.
Aug 1 '07 #8
Meetee
931 Expert Mod 512MB
here i have written some code plz help me

# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

the output of this coming like this

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

plz help me.
If you want to print like
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Then just change your loop like
Expand|Select|Wrap|Line Numbers
  1. for(j=0;j<4;j++)
  2. {
  3. cout<<endl;
  4. for(i=0;i<4;i++)
  5. cout<<a[i][j]<<"\t";
  6. }
  7.  
also change void main() to int main() and return 0 at end.

regards

PS: Do not post for more than one time please.
Aug 1 '07 #9
JosAH
11,448 Expert 8TB
@OP: I have removed your other thread concerning this very same problem.
Please stick to one thread for one problem and please don't repeat yourself
when you're under stress. Have you considered my hint in my previous reply?

kind regards,

Jos
Aug 1 '07 #10
If you want to print like
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
Then just change your loop like
Expand|Select|Wrap|Line Numbers
  1. for(j=0;j<4;j++)
  2. {
  3. cout<<endl;
  4. for(i=0;i<4;i++)
  5. cout<<a[i][j]<<"\t";
  6. }
  7.  
also change void main() to int main() and return 0 at end.

regards

PS: Do not post for more than one time please.
BT I DON'T PRINT LIKE THIS I WANT 2 PRINT LIKE THIS

here i have written some code plz help me

# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

the output of this coming like this

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

BT I want output to PRINT LIKE THIS

1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
Aug 1 '07 #11
JosAH
11,448 Expert 8TB
@OP: I have removed your other identical thread again; you should stop double
posting now. Read the guidelines (top right corner, the 'Help' link). and see the
risk for double posting over and over again.

Here's a hint (it's more of a spoiler actually): if you subtract 1 from every number
you get the numbers 0, 1, 2, ... 15. Exclusive or the numbers in the second and
fourth columns with the two lowest bits, i.e. 3 or '11' in binary; do it by hand and
watch the results. Then reverse the operations and voila.

kin regards,

Jos
Aug 1 '07 #12
@OP: I have removed your other identical thread again; you should stop double
posting now. Read the guidelines (top right corner, the 'Help' link). and see the
risk for double posting over and over again.

Here's a hint (it's more of a spoiler actually): if you subtract 1 from every number
you get the numbers 0, 1, 2, ... 15. Exclusive or the numbers in the second and
fourth columns with the two lowest bits, i.e. 3 or '11' in binary; do it by hand and
watch the results. Then reverse the operations and voila.

kin regards,

Jos

BT I DON'T PRINT LIKE THIS I WANT 2 PRINT LIKE THIS

here i have written some code plz help me

# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

the output of this coming like this

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

BT I want output to PRINT LIKE THIS

1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
Aug 2 '07 #13
JosAH
11,448 Expert 8TB
BT I DON'T PRINT LIKE THIS I WANT 2 PRINT LIKE THIS

here i have written some code plz help me
I gave you the golden tip (read: spoiler) already. Are you actually able to read
or do you just like to repeat yourself over and over again?

kind regards,

Jos
Aug 2 '07 #14
hi,

i have written coding plz change the coding and print the output.
# include<iostream.h>
#include<conio.h>

void main()
{
int a[4][4] ;
int i, j;
clrscr();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
cout<<"Enter the value for dimension arrays " ;
cin>>a[i] [j] ;
}
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
cout<<a[j][i]<<"\t";
}
getch();
}

plz change the coding the output should be like this

1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
Aug 3 '07 #15
JosAH
11,448 Expert 8TB
hi,

i have written coding plz change the coding and print the output.
No, we don't handout code solutions here; but I bet you haven't even read my
hint (see above). otherwise you would have noticed that for the numbers
4, 5, 6, 7 exclusived or-ed with the value 3, you'd get 7, 6, 5, 4 and similar with
the values 12, 13, 14, 15, exclusive or-ed with the value 3, the result is:
15, 14, 13, 12. Only do this trick for odd column index values.

kind regards,

Jos
Aug 3 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Parrot | last post by:
I am trying to program a function to return a 2 dimensional array, but it's not working. I reduced the return value to 1 dimension and tested that to make sure that the problem wasn't elsewhere. ...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
9
by: Dadi | last post by:
Hi, I can make a simple initialization work like this: Object ONE_ROW = {{"Vodafone", "5550160100197016"}}; But, now I want to create another array that consists of multiple copies of...
16
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
5
by: Diffident | last post by:
Hello All, I have a 2-dimensional array that I am storing as a session variable. I have no idea on how I can cast the session variable back to 2-dimensional array. Any pointers? Reference...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.