#include <stdio.h>
int main()
{
//define two 2D arrays
long A[3][4], B[4][3];
//define 3D array
long C[2][3][4];
//define pointer to use with arrays
long *arr = '\0';
//looping variables
int i, j, k, count = 1;
printf("Array A is: \n");
//loop to define A as numbers 1-12
//also prints A as 3x4 matrix
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
A[i][j] = count;
count++;
if(j==0)printf("\n");
printf("%d\t",A[i][j]);
}
}
// 1 2 3 4 Array A
// 5 6 7 8
// 9 10 11 12
printf("\n\nThe transpose of A is: \n");
// 1 5 9 Transpose of A
// 2 6 10 Rows become columns,
// 3 7 11 Columns become rows.
// 4 8 12
//define array B as transpose of A
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
B[j][i]=A[i][j];
}
}
//print Array B
//requires reverse looping conditions to print 4x3 matrix
for(j=0;j<4;j++)
{
for(i=0;i<3;i++)
{
if(i==0)printf("\n");
printf("%d\t",B[j][i]);
}
}
//A(2,4) refers to array location A[1][3], as arrays begin at 0.
//B(2,1) refers to array location B[1][0], for the same reason.
arr = &A[1][3];
printf("\n\nA(2 , 4) = %d",*arr);
arr = &B[1][0];
printf("\nB(2 , 1) = %d\n",*arr);
//new 3D looping conditions to copy array A into array C twice
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
{
C[i][j][k] = A[j][k];
}
}
}
}
Any feedback is appreciated! :)


Sign In
Create Account


Back to top









