Hey, this code accepts data (type int) into a 4 by 4 array. im using 1234 as an example
when entered, the data would be stored as:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

the code the rotates it once to the right so it is printed out as:

1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

it was an example my teacher gave us to practice manipulating data in 2D arrays.
i dont fully understand how it works, could someone explain to me step by step how it works?

#include<stdio.h>
main(){

int i,j,pass;
int row=4;
int col=4;
int arr[4][4],barr[4][4];
char abc;

//prompt user to enter numbers to store in arr
for(i=0;i<row;i++)
	for(j=0;j<col;j++){
		printf("Enter a number ");
		scanf("%d",&arr[i][j]);
	}

pass=row-1;
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		barr[j][pass]=arr[i][j];
	pass=pass-1;
}
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		printf("%d  ",barr[i][j]);
	printf("\n");
}
scanf ("%ch", &abc);  //keeps data on screen
return(0);
}

thanks in advance. oh and could someone possibly explain how i would manipulate to print out the array rotate it more times to the right?

Forget your code. Take a pencil and paper, and draw your array of 4 x 4 (or 2 x 2), whatever size you need. Make it simple as you need it to be.

Now, look at the paper from above, paper flat on the table or desk. Now slowly turn the paper 90° to the right. Repeat that until you have noted the 1st position of every element of the array, and it's last value, after the array is rotated.

<< THAT'S how you learn to solve a problem. >>

If you use a different value for every element of the array, it may help you.

Write down the equation that solves the change in position for any element, after the array has been rotated, given it's starting position. It's simple arithmetic, so no trig or calc is necessary, here.

Doing it this way, you WILL LEARN and remember, how this works, for a very long time. If I just told you, you'd forget it inside of a week.

And have fun, because this is a fun exercise. ;)

Then, after you do what Adak suggested, on a piece of paper draw 2 empty matrices labeled arr and barr. Label the left sides and tops based on the indices used in the program posted. Now follow the program line by line, recording values of all the variables.

cool, starting now

and its not really a problem..morel like it was explained to me in almost the same way and i still didnt understand o.o

Look at a matrix 2 x 2, with 0-3 for values:

0   1
2   3

When this matrix is turned 90° clockwise, where will each element wind up?

0 - goes to top row right column
1 - goes to bottom row right col
2 - becomes top row left col
3 - what does 3 become?

What relationships do you see in the above? The position of the value before the turn, let's call pos0. The position after the turn, let's call pos1.

pos1 = pos0 if column of the value, becomes the row. That is, the 0 column, becomes the 0 row, the 1 column (1 from the leftmost column), becomes what row?

Note that I'm always counting from the leftmost column, from left to right. That works fine when the rotation to be made is clockwise. If it's counterclockwise, then you start counting at the rightmost column, so that's the column that becomes column 0. The top row still is row 0, next row down is row 1, etc.

Work with a simple 2 X 2 array until you can see what I'm prattling on about here. Then try it with a 3 X 3 array, and work through it, this same way.

If you don't see it at first, keep working with it. The lightbulb WILL go off * POOF! *

;)

thanks, will do

i actually understood it pretty clearly with Adak's explanation,
gonna "play" with it a little bit...might come back with it again in a while

well, i tweaked it, an added an option for the user to enter a number and depending on the number, a certain variation of the array will be shown....but i havsin some difficulties....could anyone help me as to where i went wrong?

#include<stdio.h>
main(){

int i,j,pass;
int row=4;
int col=4;
int arr[4][4],barr[4][4], carr[4][4], darr[4][4] ;
char abc;
int rot ;


//prompt user to enter numbers to store in arr
for(i=0;i<row;i++)
	for(j=0;j<col;j++){
		printf("Enter a number ");
		scanf("%d",&arr[i][j]);
	}
printf("please enter amount of rotations wanted (0 to 3)  :");
scanf ("%d", &rot);

// calculations for rotations
pass=row-1;
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		barr[j][pass]=arr[i][j];
	pass=pass-1;
}

pass=row-1;
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		carr[j][pass]=barr[i][j];
	pass=pass-1;
}
pass=row-1;
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		darr[j][pass]=carr[i][j];
	pass=pass-1;
}



if (rot = 0){
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		printf("%d  ",arr[i][j]);
	printf("\n");
}
}
else 
  if (rot == 1){	
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		printf("%d  ",barr[i][j]);
	printf("\n");
}
}

else
 if (rot==2){
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		printf("%d  ",carr[i][j]);
	printf("\n");
}
}

else
 if (rot==3){
for(i=0;i<row;i++){
	for(j=0;j<col;j++)
		printf("%d  ",darr[i][j]);
	printf("\n");
}
}

scanf("%ch", abc);// keeps result on screen(mine wasnt staying on screen)

return(0);
}

well, i tweaked it, an added an option for the user to enter a number and depending on the number, a certain variation of the array will be shown....but i havsin some difficulties....could anyone help me as to where i went wrong?

The first thing you did wrong is expect us to read your mind. "i havsin some difficulties" doesn't tell us anything. You need to tell is what the problem is.

sorry....welll i made it so that depending on the number entered for "rot" a specific rotation will be shown. si set it into If statements....but since i made the additions it stops after i enter all the numbers, im not sure if its a problem with my if statements or not

fixed it, thanks for all the help guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.