Jump to content

Practice with 2D arrays, just looking for some feedback!

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Humph

Humph

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi folks, I've been working on my next batch of C coursework and have written a program to practice using 2D and 3D arrays. I've got a working code, but just want to know if there's any way I can make it more efficient?

#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! :)

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

The way you're creating transporting Matrix A to B is the simplest and efficient enough.

I've looked on your code, and I've got question about the last portion of the code. what are you trying to achieve in the following code?


//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];

			}

		}

	}




#3
Humph

Humph

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi mnirahd,

That last part is just to complete the last part of the coursework question:
"Now define a pointer that you can use to access the elements of the Arrays A and B. Use it to print out the elements A(2,4) and B(2,1).
Check that you have printed the correct values. Declare a 3D array C, big enough to store two copies of array A and copy array A into C twice."

Thanks for the feedback!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users