scanf() taking more inputs than expected [closed] - arrays

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I was attempting a problem to check symmetry in logos (link : https://www.hackerearth.com/practice/data-structures/arrays/multi-dimensional/practice-problems/algorithm/roy-and-symmetric-logos-1/description/). This is the function accepting input of the array called logo[][]:
void take_Logo(int logo[N_Max][N_Max], int N) //N is number of rows and columns
{
int i = 0, j = 0;
char in[80];
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
//gets(in); //error in input here
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}
}
The trouble is that when I run the code, for N = 2, it takes 4 inputs:
2
01
01
01
01
How do I correct it?
I am entering the 01's as a binary string.
Here is the complete code:
#include<stdio.h>
#define N_Max 32
int check_Symm(int logo[N_Max][N_Max], int N);
void take_Logo(int logo[N_Max][N_Max], int N);
int main(void)
{
int i = 0, T, N, logo[N_Max][N_Max];
scanf("%d", &T);
i = T;
while(i > 0)
{
--i;
scanf("%d", &N);
take_Logo(logo,N);
//check_Symm(logo, N) == 0 ? printf("NO\n") : printf("YES\n");
for(int k = 0; k<N; k++)
{
for(int l = 0; l<N; l++)
{
printf("%d ", logo[k][l]);
}
printf("\n");
}
}
}
void take_Logo(int logo[N_Max][N_Max], int N)
{
int i = 0, j = 0;
char in[80];
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
//gets(in); //error in input here
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}
}
int check_Symm(int logo[N_Max][N_Max], int N)
{
int test = 1;
if(N % 2 == 0)
{
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[i][j] == logo[N-1-i][j])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[j][i] == logo[j][N-1-i])
continue;
else
{
test = 0;
return test;
}
}
}
}
else
{
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[i][j] == logo[N-1-i][j])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i = 0; i < N/2; i++)
{
for(int j = 0; j<N; j++)
{
if(logo[j][i] == logo[j][N-1-i])
continue;
else
{
test = 0;
return test;
}
}
}
for(int i=0; i < N/2 ; i++)
{
if(logo[N/2][i] == logo[N/2][N-i-1])
continue;
else
{
test = 0;
return test;
}
}
for(int i=0; i < N/2 ; i++)
{
if(logo[i][N/2] == logo[N-i-1][N/2])
continue;
else
{
test = 0;
return test;
}
}
}
return test;
}

The trouble is that when I run the code, for N = 2, it takes 4 inputs:
This is expected for N == 2:
// Do loop twice
for(i = 0; i<N; i++) {
// Do loop twice
for(j = 0; j<N; j++) {
// Read 2 * 2 times
scanf("%s", in);
logo[i][j] = in[j] - '0';
}
}

Related

Why when I try to run this code online it always generates runtime error/run error. but when I run it in command prompt it works [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
This code is a program that will move the index position of several names according to the size of the toga that each participant has, and according to the name that the user wants to change.
#include <stdio.h>
#include <string.h>
typedef struct
{
char nama[100];
char ukuran[100];
} toga;
int main()
{
int n, m;
scanf("%d%d", &n, &m);
toga matriks[n][m];
int jumlah[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%s%s", &matriks[i][j].nama, &matriks[i][j].ukuran);
if(strcmp(matriks[i][j].ukuran, "XS") == 0)
{
jumlah[i][j] = 32;
}
else if(strcmp(matriks[i][j].ukuran, "S") == 0)
{
jumlah[i][j] = 36;
}
else if(strcmp(matriks[i][j].ukuran, "M") == 0)
{
jumlah[i][j] = 37;
}
else if(strcmp(matriks[i][j].ukuran, "L") == 0)
{
jumlah[i][j] = 38;
}
else if(strcmp(matriks[i][j].ukuran, "XL") == 0)
{
jumlah[i][j] = 40;
}
else if(strcmp(matriks[i][j].ukuran, "XXL") == 0)
{
jumlah[i][j] = 42;
}
else if(strcmp(matriks[i][j].ukuran, "XXXL") == 0)
{
jumlah[i][j] = 45;
}
}
}
maybe this part is the problem?
toga ditukar1[50][50];
toga ditukar2[50][50];
int cek2 = 0;
for (int i = 0; i < 50; i++)
{
for(int j = 0; j < 1; j++)
{
scanf("%s", &ditukar1[i][0].nama);
if(strcmp(ditukar1[i][0].nama, "Selesai") == 0)
{
j = 1;
i = 50;
}
else
{
scanf("%s ", &ditukar1[i][1].nama);
// cek1++;
}
}
cek2++;
}
cek2 -= 1;
// printf("ini cek1 : %d\n", cek1);
// printf("ini cek2 : %d\n", cek2);
toga matriks_baru[m][n];
for (int i = 0, a = 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len[m];
max_len[0] = 0;
int terpanjang[m];
int a = 0;
for (int i = 0; i < m; i++)
{
max_len[a] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len[a])
{
max_len[a] = strlen(matriks_baru[i][j].nama);
}
}
a++;
}
printf("Posisi awal :\n");
for(int i = 0; i < n; i++)
{
a = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len[a]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
a++;
}
printf("\n");
}
printf("\n");
int baris1, baris2;
int kolom1, kolom2;
int k = 0;
int l = 0;
maybe this part is the problem?
here I will check whether the name entered by the user whose index you want to exchange is in the matrix
for(int l = 0; l < cek2; l++)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
for(int a = 0; a < n; a++)
{
for(int b = 0; b < m; b++)
{
if(strcmp(ditukar1[l][0].nama, matriks[i][j].nama) == 0)
{
baris1 = i;
kolom1 = j;
if(strcmp(ditukar1[l][1].nama, matriks[a][b].nama) == 0)
{
// printf("%d\n", l);
baris2 = a;
kolom2 = b;
if(baris1 < baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris1][kolom1] > jumlah[baris2][kolom2])
{
temp2 = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = temp2;
strcpy(temp1, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, temp1);
}
}
else if(baris1 > baris2)
{
char temp1[] = "0";
int temp2 = 0;
if(jumlah[baris2][kolom2] > jumlah[baris1][kolom1])
{
temp2 = jumlah[baris2][kolom2];
jumlah[baris2][kolom2] = jumlah[baris1][kolom1];
jumlah[baris1][kolom1] = temp2;
strcpy(temp1, matriks[baris1][kolom1].nama);
strcpy(matriks[baris1][kolom1].nama, matriks[baris2][kolom2].nama);
strcpy(matriks[baris2][kolom2].nama, temp1);
}
}
}
}
}
}
}
}
}
for (int i = 0, a= 0; i < m && a < m; i++, a++)
{
for (int j = 0, b = 0; j < n && b < n; j++, b++)
{
strcpy(matriks_baru[a][b].nama ,matriks[j][i].nama);
}
}
int max_len2[m];
max_len2[0] = 0;
int s = 0;
for (int i = 0; i < m; i++)
{
max_len2[s] = 0;
for (int j = 0; j < n; j++)
{
if(strlen(matriks_baru[i][j].nama) > max_len2[s])
{
max_len2[s] = strlen(matriks_baru[i][j].nama);
}
}
s++;
}
printf("Posisi akhir :\n");
for(int i = 0; i < n; i++)
{
s = 0;
for(int j = 0; j < m; j++)
{
if(j != m-1)
{
printf("%s ", matriks[i][j].nama);
}
else
{
printf("%s", matriks[i][j].nama);
}
if(j != m-1)
{
for(int k = 0; k < (max_len2[s]) - (strlen(matriks[i][j].nama)); k++)
{
printf(" ");
}
}
s++;
}printf("\n");
}
return 0;
}

Sort a matrix row wise (c programming)

I wrote this code for sorting an nxn matrix: odd rows in descending order, double rows in ascending order, but it doesn't pass the compiler stage.
What did I do wrong?
It mostly tells me this: assignment to 'int' from 'int *' makes integer from pointer without a cast (how can I solve this problem)?
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if (a < b) {
return 1;
} else {
return 0;
}
}
int comp_d(int a, int b) {
if (a > b) {
return 1;
} else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (comp(a[j], a[j + 1]) == 0) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
int main() {
int n;
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
int *mat = (int*)calloc(n, sizeof(int));
for (int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
for (int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for (int i = 0; i < n; i++) {
if (i%2 == 0) {
sort(mat[i], n, &comp_a);
} else {
sort(mat[i], n, &comp_d);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
You should change your matrix allocation to this:
int **mat = (int**)calloc(n, sizeof(int*)); // (1)
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int)); // (2)
}
(1): Declaring an array of pointers of size n x int*.
(2): Where each pointer in this array points to an array of integers of size n x int.
I changed your matrix allocation
int *mat = (int*)calloc(n, sizeof(int));
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
to
int mat[n][n];
I changed for loop of ordred
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1; j++) {
if(comp(a[j], a[j + 1]) == 0)
because he take a lot of time (time of running) to
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0)
my code:
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if(a < b) {
return 1;
}
else {
return 0;
}
}
int comp_d(int a, int b) {
if(a > b) {
return 1;
}
else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0) {
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
}
int main() {
int n;
do
{
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
}while(n<1);
int mat[n][n];
for(int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for(int i = 0; i < n; i++) {
if(i%2 == 0) {
sort(mat[i], n, &comp_a);
}
else {
sort(mat[i], n, &comp_d);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}

How to changing size of dynamically allocated matrix?

I have an C exercice at school. I had to make a dynamically allocated matrix filled with random number and which have a size choosing by user. Then, the program ask to the user to redefine a bigger size, and the new matrix have to keep old values and fill new values with random.
The program is blocking when I'm trying to redefine the size of the matrix. I'm receiving a segmentation fault as of line 73. I dont understand why :|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stddef.h>
int nbRand() {
static int first = 0;
if(first == 0) {
srand(time(NULL));
first = 1;
}
return rand()%10;
}
int main(int argc, char *argv[] ){
int **matrice, **matrice2; // Tableau
int i, j; // Compteur
int longueur1, largeur1, longueur2, largeur2; // Taille dynamique tableau
size_t n;
printf("Saisissez la taille du tableau.\n");
printf("Longueur : ");
scanf("%d", &longueur1);
printf("Largeur : ");
scanf("%d", &largeur1);
matrice = (int**) malloc(longueur1*sizeof(int*));
for(i = 0; i < longueur1; i++) {
matrice[i] = (int*) malloc(largeur1*sizeof(int));
}
/* Autre manière
matrice = (int*) malloc(longueur1*largeur1*sizeof(int));
*/
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
matrice[i][j] = nbRand();
}
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
// Agrandir la matrice
printf("NOUVELLE TAILLE\n");
do {
printf("Longueur : ");
scanf("%d", &longueur2);
} while(longueur2 < longueur1);
do {
printf("Largeur : ");
scanf("%d", &largeur2);
} while(largeur2 < largeur1);
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = (int*) realloc(matrice[i], largeur2*sizeof(int));
}
matrice=matrice2;
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
matrice[i][j] = nbRand();
printf("%d\t%d\t%d\n", i, j, matrice[i][j]);
}
}
printf("test\n");
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
EDITED :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stddef.h>
int nbRand() {
static int first = 0;
if(first == 0) {
srand(time(NULL));
first = 1;
}
return rand()%10;
}
int main(int argc, char *argv[] ){
int** matrice;
int** tmp; // Tableau
int i, j; // Compteur
int longueur1, largeur1, longueur2, largeur2; // Taille dynamique tableau
size_t n;
printf("Saisissez la taille du tableau.\n");
printf("Longueur : ");
scanf("%d", &longueur1);
printf("Largeur : ");
scanf("%d", &largeur1);
matrice = (int**) malloc(longueur1*sizeof(int*));
for(i = 0; i < longueur1; i++) {
*(matrice+i) = (int*) malloc(largeur1*sizeof(int));
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
matrice[i][j] = nbRand();
}
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
// Agrandir la matrice
printf("NOUVELLE TAILLE\n");
do {
printf("Longueur : ");
scanf("%d", &longueur2);
} while(longueur2 < longueur1);
do {
printf("Largeur : ");
scanf("%d", &largeur2);
} while(largeur2 < largeur1);
tmp = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
*(tmp + i) = (int*) realloc(*(tmp+i), largeur2*sizeof(int));
}
matrice=tmp;
for(i = longueur1; i < longueur2; i++) {
for(j = largeur2; j < largeur2; j++) {
matrice[i][j] = nbRand();
// printf("%d\t%d\t%d\n", i, j, matrice[i][j]);
}
}
printf("test\n");
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = (int*) realloc(matrice[i], largeur2*sizeof(int));
^^^^^^^^^^
|
-> You are accessing old pointer here.
}
is wrong, it should be
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = realloc(matrice2[i], largeur2*sizeof(int));
}

In C program which find union and intersection of two input arrays, I have a problem in intersection part

In C program which find union and intersection of two input arrays, I have a problem in intersection part. The problem is when I print out the intersection array I[u + z], the output is only one number repeated, without printing other intersections of the two arrays.the part of code has the problem is at the end of the code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, z, f ;
printf("Enter size of array\n");
scanf("%d", &n);
int a[n];
if(n <= 20) {
for(int i = 0 ; i < n; i++) {
printf("Enter integer \n");
scanf("%d", &a[i]);
}
}
for(int i = 0 ; i < n; i++) {
printf("%d " ,a[i]);
}
printf("\nEnter size of the 2nd array\n");
scanf("%d", &z);
int b[z];
if(z <= 20 ){
for(int i = 0 ; i < z; i++) {
printf("Enter integer \n");
scanf("%d", &b[i]);
}
}
for(int i = 0 ; i < z; i++) {
printf("%d " ,b[i]);
}
for(int i = 0 ; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(a[i] == a[j]) {
for(int l = j; l < n; l++)
{
a[l] = a[l + 1];
}
n--;
j--;
}
}
}
printf("\nArray1: ");
for(int i = 0 ; i < n; i++) {
printf("%d ", a[i]);
}
for(int t = 0; t < z; t++) {
for(int u = t + 1; u < z; u++) {
if(b[t] == b[u]) {
for(int l = u; l < z; l++)
{
b[l] = b[l + 1];
}
z--;
u--;
}
}
}
printf("\nArray2: ");
for(int e = 0; e < z; e++) {
printf("%d ", b[e]);
}
int u[n + z] ; //union
for(int i = 0; i < n; i++) {
u[i] = a[i];
}
for(int i = n ; i < n + z; i++) {
u[i] = b[i - n];
}
for(int i = n ; i < n + z; i++) { //remove repeated values from union
for(int j = i + 1; j < n + z; j++) {
if(u[i] == u[j]) {
for(int l = j; l < n + z; l++)
{
u[l] = u[l + 1];
}
n + z--;
j--;
}
}
}
printf("\n union is ");
for(int i = n ; i < n + z; i++) {
printf("%d ",u[i]);
}
// to get intersection
int I[n + z]; // to get intersection
for(int i = 0; i < n; i++) {
for(int j = 0; j < z; j++) {
if(a[i] == b[j]) {
for(int k = 0; k < n + z; k++) {
I[k] = a[i];
}
}
}
}
printf("\n intersection is ");
for(int i = 0; i < n + z; i++) {
printf("%d ", I[i]);
}
return 0;
}
You should add only one number when you find equality, not for the whole array.
// to get intersection
int I[n + z];
k = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < z; j++)
if(a[i] == b[j]) {
I[k++] = a[i];
break;
}
printf("\n intersection is ");
for(int i = 0; i < k; i++)
printf("%d ", I[i]);
#include <stdio.h>
int main() {
int a, b, A[100], B[100], I[100], i, j, k = 0, l; /* i have also taken
intersection array. as I[100] */
printf("pls enter the a and b respectively ..\n");
scanf("%d%d", & a, & b);
printf("pls enter the elements in A\n");
for (i = 0; i < a; i++) {
scanf("%d", & A[i]);
}
printf("pls enter the elements in B\n");
for (i = 0; i < b; i++) {
scanf("%d", & B[i]);
}
// intersection calculation
printf("intersection =");
printf("{");
for (i = 0, k = 0; i < a; i++) {
for (j = 0; j < b; j++) {
if (A[i] == B[j]) {
printf("%d,", A[i]);
I[k] = A[i];
k++;
}
}
}
printf("}\n");
// lets find union
printf("union=");
printf("{");
for (i = 0; i < a; i++) {
for (j = 0, l = 0; j < k; j++) {
if (A[i] != I[j]) // this is A-B
{
l++;
if (l == (k)) {
printf("%d,", A[i]);
}
}
}
}
for (i = 0, k = 0; i < a; i++) {
for (j = 0; j < b; j++) {
if (A[i] == B[j]) {
printf("%d,", A[i]); // intersection
I[k] = A[i];
k++;
}
}
}
for (i = 0; i < b; i++) {
for (j = 0, l = 0; j < k; j++) {
if (B[i] != I[j]) {
l++;
if (l == (k)) {
printf("%d,", B[i]); // this is B-A
}
}
}
}
printf("}\n");
// union calculation ended here
printf("A-B = ");
printf("{");
for (i = 0; i < a; i++) {
for (j = 0, l = 0; j < k; j++) // A-B calculation
{
if (A[i] != I[j]) {
l++;
if (l == (k)) {
printf("%d,", A[i]);
}
}
}
}
printf("}\n");
printf("B-A = "); // Calculates B-A
printf("{");
for (i = 0; i < b; i++) {
for (j = 0, l = 0; j < k; j++) {
if (B[i] != I[j]) {
l++;
if (l == (k)) {
printf("%d,", B[i]);
}
}
}
}
printf("}\n");
return 0;
}

Array rotation in C

I am trying to rotate an array that looks like this:
a a a a
a b a a
b b b a
a a a a
I am supposed to rotate it 5 times for 90 degrees. It is supposed to be done in C.
I appreciate every help because I am just a beginner and am stuck on this.
Thanks in advance.
#include <stdio.h>
int main()
{
char array_1[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'o','o','o','-'},
{'-','-','-','-'}};
char array_2[4][4] = { {'-','-','-','-'},
{'-','o','o','-'},
{'o','o','-','-'},
{'-','-','-','-'}};
char array_3[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'-','o','-','-'},
{'-','o','o','-'}};
char array_4[4][4] = { {'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'}};
int counter = 0;
int counter_1 = 0;
for(counter = 0; counter < 4; counter++)
{
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_1[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_2[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_3[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_4[counter][counter_1]);
}
printf(" ");
printf("\n");
}
printf("\n");
for(counter= 0; counter < 4; counter++)
{
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_1[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_2[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_3[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_4[counter_1][counter]);
}
printf(" ");
printf("\n");
}
printf("\n");
like this:
#include <stdio.h>
typedef struct point { int x, y; } Point;
void rotate(int n, char array[n][n]){
//rotate right 90 degrees
if(n == 1) return ;
int times = n / 2;
for(int i = 0; i < times; ++i){
Point base = { i, i };
for(int j = 0; j < n - 1; ++j){
Point transition[4] = { {j, n-1}, {n-1,n-1-j},{n-1-j,0},{0,j} };
char curr = array[base.x][base.y+j];//base + {0,j}
for(int k = 0; k < 4; ++k){
char temp = array[base.x + transition[k].x][base.y + transition[k].y];
array[base.x + transition[k].x][base.y + transition[k].y] = curr;
curr = temp;
}
}
n -= 2;
}
}
void display(int n, char array[n][n]){
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(j)
putchar(' ');
putchar(array[i][j]);
}
putchar('\n');
}
putchar('\n');
}
int main(void){
//demo
char array4[4][4] = {
{'1','2','3','4'},
{'5','6','7','8'},
{'9','A','B','C'},
{'D','E','F','0'}
};
display(4, array4);
int n = 4;
while(n--){
rotate(4, array4);
display(4, array4);
}
char array5[5][5] = {
{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','W','X','Y'}
};
display(5, array5);
n = 4;
while(n--){
rotate(5, array5);
display(5, array5);
}
}

Resources