How can I generate some random cards with numbers, but I need manipulate them as a 3 dimenson matrix . [players][n][n] .. n = The dimenson
My program generates only one card, how can I generate more cards? The index of the cards will be set in the variable players( jogadores in the program) that is the first dimension of the Matrix
#include <stdio.h>
#include <stdlib.h>
int n, soma;
int jogadores;
int menu;
int
main()
{
srand(123);
printf("Número de jogadores: \n");
do {
scanf("%d", &jogadores);
} while (jogadores < 2 || jogadores > 10);
printf("Numero de jogadores salvo \n Escolha a dimensao das cartelas: \n");
do {
scanf("%d", &n);
} while (n < 2 || n > 9);
printf("\n Dimensao das cartelas salva \n ");
//printf(" %d %d \n ",jogadores,n);
int value = 10 * n;
// I need to add the dimensiona _jogadores_ ==> cartela [jogadores][n][n]
int cartela[n][n];
// Loop number of players
// for (int q = 0; q <=jogadores;q++)
// {
// Loop lines of the card
for (int i = 0; i <= n; i++) {
// Loop rows of the card
for (int j = 0; j < n; j++) {
do {
soma = 0;
// Colocar a dimensão jogadores
cartela[i][j] = rand() % value;
for (int l = 0; l < n; l++) {
for (int c = 0; c < n; c++) {
if (cartela[i][j] == cartela[l][c] && (i != l && j != c)) {
soma++;
}
}
}
} while (soma != 0);
}
}
// for (int j = 0; j < jogadores; j++)
// {
for (int l = 0; l < n; l++) {
for (int c = 0; c < n; c++) {
printf("\t %d", cartela[l][c]);
}
printf("\n");
}
// }
//while(1)
//{
//}
return 0;
}
That's my code, and like I said before, the biggest question is how can i use the 3 dimension matrix to get more cards.
A few things ...
The main issue is that you should break down the main into some separate functions.
That way, you can create a function that creates a card. Then, you can call it N times for the number of players.
The card creation you have could take a long time and is very slow. Better to fill the array linearly and then randomly swap cells. This guarantees uniqueness but is much faster.
Here's a refactored version:
#include <stdio.h>
#include <stdlib.h>
int n, soma;
int jogadores;
int menu;
void
card_print(int n,int card[n][n],const char *reason)
{
printf("\n");
printf("%s:\n",reason);
for (int irow = 0; irow < n; ++irow) {
for (int icol = 0; icol < n; ++icol)
printf(" %2d",card[irow][icol]);
printf("\n");
}
}
void
card_init(int n,int card[n][n])
{
int cardno = 0;
for (int irow = 0; irow < n; ++irow) {
for (int icol = 0; icol < n; ++icol)
card[irow][icol] = cardno++;
}
// randomly swap cells in card
int passmax = n * n;
for (int passno = 0; passno < passmax; ++passno) {
int irow = rand() % n;
int jrow = rand() % n;
int icol = rand() % n;
int jcol = rand() % n;
int tmp = card[irow][icol];
card[irow][icol] = card[jrow][jcol];
card[jrow][jcol] = tmp;
}
}
int
main()
{
srand(123);
printf("Número de jogadores: \n");
do {
scanf("%d", &jogadores);
} while (jogadores < 2 || jogadores > 10);
printf("Numero de jogadores salvo \n Escolha a dimensao das cartelas: \n");
do {
scanf("%d", &n);
} while (n < 2 || n > 9);
printf("\n Dimensao das cartelas salva \n ");
//printf(" %d %d \n ",jogadores,n);
//int value = 10 * n;
// I need to add the dimensiona _jogadores_ ==> cartela [jogadores][n][n]
int cartela[n][n];
card_init(n,cartela);
card_print(n,cartela,"Single");
int players[jogadores][n][n];
for (int iplayer = 0; iplayer < jogadores; ++iplayer) {
char msg[100];
card_init(n,players[iplayer]);
sprintf(msg,"Player %d",iplayer);
card_print(n,players[iplayer],msg);
}
return 0;
}
Here's the program output for 3 players and card dimension 7:
Número de jogadores:
Numero de jogadores salvo
Escolha a dimensao das cartelas:
Dimensao das cartelas salva
Single:
13 34 19 44 15 5 47
7 25 32 48 16 28 0
9 11 2 17 18 4 42
36 41 12 23 8 39 6
24 38 31 29 27 20 26
1 21 37 3 14 10 22
46 43 35 45 40 33 30
Player 0:
7 30 17 23 11 2 15
10 38 0 21 34 25 3
6 40 12 46 18 22 41
47 26 19 36 13 16 31
33 28 1 35 27 14 4
20 39 5 8 42 29 32
9 43 44 45 24 37 48
Player 1:
0 8 22 18 2 5 21
20 29 16 10 7 42 24
14 1 37 17 3 13 32
31 6 23 39 15 11 45
28 9 43 25 19 4 36
35 12 47 38 40 30 44
41 33 48 27 46 34 26
Player 2:
40 44 18 3 28 5 43
13 39 42 16 17 4 35
27 23 14 45 37 41 30
21 22 15 9 0 26 47
46 12 11 31 2 7 20
8 29 48 38 33 32 1
24 6 25 36 19 34 10
Related
I'm currently a CS50x student.
I've been toying around with the search and sorting algorithms. Trying to understand them in code. Now, on the topic of bubble sort: I created what I think is a bubble sort algorithm. But I couldn't quite fit in the idea of the swap count that needs to be set to non-zero value. My algorithm (below) does sort all numbers. Where would I fit in the swap idea though? If anyone could be so kind to explain I'd really appreciate it.
#import <stdio.h>
#import <cs50.h>
int main(void)
{
// Creating an unsorted array
int count = 10;
int array[count];
for (int z = 0; z < count; z++)
scanf("%i", &array[z]);
// Bubble Sort
int buffer;
for (int b = 0; b < count; b++)
{
int a = 0;
while (a < count)
{
if (array[a] > array[a+1])
{
buffer = array[a];
array[a] = array[a+1];
array[a+1] = buffer;
}
a++;
}
}
printf("Sorted: ");
for (int b = 0; b < count; b++)
printf("%i ", array[b]);
printf("\n");
}
The directive is #include, not #import. The idea of counting swaps is to break the outer loop if nothing is out of sequence (because there were no swaps needed in the inner loop). This code implements that:
#include <stdio.h>
int main(void)
{
// Creating an unsorted array
int count = 10;
int array[count];
for (int z = 0; z < count; z++)
scanf("%i", &array[z]);
putchar('\n');
printf("%8s:", "Unsorted");
for (int b = 0; b < count; b++)
printf(" %i", array[b]);
printf("\n");
// Bubble Sort
for (int b = 0; b < count; b++)
{
int a = 0;
int num_swaps = 0;
while (a < count - 1)
{
if (array[a] > array[a+1])
{
int buffer = array[a];
array[a] = array[a+1];
array[a+1] = buffer;
num_swaps++;
}
a++;
}
if (num_swaps == 0)
break;
}
printf("%8s:", "Sorted");
for (int b = 0; b < count; b++)
printf(" %i", array[b]);
printf("\n");
return 0;
}
Sample runs (source bs97.c compiled to bs97; home-brew random number generator random — the options used generate 10 numbers between 10 and 99 inclusive):
$ random -n 10 10 99 | bs97
Unsorted: 68 47 85 39 52 54 31 81 19 59
Sorted: 19 31 39 47 52 54 59 68 81 85
$ random -n 10 10 99 | bs97
Unsorted: 75 85 36 11 35 87 59 63 26 36
Sorted: 11 26 35 36 36 59 63 75 85 87
$ random -n 10 10 99 | bs97
Unsorted: 90 27 64 90 76 79 52 46 98 99
Sorted: 27 46 52 64 76 79 90 90 98 99
$ random -n 10 10 99 | bs97
Unsorted: 53 60 87 89 38 68 73 10 69 84
Sorted: 10 38 53 60 68 69 73 84 87 89
$
Note that the code avoids trailing blanks in the output.
You could also define int num_swaps = 1; outside the sorting for loop and test it in the main loop condition:
for (int b = 0; b < count - 1 && num_swaps > 0; b++)
{
num_swaps = 0;
and remove the if (num_swaps == 0) break; from the end of the loop. The inner loop could be a for loop too. And the first cycle of the outer loop moves the largest value to the end of the array, so you can shorten the inner cycle so it has less work to do. The printing code should be factored into a function, too.
#include <stdio.h>
static void dump_array(const char *tag, int size, int data[size])
{
printf("%8s (%d):", tag, size);
for (int i = 0; i < size; i++)
printf(" %i", data[i]);
printf("\n");
}
int main(void)
{
// Creating an unsorted array
int count = 10;
int array[count];
for (int z = 0; z < count; z++)
{
if (scanf("%i", &array[z]) != 1)
{
fprintf(stderr, "Failed to read number %d\n", z + 1);
return 1;
}
}
putchar('\n');
dump_array("Unsorted", count, array);
// Bubble Sort
int num_swaps = 1;
for (int b = 0; b < count - 1 && num_swaps > 0; b++)
{
num_swaps = 0;
for (int a = 0; a < count - b - 1; a++)
{
if (array[a] > array[a + 1])
{
int buffer = array[a];
array[a] = array[a + 1];
array[a + 1] = buffer;
num_swaps++;
}
}
}
dump_array("Sorted", count, array);
return 0;
}
Sample output:
$ random -n 10 10 99 | bs97
Unsorted (10): 31 82 81 40 12 17 70 44 90 12
Sorted (10): 12 12 17 31 40 44 70 81 82 90
$
I'm trying to arrange numbers in a nxn(n is odd number) matrix with bow tie shapes. (like fig.)
Trying to 5x5 matrix set coordinates but no result.
my code:
bowtie {
int a[5][5] = {{
0,
},
{
0,
},
{
0,
},
{
0,
},
{
0,
}};
int i, j;
int num = 1;
for (i = 0; i < 5; i++) {
if (i <= 2) // y>=0 - coordinate(2d)
{
for (j = i; j <= 2; j++) // x<=0, y>=0 Quadrant 2
{
a[i][j] = num;
num++;
}
for (j = 4 - i; j > i; j++) // Quadrant 1
{
a[i][j] = num;
num++;
}
} else // y<0
for (j = 4 - i; j <= 4 - i; j++) // Quadrant 3
{
a[i][j] = num;
num++;
}
for (j = i; j >= i; j++) // Quadrant 2
{
a[i][j] = num;
num++;
}
}
for (i = 0; i < 5; j++) {
for (j = 0; j < 5; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
The loop
for (j = 4 - i; j > i; j++) // Quadrant 1
is wrong because j starts at 4 (i valuing 0) being the last valid index then never stop to grow producing an undefined behavior when you go out of the array
The loop
for (j = 4 - i; j <= 4 - i; j++) // Quadrant 3
is strange because the last possible value is the first one, so this is not a loop but just its body executed with j = 4 - i
The loop
for (j = i; j >= i; j++) // Quadrant 2
is like the first and makes j incompatible with the array dimensions
A proposal where the size in given in argument and can be odd or even :
#include <stdio.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int a[n][n];
int v = 0; /* the value 1.. to put in the cells */
int empty; /* the empty height */
int i,j;
/* first half and may be center */
empty = -1;
for (j = 0; j <= (n-1)/2; ++j) {
empty += 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = ++v;
}
if ((n & 1) == 0)
empty += 1;
/* second half */
for (; j < n; ++j) {
empty -= 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = ++v;
}
/* show result */
for (i = 0; i != n; ++i) {
for (j = 0; j != n; ++j) {
if (a[i][j] == 0)
fputs(" ", stdout); /* witdh = 4 compatible with a size up to 43 */
else
printf("% 4d", a[i][j]); /* width = 4 compatible with a size up to 43 */
}
putchar('\n');
}
}
}
return 0;
}
Compilation and executions :
pi#raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall b.c
pi#raspberrypi:/tmp $ ./a.out 1
1
pi#raspberrypi:/tmp $ ./a.out 2
1 3
2 4
pi#raspberrypi:/tmp $ ./a.out 3
1 5
2 4 6
3 7
pi#raspberrypi:/tmp $ ./a.out 4
1 9
2 5 7 10
3 6 8 11
4 12
pi#raspberrypi:/tmp $ ./a.out 5
1 13
2 6 10 14
3 7 9 11 15
4 8 12 16
5 17
pi#raspberrypi:/tmp $ ./a.out 6
1 19
2 7 15 20
3 8 11 13 16 21
4 9 12 14 17 22
5 10 18 23
6 24
pi#raspberrypi:/tmp $ ./a.out 7
1 25
2 8 20 26
3 9 13 17 21 27
4 10 14 16 18 22 28
5 11 15 19 23 29
6 12 24 30
7 31
If you do not accept even size
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1) || ((n & 1) == 0))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int a[n][n];
int v = 1; /* the value 1.. to put in the cells */
int empty; /* the empty height */
/* first half more center */
empty = -1;
for (int j = 0; j <= n/2; ++j) {
int i;
empty += 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = v++;
}
/* second half */
for (int j = n/2 + 1; j < n; ++j) {
int i;
empty -= 1;
for (i = 0; i != empty; ++i)
a[i][j] = a[n - i - 1][j] = 0;
for (int k = n - empty*2; k; --k)
a[i++][j] = v++;
}
/* show result */
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
if (a[i][j] == 0)
fputs(" ", stdout);
else
printf("% 4d", a[i][j]);
}
putchar('\n');
}
}
}
return 0;
}
Compilation and executions :
pi#raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall o.c
pi#raspberrypi:/tmp $ ./a.out
Usage ./a.out <size>
pi#raspberrypi:/tmp $ ./a.out 1
1
pi#raspberrypi:/tmp $ ./a.out 2
invalid size 2
pi#raspberrypi:/tmp $ ./a.out 3
1 5
2 4 6
3 7
pi#raspberrypi:/tmp $ ./a.out 5
1 13
2 6 10 14
3 7 9 11 15
4 8 12 16
5 17
pi#raspberrypi:/tmp $ ./a.out 17
1 145
2 18 130 146
3 19 33 117 131 147
4 20 34 46 106 118 132 148
5 21 35 47 57 97 107 119 133 149
6 22 36 48 58 66 90 98 108 120 134 150
7 23 37 49 59 67 73 85 91 99 109 121 135 151
8 24 38 50 60 68 74 78 82 86 92 100 110 122 136 152
9 25 39 51 61 69 75 79 81 83 87 93 101 111 123 137 153
10 26 40 52 62 70 76 80 84 88 94 102 112 124 138 154
11 27 41 53 63 71 77 89 95 103 113 125 139 155
12 28 42 54 64 72 96 104 114 126 140 156
13 29 43 55 65 105 115 127 141 157
14 30 44 56 116 128 142 158
15 31 45 129 143 159
16 32 144 160
17 161
pi#raspberrypi:/tmp $
From you remark
'int a[n][n];' has problem.
probably you compiled in C++ rather than C, but it is easy to change that :
replace int a[n][n]; by int * a = malloc(n*n*sizeof(int));
replace each form a[x][y] by a[(x)*n+y]
add a free(a); at the end
For instance if I do that on the proposal only accepting odd size :
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
if (argc != 2)
printf("Usage %s <size>\n", *argv);
else {
int n;
if ((sscanf(argv[1], "%d", &n) != 1) || (n < 1) || ((n & 1) == 0))
fprintf(stderr, "invalid size %s\n", argv[1]);
else {
int * a = malloc(n*n*sizeof(int));
int v = 1; /* the value 1.. to put in the cells */
int empty; /* the empty height */
/* first half more center */
empty = -1;
for (int j = 0; j <= n/2; ++j) {
int i;
empty += 1;
for (i = 0; i != empty; ++i)
a[i*n+j] = a[(n - i - 1)*n+j] = 0;
for (int k = n - empty*2; k; --k)
a[i++*n+j] = v++;
}
/* second half */
for (int j = n/2 + 1; j < n; ++j) {
int i;
empty -= 1;
for (i = 0; i != empty; ++i)
a[i*n+j] = a[(n - i - 1)*n+j] = 0;
for (int k = n - empty*2; k; --k)
a[i++*n+j] = v++;
}
/* show result */
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
if (a[i*n+j] == 0)
fputs(" ", stdout);
else
printf("% 4d", a[i*n+j]);
}
putchar('\n');
}
free(a);
}
}
return 0;
}
I have been trying to solve the problem in C.
I can travel through my 2D array diagonally with this code:
for(int k = 0; k<10*2; k++) {
for(int j = 0; j<=k; j++) {
int i = k-j;
if (i <10 && j<10) {
printf("%d ", tomb[i][j]);
}
}
printf("\n");
}
So if my 2D array (tomb) is:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
Then my output for diagonal ( / shape):
0
5 1
10 6 2
15 11 7 3
20 16 12 8 4
21 17 13 9
22 18 14
23 19
24
My questions is, how I could travel through this array in reverse diagonal ( \ shape), so that my output would look like this:
4
3 9
2 8 14
1 7 13 19
0 6 12 18 24
5 11 17 23
10 16 22
15 21
20
Considering the square matrix of dimension = size x size here is the code
for(int i = 0; i < size; ++i) {
int j = size - i - 1;
for(int k = 0; k <= i; ++k,++j) {
printf("%d ",tomb[k][j]);
}
printf("\n");
}
for(int i = 1; i < size; ++i) {
int j = 0;
for(int k = i; k < size ; k++, j++) {
printf("%d ", tomb[k][j]);
}
printf("\n");
}
Whole code demo
https://ide.geeksforgeeks.org/fq59Cm8Hqt Only the code is in Java
Nevermind. The answer is:
for(int k = -5; k<=5; k++) {
for(int j = 0; j<5; j++) {
if ((j-k>=0) &&(j-k<5)) {
printf("%d ", tomb[j][j-k]);
}
}
printf("\n");
}
# include <stdio.h>
# include <stdlib.h>
int main(void)
{
int s;
int row;
int column;
int k;
int array[99][99] ;
printf("Enter the dimension of the square : ") ;
scanf("%d", &s) ;
if (s % 2 == 0)
{
printf("Please enter an even number") ;
goto last;
}
column = (s + 1) / 2 ;
row = 1 ;
int sqr1 = s*s;
for(k = 1 ; k <= sqr1 ; k++)
{
array[row][column] = k ;
if(k % s == 0)
{
row = (row + 1);
goto loop ;
}
if(row == 1)
row = s ;
else
row = row - 1 ;
if(column == s)
column = 1;
else
column = column + 1 ;
loop : ;
}
for (row = 1 ; row <= s ; row++)
{
for (column = 1 ; column <= s ; column++)
{
printf("%d\t", array[row][column]) ;
}
printf("\n\n") ;
}
last : ;
return 0;
}
I was wondering if anyone could tell me where the code puts the number down. Say I wanted a 3x3 magic square. The output would be:
https://i.stack.imgur.com/BYTSn.png
I was wondering where in the code it would move the 4 down because the 1 is already there. Same thing with the 7 moving down. The principle is you go 1 up and 1 to the right everytime and if there is something there you move down and keep going.
What About:
#include <stdio.h>
#include <string.h>
#define N (3)
int main( int argc, char * argv[] )
{
int square[ N ][ N ];
int i = N / 2;
int j = N - 1;
int num = 1;
memset( square, 0, sizeof(square) );
while( num <= N * N )
{
if( (i == -1) && (j == N) )
{
i = 0;
j = N - 2;
}
else
{
if( i < 0 )
i = N - 1;
if( j == N )
j = 0;
}
if( square[i][j] )
{
i++;
j = j - 2;
continue;
}
else
{
square[i][j] = num;
num++;
}
j++;
i--;
}
printf("N = %d\n", N );
printf("Sum = %d\n\n", N * (N * N + 1) / 2 );
for( i = 0; i < N; i++ )
{
for( j = 0; j < N; j++ )
printf("%3d ", square[i][j]);
printf("\n");
}
return 0;
}
Outputs:
N = 3
Sum = 15
2 7 6
9 5 1
4 3 8
N = 5
Sum = 65
9 3 22 16 15
2 21 20 14 8
25 19 13 7 1
18 12 6 5 24
11 10 4 23 17
N = 7
Sum = 175
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
N = 9
Sum = 369
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
References:
https://en.wikipedia.org/wiki/Siamese_method
https://en.wikipedia.org/wiki/Magic_square
I know how to transpose a matrix, But how to transpose a matrix with odd row Reversed form.
Example 3*3 Matrix
1 2 3
4 5 6
7 8 9
Output
1 6 7
2 5 8
3 4 9
Here is a solution that breaks the problem into smaller subproblems. The function reverse_row() is a function that reverses an array in place, and the function transpose_array() transposes an array in place.
The first function, reverse_row() is called in a loop on the rows of the 2d array with odd indices, then the transpose_array() function is called on the resulting array. Note that the test if (i % 2) {} is used to determine if an array index is odd or even, since i % 2 evaluates to 0 only when i is even; you could also avoid this test and simply increment i by two at each iteration (starting from 1), as suggested by #Lưu Vĩnh Phúc in the comments:
for (size_t i = 1; i < MATRIX_SZ; i += 2) {
reverse_row(MATRIX_SZ, array[i]);
}
Also note that to transpose a square matrix in place, you do not need to iterate over all of the elements, but only the elements above or below the diagonal, swapping with the appropriate element. In this case, I have chosen to iterate over the elements below the diagonal, swapping with the corresponding element above the diagonal. Of course, the elements on the diagonal remain unchanged so are not iterated over at all.
Here is the code, using a 4X4 array as input. This code works for square matrices, and could be adapted to work for rectangular matrices. This would require care in choosing the size of the array used to represent the matrix, or dynamic allocation.
#include <stdio.h>
#define MATRIX_SZ 4
void print_array(size_t n, int arr[n][n]);
void reverse_row(size_t n, int arr[n]);
void transpose_array(size_t n, int arr[n][n]);
int main(void)
{
int array[MATRIX_SZ][MATRIX_SZ] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
puts("Before transformation:");
print_array(MATRIX_SZ, array);
putchar('\n');
for (size_t i = 0; i < MATRIX_SZ; i++) {
if (i % 2) {
reverse_row(MATRIX_SZ, array[i]);
}
}
transpose_array(MATRIX_SZ, array);
puts("After transformation:");
print_array(MATRIX_SZ, array);
putchar('\n');
return 0;
}
void print_array(size_t n, int arr[n][n])
{
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
printf("%5d", arr[i][j]);
}
putchar('\n');
}
}
void reverse_row(size_t n, int arr[n])
{
size_t mid = n / 2;
for (size_t i = 0; i < mid; i++) {
size_t swap_dx = n - 1 - i;
int temp = arr[i];
arr[i] = arr[swap_dx];
arr[swap_dx] = temp;
}
}
void transpose_array(size_t n, int arr[n][n])
{
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < i; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
And here is the program output:
Before transformation:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After transformation:
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
If I have understood the assignment correctly then the program can look the following way.
#include <stdio.h>
#define MAX_VALUE 100
int main(void)
{
while ( 1 )
{
printf( "Enter the size of an array (0 - Exit): " );
size_t n;
if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;
putchar('\n');
n %= MAX_VALUE;
int a[n][n];
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ )
{
a[i][j] = n * i + j;
}
}
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
printf("%3d ", a[i][j]);
}
putchar('\n');
}
putchar('\n');
for (size_t i = 0; i < n; i++)
{
for (size_t j = i; j < n; j++)
{
if (j % 2 == 1 && i < n / 2)
{
int tmp = a[j][i];
a[j][i] = a[j][n - i - 1];
a[j][n - i - 1] = tmp;
}
if (i != j)
{
int tmp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = tmp;
}
}
}
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
printf("%3d ", a[i][j]);
}
putchar('\n');
}
putchar('\n');
}
return 0;
}
Its output might look like
Enter the size of an array (0 - Exit): 10
0 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 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
0 19 20 39 40 59 60 79 80 99
1 18 21 38 41 58 61 78 81 98
2 12 22 37 42 57 62 77 82 97
3 13 23 36 43 56 63 76 83 96
4 14 24 34 44 55 64 75 84 95
5 15 25 35 45 54 65 74 85 94
6 16 26 33 46 53 66 73 86 93
7 17 27 32 47 52 67 72 87 92
8 11 28 31 48 51 68 71 88 91
9 10 29 30 49 50 69 70 89 90
Enter the size of an array (0 - Exit): 3
0 1 2
3 4 5
6 7 8
0 5 6
1 4 7
2 3 8
Enter the size of an array (0 - Exit): 0
The compiler should support Variable Length Arrays. Otherwise you can rewrite the program for an array with a fixed size.