Array issue

Creating a 4 x 4 array containing numbers input by user

Page 1 of 1

6 Replies - 863 Views - Last Post: 19 July 2010 - 07:31 AM

#1 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Array issue

Posted 15 July 2010 - 07:08 AM

I'm trying to create a 4 x 4 array filled with numbers input by the user. I then need to rotate the grid clockwise like I've shown below.

01 02 03 04
05 06 07 08
09 10 11 12
13 14 15 16

13 09 05 01
14 10 06 02
15 11 07 03
16 12 08 04

I made all numbers double digit just for show in the example so the table is easier to read.
So far the code I have is just trying to create the array and fill with numbers from the user. This asks for a number once then doesn't display anything at all. Any clues as to what I'm doing wrong?

<html>
<head>
<title>Rotate Array Matrix</title>
</head>
<body>
<script type="text/javascript">
<!--
var row, col;
var i;
var n;
var a;
var x = new Array();
for (i = 1; i <= 4; i++)
	x[i] = new Array();
	for (n = 1; n <= 4; n++)
	{	x[i][n] = prompt("Enter a number.", "");;
	}

for (n = 1; n <= 4; n++)
 { for (i = 1; i <= 4; i++)
    document.write(x[i][n] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
   document.write("<br />");
 }

//-->
</script>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Array issue

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3382
  • View blog
  • Posts: 9,569
  • Joined: 08-June 10

Re: Array issue

Posted 15 July 2010 - 07:11 AM

maybe its the double semi-colon?
Was This Post Helpful? 0
  • +
  • -

#3 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Array issue

Posted 15 July 2010 - 07:13 AM

Thanks for pointing that out. Doesn't fix the problem though.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Guest*


Reputation:

Re: Array issue

Posted 15 July 2010 - 07:14 AM

anything in the error console?
Was This Post Helpful? 0

#5 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Array issue

Posted 15 July 2010 - 07:16 AM

I got it. Didn't have the first for loop in brackets.

<html>
<head>
<title>Rotate Array Matrix</title>
</head>
<body>
<script type="text/javascript">
<!--
var row, col;
var i;
var n;
var a;
var x = new Array();
for (i = 1; i <= 4; i++)
{	x[i] = new Array();
	for (n = 1; n <= 4; n++)
	{	x[i][n] = prompt("Enter a number.", "");
	}
}

for (n = 1; n <= 4; n++)
 { for (i = 1; i <= 4; i++)
    document.write(x[i][n] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
   document.write("<br />");
 }

//-->
</script>
</body>
</html>



now I just need to figure out how to rotate it...I'm not going to check back here until I've tried to figure it out. Feel free to make suggestions. I'll see what I can do on my own.

View PostGuest, on 15 July 2010 - 06:14 AM, said:

anything in the error console?


I'm just learning. How do I use the error console? That would probably help me a lot.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Guest*


Reputation:

Re: Array issue

Posted 15 July 2010 - 07:23 AM

in (e.g.) Firefox, go to Tools > Error Console. ther will be listed all errors and warnings, that are caused by CSS and Javascript.
Was This Post Helpful? 0

#7 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Array issue

Posted 19 July 2010 - 07:31 AM

I'm having trouble with this rotating business. I understand I need to switch the columns and rows and reverse the order of the column if reversing before the switch or the rows if reversing after the switch.

How do I reverse the order of just the columns or just the rows in a 2-dimensional array?

Lines 27-29 were an attempt to reverse the order of the content of the columns in array x and copy that to the rows of array y.

The error console only shows one error:
x[i] is not defined
line 34

the first array is printing to the screen still but I can't get the second array to print.

Here's what I have so far:
<html>
<head>
<title>Rotate Array Matrix</title>
</head>
<body>
<script type="text/javascript">
<!--
var row, col;
var i;
var n;
var a;
var x = new Array();
var y = new Array();
for (i = 1; i <= 4; i++)
{	x[i] = new Array();
	for (n = 1; n <= 4; n++)
	{	x[i][n] = prompt("Enter a number.", "");
	}
}

for (n = 1; n <= 4; n++)
{ 	for (i = 1; i <= 4; i++)
    		document.write(x[i][n] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<br />");
}

//y[n] = new Array();
//y[n] = x.reverse([i]);
//y[i] = x[n];

for (n = 0; n <= y.length; n++)
{	y[n] = new Array();
	for(i = 0; i <= y.length; i++)
	{	y[n][i] = x[i][n];
	}
}

for (i = 0; i <= y.length; i++)
{	for(n = 0; n <= y.length; n++)
		document.write(y[n][i] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<br />");
}

//-->
</script>
</body>
</html>


This post has been edited by monkadelicd: 19 July 2010 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1