1

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.

Setzo
  • 39
  • 1
  • 7
  • What other four things have you tried? Why were they wrong? Why is this attempt wrong? Have you first worked out the problem logically? Do you understand every step you have to take to solve the problem? Finally, if you did, you should read [this excellent article by Eric Lippert](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) about how to debug small programs such as yours. – Bart van Nierop Dec 04 '14 at 19:02
  • "Turn the frame of an array to left" is not a very precise problem statement. (And your program is C, not C++). – ooga Dec 04 '14 at 19:04
  • Do you need to fill a variable, or just print it out? – Scott Hunter Dec 04 '14 at 19:09
  • I tried to iterate through the main array without making any new arrays. Then I was trying to use booleans, for each corner of the table, to change direction. And then I just tried with these 4 additional arrays on some different ways of iterating. Why were they wrong? Cause I don't know how to change the value of two adjacent cells, without causing one of them to overwrite second one. Why is this one wrong - probably because I have overwritten one of the original elements, then tried to copy it to the next one. I tried to do it logically, but I have no idea how to iterate through the loop. – Setzo Dec 04 '14 at 19:10
  • ooga - Sorry, english isnt my native language, I didn't know how to explain this. Scott Hunter - no I dont need to fill in the value, I just need to print it out. – Setzo Dec 04 '14 at 19:12

2 Answers2

0

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.

#include <stdio.h>

#define N   3

int main( void ) 
{
    int a[N][N] =
    {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
    size_t i;

    for ( i = 0; i < N; i++ )
    {
        size_t j;
        for ( j = 0; j < N; j++ ) printf( "%d ", a[i][j] );
        printf( "\n" );
    }

    printf( "\n" );

    int tmp = a[0][0];

    for ( i = 1; i < N; i++ ) a[0][i-1] = a[0][i];
    for ( i = 1; i < N; i++ ) a[i-1][N-1] = a[i][N-1];
    for ( i = 1; i < N; i++ ) a[N-1][N-i] = a[N-1][N-i-1];
    for ( i = 1; i < N - 1; i++ ) a[N-i][0] = a[N-i-1][0];
    a[N-i][0] = tmp;

    for ( i = 0; i < N; i++ )
    {
        size_t j;
        for ( j = 0; j < N; j++ ) printf( "%d ", a[i][j] );
        printf( "\n" );
    }

    return 0;
}

The output is

1 2 3 
4 5 6 
7 8 9 

2 3 6 
1 5 9 
4 7 8 

If it is what you need then you can modify the program according to your requirements.:)

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
0

This will do the job w/o having to store the whole matrix. (Replace nextNum with however you are reading in numbers):

int main() {
    int m;      // # of matrices to read/shift/print

    // Get # of matrices
    m = nextNum();
    for ( ; m>0; m-- ) {    
        int h, w;   // dimensions of matrix
        int s1;     // value read from first column of one row to be printed in next row
        int i, j;   // index variables
        int r[101]; // space to save a row

        // Get matrix dimensions
        h = nextNum();
        w = nextNum();

        // Read top-left value
        s1 = nextNum();

        // Read rest of first row & print it; since we skipped 1st value, is shifted left
        for ( i=1; i<w; i++ ) {
            int n = nextNum();
            printf( "%d ", n );
        }

        // Process each remaining row of input
        for ( i=1; i<h; i++ ) {
            int last = i==h-1 ? 1 : 0;  // = 1 if last row, 0 o/w

            // Read in the row
            for ( j=0; j<w; j++ )
                r[j] = nextNum();

            // Print end of it to finish off previous row;
            //  print start of next w/ value save from start of previous row
            printf( "%d\n%d ", r[w-1], s1 );

            // Print current row (if last row, include the 1st column)
            for ( j=1-last; j<w-1-last; j++ )
                printf( "%d ", r[j] );
            s1 = r[0];
        }
        // Print the second-to-last item of the last row read, 
        printf( "%d\n", r[w-2] ); 
    }
}
Scott Hunter
  • 44,196
  • 8
  • 51
  • 88