Write a program that transfers a two-dimensional array R and C (maximum dimensions: 10 rows and 10 columns) into a one-dimensional array V of size R * C.
\(\begin{pmatrix} a & b & c & d \\e & f & g & h \\ i & j & k & l \end{pmatrix}\Rightarrow (a \ b \ c \ d \ e \ f \ g \ h \ i \ j \ k \ l )\)
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define SIZE 10
void main()
{
char A[SIZE][SIZE], T[SIZE*SIZE];
int R, C; // dimension of the A
int i, j, k; // counters
do {
printf("Enter the number of rows: ");
scanf("%d", &R);
} while (R <= 0 || R > SIZE);
do {
printf("Enter the number of columns: ");
scanf("%d", &C);
} while (C <= 0 || C > SIZE);
for (i = 0; i<R; i++)
for (j = 0; j < C; j++)
{
printf("Enter A[%d][%d]=", i, j);
scanf("\n%c", &A[i][j]);
}
printf("\n\nPrinting the array\n");
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
{
printf("%7c", A[i][j]);
}
printf("\n");
}
// solution 1
/*
k = 0;
for (i = 0;i < R;i++)
for (j = 0;j < C;j++)
T[k++] = A[i][j];
*/
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
T[i*C + j] = A[i][j];
printf("\n\nPrinting the array :\n");
for (i = 0; i < R*C; i++)
printf("%4c", T[i]);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Length of the longest ascending sequence of characters