C: accessing multidimensional arrays with pointers - c

I have the following code which works for 2-dimensional arrays using two methods shown. It also works for 3-dimension arrays only a single method.
Does anyone know what the pointer based solution, similar to int *p = ((int *) mda1) + (3 * r) + c;, would look like for the 3-dimensional array? Thanks
#include <stdio.h>
int main(int argc, char *argv[])
{
int mda1[2][3] = {{0, 1, 2}, {3, 4, 5}};
int mda2[][3] = {{0, 1, 2}, {3, 4, 5}};
int mda3[2][2][1] = { {{0}, {1}},
{{2}, {3}} };
for (int r=0; r<2; r++) {
for (int c=0; c<3; c++) {
int *p = ((int *) mda1) + (3 * r) + c;
printf("%d", *p);
}
}
printf("\n");
int (*mda1p)[3] = mda1;
for (int i=0; i<2; ++i) {
for (int j=0; j<3; ++j)
printf("%d", *(*(mda1p + i) + j));
}
printf("\n");
int (*mda3p)[2][1] = mda3;
for (int i=0; i<2; ++i) {
for (int j=0; j<2; ++j) {
for (int k=0; k<1; ++k) {
printf("%d", *(*(*(mda3p + i) + j) + k));
}
}
}
return 0;
}
SOLUTION:
#define MDA3_X 2
#define MDA3_Y 2
#define MDA3_Z 1
int mda3[MDA3_X][MDA3_Y][MDA3_Z] = { {{0}, {1}},
{{2}, {3}} };
for (int i=0; i<MDA3_X; ++i) {
for (int j=0; j<MDA3_Y; ++j) {
for (int k=0; k<MDA3_Z; ++k) {
printf("%d",
*((int *) mda3 +
MDA3_Y * MDA3_Z * i + MDA3_Z * j + k));
}
}
}
printf("\n");

If I have understood correctly you are trying to write something like
int *p = ( int * )mda3;
printf( "%d", *( p + 2 * 1 * i + 1 * j + k ) );
That is in general if you have a three-dimensional array declared like
int a[N1][N2][N3];
then using a pointer of the type int * you can write
int *p = ( int * )a;
//...
printf( "%d", *( p + N2 * N3 * i + N3 * j + k ) );
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
enum { N1 = 2, N2 = 3, N3 = 4 };
int a[N1][N2][N3] =
{
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 10, 11, 12, 13 }
},
{
{ 21, 22, 23, 24 },
{ 25, 26, 27, 28 },
{ 30, 31, 32, 33 }
}
};
int *p = ( int * )a;
puts( "a =" );
printf( "{\n" );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\t{\n" );
for ( size_t j = 0; j < N2; j++ )
{
printf( "\t\t{" );
for ( size_t k = 0; k < N3; k++)
{
printf( "%2d ", *( p + N2 * N3 *i + N3 * j + k ) );
}
printf( "}\n" );
}
printf( "\t}\n" );
}
printf( "}\n" );
return 0;
}
Its output is
a =
{
{
{ 1 2 3 4 }
{ 5 6 7 8 }
{10 11 12 13 }
}
{
{21 22 23 24 }
{25 26 27 28 }
{30 31 32 33 }
}
}

Related

Reverse order of rows in 2d array

I'm trying to figure out how to "flip" an 2d array like this:
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
{{7, 8, 9},
{4, 5, 6},
{1, 2, 3}}
Searching how to reverse/flip rows of 2D array just returns how to reverse the content of the rows, or some other variation that doesn't work like I need
Swapping rows work exactly the same way as swapping integers, only instead of assigning you need to copy the memory.
void reverseRows(size_t rows, size_t cols, int (*array)[cols])
{
int temp[cols];
size_t last = rows - 1, first = 0;
while(last > first)
{
memcpy(temp, array[first], sizeof(*array));
memcpy(array[first++], array[last], sizeof(*array));
memcpy(array[last--], temp, sizeof(*array));
}
}
#define N 3
int main(void)
{
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
reverseRows(N,N,a);
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
}
https://godbolt.org/z/qxW3rG3sx
Here you are.
#include <stdio.h>
void swap_arrays( int *a1, int *a2, size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
int tmp = a1[i];
a1[i] = a2[i];
a2[i] = tmp;
}
}
int main(void)
{
enum { N = 3 };
int a[N][N] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
for ( size_t i = 0; i < N / 2; i++ )
{
swap_arrays( a[i], a[N - i - 1], N );
}
for ( size_t i = 0; i < N; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%d ", a[i][j] );
}
putchar( '\n' );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3
4 5 6
7 8 9
7 8 9
4 5 6
1 2 3

Changing the sign of array values in C

I need to write a function that takes the elements in an array and changes the sign (ex. 3 --> -3 or -3 --> 3). l want use this array (int a[2][3] = { { 55,-44,},{1, -4},{6,11} };) instead of ( int a[] = { 5,6,-4};)
What should I do?
#include <stdio.h>
void change_sign(int* beta)
{
for (int i = 0; i < 3; i++) {
beta[i] = -beta[i];
}
}
int main(void)
{
int a[] = { 5, 6, -4};
for (int i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
printf("\n");
change_sign(a);
for (int i = 0; i < 3; i++) {
printf("%d ", a[i]);
}
return 0;
}
For starters it seems you mean this array
int a[3][2] = { { 55,-44,},{1, -4},{6,11} };
instead of this
int a[2][3] = { { 55,-44,},{1, -4},{6,11} };
In any case if your compiler supports variable length arrays then the function can look like
void change_sign( size_t m, size_t n, int a[][n] )
{
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < n; j++ )
{
a[i][j] = -a[i][j];
}
}
}
and call the function like
change_sign( 3, 2, a );
Otherwise the function can look like
#define N 2
void change_sign( int a[][N], size_t m )
{
for ( size_t i = 0; i < m; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
a[i][j] = -a[i][j];
}
}
}
and called like
change_sign( a, 3 );

All max elements and their postition of array

So, for example, I have array: [1, 4, 9, 3, 9]
I need to find all max elements [9, 9] and their index [2, 4]
How can I do this? In C language
int i, pom, max;
max=*gradovi;
for(i=0;i<n;i++) {
if(*(gradovi+i)>max) {
max=*(gradovi+i);
pom=i;
}
if(*(gradovi+i)==max) {
pom=i;
}
}
return pom;
I need postions of all max elemenents, but this print just last
In any case you need a container that will store the indices of the elements with the maximum value.
You can allocate memory dynamically for such a container.
Here is a demonstrative program.
#include <stdio.h>
#include <stdlib.h>
size_t max_elements( const int a[], size_t n, size_t **result )
{
*result = NULL;
size_t count = 0;
if ( n != 0 )
{
size_t max_i = 0;
count = 1;
for ( size_t i = 1; i < n; i++ )
{
if ( a[max_i] < a[i] )
{
max_i = i;
count = 1;
}
else if ( !( a[i] < a[max_i] ) )
{
++count;
}
}
*result = malloc( count * sizeof( size_t ) );
if ( *result != NULL )
{
for ( size_t i = max_i, j = 0; i < n && j < count; i++ )
{
if ( !( a[i] < a[max_i ] ) ) ( *result )[j++] = i;
}
}
}
return count;
}
int main(void)
{
int a[] = { 1, 4, 9, 3, 9 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t *result = NULL;
size_t count = max_elements( a, N, &result );
for ( size_t i = 0; i < count; i++ )
{
printf( "%zu: %d, ", result[i], a[result[i]] );
}
putchar( '\n' );
free( result );
return 0;
}
Its output is
2: 9, 4: 9,
If the returned value from the function is not equal to 0 but the pointer result was set to NULL then it means that there was a memory allocation error. You can check such a situation.
I'm stupid, it's simple solution:
void maks(int *gradovi, int n){
int i, pom, max;
max=*gradovi;
for(i=1;i<n;i++){
if(*(gradovi+i)>max){
max=*(gradovi+i);
}
if(*(gradovi+i)==max){
pom=i;
printf("Najvise zarazenih je u gradu sa indeksom: %d\n", pom);
}
}
}

How to print all square submatrices of square matrix in C?

Please, help me to find and print all square submatrices of square matrix from big to small square matrices in C programming language
I wrote code that works wrong:
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i,j;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--)
{
for(i = 0; i < sub_mtrx_size; i++)
{
for(j = 0; j < sub_mtrx_size; j++)
{
printf("%3d ", mat[i][j]);
}
printf("\n");
}
printf("\n");
}
return 0;
Here I need to find all 8x8, 7x7, 6x6, 5x5, 4x4, 3x3 and 2x2 submatrices.
Your code was just printing a single sub-matrix for each size, positioned in the upper-left corner of the matrix. You need to add i and j offsets to get the sub-matrices at all positions:
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}
Java implementation for a general nxm matrix:
private static void printSubMatrix(int[][] mat) {
int rows=mat.length;
int cols=mat[0].length;
//prints all submatrix greater than or equal to 2x2
for (int subRow = rows; subRow >= 2; subRow--) {
int rowLimit = rows - subRow + 1;
for (int subCol = cols; subCol >= 2; subCol--) {
int colLimit = cols - subCol + 1;
for (int startRow = 0; startRow < rowLimit; startRow++) {
for (int startCol = 0; startCol < colLimit; startCol++) {
for (int i = 0; i < subRow; i++) {
for (int j = 0; j < subCol; j++) {
System.out.print(mat[i + startRow][j + startCol] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
}
}
}
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
/* if we make terminating condition sub_mtrx_size>=1 then we will have all
possible square sub matrices */
for(sub_mtrx_size = mtrx_size; sub_mtrx_size >= 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}

Bubble sort an Array of pointers of another arrays in a function (C)

I want to make a bubble sort function of an array of pointers that each of the pointers point to another arrays - inside of function and i'm getting a error that i violated a writing location (Visual Studio)
P.S, I do (*parr)++ because the first value of each array shows the length of the array without the first value so i need to start bubble sorting from the second box (arr[1] and not arr[0] for example ).
can someone write to me how can i fix it?
Thanks for help
(I need to sort the values of the original arrays not the pointer of the arrays).
int main(void){
int i = 0;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
system("PAUSE");
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++, (parr)++)
{
length = **parr;
(*parr)++;
for (j = 0; j < length-1; j++)
{
for (k = 0; k < length - j - 1; k++, (*parr)++)
{
if ((**parr)>(*(*parr + 1)))
{
temp = **(parr);
**(parr) = (*(*parr + 1));
(*(*parr + 1)) = temp;
}
}
}
}
}
This seems to work. It is easier in func1 to use dereferenceing as parr[i][k] rather than moving the pointer.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func1(int** parr);
int main(void){
int j;
int arr0[4] = { 3, 9, 6, 7 };
int arr1[3] = { 2, 5, 5 };
int arr2[1] = { 0 };
int arr3[2] = { 1, 6 };
int arr4[5] = { 4, 5, 6, 2, 1 };
int* parr[5] = { arr0, arr1, arr2, arr3, arr4 };
func1(parr);
for (j = 1; j <= arr0[0]; j++)
{
printf ( "arr0[%d] %d\n", j, arr0[j]);
}
for (j = 1; j <= arr4[0]; j++)
{
printf ( "arr4[%d] %d\n", j, arr4[j]);
}
return (0);
}
void func1(int** parr)
{
int i;
int temp;
int j;
int k;
int length;
for (i = 0; i < 5; i++)
{
length = **parr;
for (j = 0; j < length; j++)
{
for (k = 1; k < length - j; k++)
{
temp = *((*parr)+k);
if (*((*parr)+k)>*((*parr)+k+1))
{
temp = *((*parr)+k);
*((*parr)+k) = *((*parr)+k+1);
*((*parr)+k+1) = temp;
}
}
}
*parr++;// next array
}
}

Resources