简体   繁体   中英

input 1D array into 2D array in increasing order (C)

I need to build a program, that requesting from user to insert a sentence like "Hello World" or whatever

and than take this sentence and input it inside 1D array

and from there to input it in 2D array that will present his sentence in this order:

Hello!! :)
)Hello!! :
:)Hello!! 
 :)Hello!!
! :)Hello!
!! :)Hello
o!! :)Hell
lo!! :)Hel
llo!! :)He
ello!! :)H

I did most of the job done, but i can't make it exactly like this i mean its present me the sentence decreasing and not increasing

my code:

#include <stdio.h>
#define P 10
int main(int argc, const char * argv[])
{
    char array1[P][P];
    char array2[P];
    int i, j;

    for (i = 0; i<P; i++) //fill the array with the chars.
    {
        scanf("%c", &array2[i]);
    }

    for (i=0 ; i<P ; i++)
    {
        for (j=0 ; j<P ; j++)
        {
            array1[i][j] = array2[j+i];
        }
    }

    for (i=0 ; i<P ; i++)
    {
        for (j=0 ; j<P ; j++)
        {
            printf("%c", array1[i][j]);
        }
        putchar('\n');
    }

}

where is my mistake? and how i can fix it? Thanks.

array1[i][j] = array2[j+i]; should be array1[i][j] = array2[(j+Pi)%P];

In your code during entering in array1 for first run of your outer loop is correct but for second time it is inserting value at array1[1][0]=array2[1]; means second element of the array2 so use this

array1[i][j] = array2[(j+P-i)%P];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM