开发者

Entering a 2d array in a frame in C

开发者 https://www.devze.com 2023-03-01 13:18 出处:网络
I am trying to have the user input a 2d array and then it would print out that array in something like a frame, i wrote the code and it works but when it comes to printing the array the program just p

I am trying to have the user input a 2d array and then it would print out that array in something like a frame, i wrote the code and it works but when it comes to printing the array the program just prints the first row of what the user inputted over and over like this:

Enter size
3
Enter Array
1 2 3
4 5 6
7 8 9

Your Array:
     a b c    
   # # # # # 
1  # 1 2 3 # 
2  # 1 2 3 # 
3  # 1 2 3 # 
   # # # # # 

Here is my code: I feel its a small error but i just can't find it.

#include <stdio.h>

int main(){
    int i,j;
    int y,x;
    int n,a,num,readNum,z;
    char ch;

    ch = 'a';
    num = 1;

    printf("Enter size\n");
    readNum = scanf("%d",&z);

    int Board[z][z];
    n = z+2;
    a = n+1;

    printf("Enter Array\n");
    for(x=0;x<z;x++){
        for(y=0;y<z;y++){
           readNum = scanf("%d",&Board[x][y]);
        }
    }

    printf("\nYour Array:\n");
    x=0;
    for(i=0;i<a;i++){
        y=0;
        for(j=0;j<a;j++){
            if(((i==0)&&(j==1))||
               ((i==1)&&(j==0))||
               ((i==0)&&(j==n))||
               ((j==0)&&(i==n))){
                printf("开发者_如何学运维   ");
            }
            if ((i==0)&&(j==0)){
                printf("  ");
            }
            if ((i==0 && (j>=2 && j<n))){
                printf("%c ",ch);
                ch++;
            }
            if ((j==0) &&(i>=2 && i<n)){
                printf("%d  ",num);
                num++;
            }
            if(((i==1) && (j>=1 && j<=n))||
               ((i==n) && (j>=1 && j<=n))||
               ((j==1) && (i>1  && i<n ))||
               ((j==n) && (i>1  && i<n ))){
                printf("# ");
            }
            if((i>1 && i<n) && (j>1 && j<n)){ 
                printf("%d ",Board[x][y]);
                y++;
            }
        }
        printf("\n");
    }
    return 0;
}


After

     printf("\nYour Array:\n");
     x=0; 

you never change x.


You need to increment x when you finish a line.

The fix is small:

            if((i>1 && i<n) && (j>1 && j<n)){ 
                printf("%d ",Board[x][y]);
                y++;
                if ((y%z) == 0)
                    x++;
            }

When you had z ys, it's time to go down one line (increment x).

The following code, which is your code with my modification prints the correct lines (I tried up to 5):

#include <stdio.h>

int main(){
    int i,j;
    int y,x;
    int n,a,num,readNum,z;
    char ch;

    ch = 'a';
    num = 1;

    printf("Enter size\n");
    readNum = scanf("%d",&z);

    int Board[z][z];
    n = z+2;
    a = n+1;

    printf("Enter Array\n");
    for(x=0;x<z;x++){
        for(y=0;y<z;y++){
           readNum = scanf("%d",&Board[x][y]);
        }
    }

    printf("\nYour Array:\n");
    x=0;
    for(i=0;i<a;i++){
        y=0;
        for(j=0;j<a;j++){
            if(((i==0)&&(j==1))||
               ((i==1)&&(j==0))||
               ((i==0)&&(j==n))||
               ((j==0)&&(i==n))){
                printf("   ");
            }
            if ((i==0)&&(j==0)){
                printf("  ");
            }
            if ((i==0 && (j>=2 && j<n))){
                printf("%c ",ch);
                ch++;
            }
            if ((j==0) &&(i>=2 && i<n)){
                printf("%d  ",num);
                num++;
            }
            if(((i==1) && (j>=1 && j<=n))||
               ((i==n) && (j>=1 && j<=n))||
               ((j==1) && (i>1  && i<n ))||
               ((j==n) && (i>1  && i<n ))){
                printf("# ");
            }
            if((i>1 && i<n) && (j>1 && j<n)){ 
                printf("%d ",Board[x][y]);
                y++;
                if ((y%z)==0)
                   x++;
            }
        }
        printf("\n");
    }
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消