I have a problem with a task for my IT school. The problem is : Turn the frame of an array to left. Input : First get the numbers of tests (t). Then for each test get l and k (line and column), 3 <= l, k <= 100. Then fill the matrix with numbers from user.
Input:
1
3 3
1 2 3
4 5 6
7 8 9
Output:
2 3 6
1 5 9
4 7 8
My code so far :
#include<iostream>
#include<cstdio>
#include<cstdlib>
int main()
{
int t, w, k;
int tab[101][101];
int t1[101], t2[101], t3[101], t4[101];
scanf_s("%d", &t);
for (int i = 0; i < t; i++) {
scanf_s("%d %d", &w, &k);
for (int j = 0; j < w; j++) {
for (int x = 0; x < k; x++) {
scanf_s("%d", &tab[j][x]);
if (j == 0) { //1 linia
t1[x] = tab[j][x];
}
if (j + 1 == w) { //3 linia
t2[x] = tab[j][x];
}
if (x == 0) { //2 linia
t3[j] = tab[j][x];
}
if (x + 1 == k) { //4 linia
t4[j] = tab[j][x];
}
}
}
printf("\n");
}
for (int j = 0; j < w; j++) {
for (int x = 0; x < k; x++) {
if (j == 0) {
if (x == 0) {
tab[j][x] = t3[1];
}
else if (x + 1 != k-1) {
tab[j][x] = t1[j + 1];
}
else if (x + 1 == k-1) {
tab[j][x] = t4[1];
}
}
if (j + 1 == w) {
if (x == 0) {
tab[j][x] = t3[k - 2];
}
else if (x + 1 == k - 1) {
tab[j][x] = t4[w-2];
}
else if (x + 1 != k-1) {
tab[j][x] = t2[x + 1];
}
}
}
}
for (int j = 0; j < w; j++) {
for (int x = 0; x < k; x++) {
printf("%d ", tab[j][x]);
}
printf("\n");
}
printf("\n");
system("pause");
return 0;
}
I know i'm doing the repositioning wrong. I tried it for like 5 different ways now. If anyone would show me a way of iterating through the table moving values to the left. I would be grateful. Also have in mind that l dont have to be equal to k.
You marked the question with C++ tag though I do not see neither C++ except some unused headers.:)
So I have written my demonstrative program In C.
If I have understood correctly you need something like the following. Only I have not entered values of the array. The array is set initially.
The output is
If it is what you need then you can modify the program according to your requirements.:)