-
Notifications
You must be signed in to change notification settings - Fork 4
/
7-2D-array-into-linear-array.c
54 lines (45 loc) · 1.08 KB
/
7-2D-array-into-linear-array.c
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
/*Copy elements of a 2D array into a 1D/linear array and print the elements of group 3 from the 1D array.*/
#include<stdio.h>
#define n 3
void print1DArray(int arr[])
{
int x = n*n, i;
for(i=0; i<x; i++){
printf(" %d ", arr[i]);
}
}
int main()
{
int array[n][n], rowArray[n*n], colArray[n*n], i, j, k=0, l=0;
printf(" Enter 2D array elements (Row = 3, Col=3) \n");
for(i=0; i<n; i++){
for(j=0; j<n; j++){
scanf("%d", &array[i][j]);
}
}
printf("\n\n 2D ARRAY: \n");
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf(" %d ", array[i][j]);
}
printf("\n");
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
rowArray[k] = array[i][j];
k++;
}
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
colArray[l] = array[j][i];
l++;
}
}
printf("\n\n Linear array (Row wise)\n");
print1DArray(rowArray);
printf("\n\n Linear array (Row wise)\n");
print1DArray(colArray);
printf("\n");
return 0;
}