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
Related
I'm trying to write a function that copies all of the values in source1 which are also found in source2 into a destination and then returns the number of elements copied into the destination.
int common_elements(int length, int source1[length], int source2[length], int destination[length])
{
int counter = 0;
int i = 0;
while (i < length) {
int j = 0;
while (j < length) {
if ( source1[i] == source2[j]) {
destination[counter] = source1[i];
counter++;
}
j++;
}
i++;
}
return counter;
}
The problem is e.g. given (common_elements(5, {1,2,3,4,5}, {1,2,3,2,1}, [])), the correct input should be
1,2,3
return value: 3
However, the program is accounting for the duplicates and produces :
1,1,2,2,3
return value: 5
which is incorrect.
How can I remedy this?
In this while loop
int j = 0;
while (j < length) {
if ( source1[i] == source2[j]) {
destination[counter] = source1[i];
counter++;
}
j++;
}
you are counting all elements in the array source2 that are equal to the element source1[i].
I can suggest the following solution provided that the source arrays may not be changed within the function.
#include <stdio.h>
size_t common_elements( int destination[],
const int source1[],
const int source2[],
size_t n )
{
size_t counter = 0;
for ( size_t i = 0; i < n; i++ )
{
size_t number = 1;
for ( size_t j = 0; j < i; j++ )
{
if ( source1[i] == source1[j] ) ++number;
}
for ( size_t j = 0; number && j < n; j++ )
{
if ( source1[i] == source2[j] ) --number;
}
if ( number == 0 ) destination[counter++] = source1[i];
}
return counter;
}
int main(void)
{
enum { N = 5 };
int source1[N] = { 1, 2, 3, 4, 5 };
int source2[N] = { 1, 2, 3, 2, 1 };
int destination[N];
size_t n = common_elements( destination, source1, source2, N );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", destination[i] );
}
putchar( '\n' );
return 0;
}
The program output is
1 2 3
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 );
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 }
}
}
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);
}
}
}
Input array is for example Array[10] = {12,23,0,-7,138,22,7,99,10,-2}
I want to print out the array with even numbers on one end and odd numbers on the other, something like this: Array[10] = {12,0,-2,10,22,138,-7,99,7,23}
int main()
{
int N = 0, i = 0, j = N-1, index = 0;
printf("Enter N (0>N<10): ");
scanf("%d", &N);
int Array[N];
for(i=0;i<N;i++){
printf("Enter the value number %d :", i+1);
scanf("%d", &Array[i]);
}
printf("Array[%d] ={",N);
for(i=0;i<N;i++){
printf("%d\t", Array[i]);
}
printf("}");
// swaping odd and even numbers
int Array2[N];
while (i < j){
if(Array[i]%2 == 0){
Array2[i] = Array[index];
i++;
}
else{
Array2[j] = Array[index];
j++;
}
index++;
}
// view the new array
printf("\n Array[%d] ={",N);
for(i=0;i<N;i++){
printf("%d\t", Array2[i]);
}
printf("}");
return 0;
}
This doesn't seem to work. Any help would be appreciated.
Note: I know the Array[N] part is not how it's supposed to be done, it's just to simplify things.
In your else statement:
else{
Array2[j] = Array[index];
j++;
}
You need j--; not j++.
In your else statement:
if(Array[i]%2 == 0){
don't you want to inspect the item you are sorting (Array[index]) ?
At the beginning of the program in this declaration
int N = 0, i = 0, j = N-1, index = 0;
j is set to -1 because N is initialized by 0.
So this loop
while (i < j){
//...
will be iterate never independing on whether it makes any sense.:)
As for the loop then it does not swap even and odd numbers.
If I have understood your approach correctly you need something like the folloing. You can modify the demonstrative program such a way that the values of elements of the array will be enetered by the user.
#include <stdio.h>
#define N 10
int main( void )
{
int a[N] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
int i, j;
printf( "Array[%d] = { ", N );
i = 0;
do
{
printf( "%d", a[i] );
} while ( ++i < N && printf( ", " ) );
printf( " };\n");
i = 0; j = N;
while ( i != j )
{
if ( a[i] % 2 == 0 )
{
++i;
}
else if ( a[--j] % 2 == 0 )
{
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
printf( "Array[%d] = { ", N );
i = 0;
do
{
printf( "%d", a[i] );
} while ( ++i < N && printf( ", " ) );
printf( " };\n");
return 0;
}
The program output is
Array[10] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
Array[10] = { 12, -2, 0, 10, 138, 22, 7, 99, -7, 23 };
Dry run it, and you will understand the concept
#include <stdio.h>
int main()
{
int arr[10];
int i,temp;
printf("Enter elements into arry\n");
for(i=0;i<=9;i++)
{
printf("Element #%d-->",i);
scanf("%d",&arr[i]);
}
for(i=0;i<=9;i=i+2)
{
if((arr[i]%2)!=0)
{
if((arr[i+1]%2)!=0)
{
arr[i]=arr[i];
arr[i+1]=arr[i+1];
}
else if((arr[i+1]%2)==0)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
else
{
if((arr[i+1]%2)==0)
{
arr[i]=arr[i];
arr[i+1]=arr[i+1];
}
else if((arr[i+1]%2)!=0)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
for(i=0;i<=9;i++)
{
printf("Element #%d-->%d\n",i,arr[i]);
}
return 0;
}