简体   繁体   English

打印二维数组中的某些元素

[英]Printing out certain elements in a 2D array

I'm kinda new to C programming, and I have a certain school assignment I need help solving. 我是C编程的新手,我有一定的学校作业需要解决。 Assignment 分配

So, I'm supposed to print out these yellow-marked elements in an array of any given size. 因此,我应该将这些带有黄色标记的元素打印成任意给定大小的数组。

So, here's my code I've come up so far: 因此,这是到目前为止我提出的代码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{

int i,j,n;

printf("Matrix dimension: ");
scanf("%d",&n);

int matrix[n][n];

srand((unsigned)time(NULL));

for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
    {
        matrix[i][j]=100+(float)rand()/RAND_MAX*900; //filling out matrix with random integers
    }
}
for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
    {
        printf("%d\t",matrix[i][j]);    //printing out matrix itself
    }
    printf("\n");
}
printf("\nLeft:\n");    //elements below main diagonal, above secondary diagonal
for(i=1;i<n-1;i++)
{
    printf("\n");
    for(j=0;j<(n-1)/2;j++)
    {
       if((j<i) && (j+i!=n-1))printf("%d\t",matrix[i][j]);
        else printf("-\t");
    }

}
printf("\n\nRight:\n");     //elements above main diagonal, below secondary diagonal
for(i=1;i<n-1;i++)
{
    printf("\n");
    for(j=n-1;j>(n/2);j--)
    {
       if((j>i) &&(i+j!=n-1) )printf("%d\t",matrix[i][j]);
       else printf("-\t");
    }
}

printf("\n\nUp:\n");    //elements above main and secondary diagonal
for(i=0;i<(n-1)/2;i++)
{
    printf("\n");
    for(j=1;j<n-1;j++)
    {
       if((j>i) && (j+i!=n-1 && j+i!=n))printf("%d\t",matrix[i][j]);
       else printf("-\t");
    }
}
printf("\n\nDown:\n");     //elements below main and secondary diagonal
for(i=n-1;i>(n/2);i--)
{
    printf("\n");
    for(j=1;j<n-1;j++)
    {
       if((i>j) &&(i+j!=n-1) )printf("%d\t",matrix[i][j]);
       else printf("-\t");
    }
}
return 0;
}

This code works, but it doesn't work on matrixes whose dimension is equal to 7 or greater. 该代码有效,但不适用于尺寸等于或大于7的矩阵。 There's always one element extra in those situations. 在这些情况下,总会有一个额外的因素。 I've realized I can put some extra statements which would solve the problem, but only temporarily. 我意识到我可以添加一些额外的语句来解决问题,但这只是暂时的。 Example: 例:

if((j<i) && (j+i!=n-1 && j+i!=n))

Then it would work on matrixes up to 8x8. 然后它将在高达8x8的矩阵上工作。

Then I added another statement. 然后,我添加了另一个声明。

if((j<i) && (j+i!=n-1 && j+i!=n && j+i!=n+1))

And then it would work on matrixes up to 10x10. 然后,它将在高达10x10的矩阵上工作。

So I've noticed that I need to increment j+i statement by 1 every 2 "dimensions". 因此,我注意到我需要将j + i语句每2个“维度”增加1。

However, I don't really know how to do that. 但是,我真的不知道该怎么做。 I tried adding something like 我尝试添加类似

j+i!=n-1+c

where I would set c to zero, and increment in the loop, but it doesnt't work. 我将c设置为零,并在循环中递增,但是它不起作用。 Also, I would like to print elements as they are in the matrix. 另外,我想打印元素,因为它们在矩阵中。 So, if I'm printing out right "triangle", I would like if the elements were arranged as they are in the matrix, and not like this: Example of incorrect output 因此,如果我要打印正确的“三角形”,我希望这些元素是否按它们在矩阵中的排列方式而不是这样: 错误输出示例

I'd just like to like mirror these numbers, so they're in their spot. 我只想反映这些数字,所以它们就在他们的位置。 So, yeah, that's my problem. 是的,那是我的问题。 I'm sure that there are easier solutions to this, but I'm a C noob, and probably making stuff more complicated than it should be. 我敢肯定有更简单的解决方案,但是我是C菜鸟,可能会使事情变得比应有的复杂。 Anyways, any help is appreciated! 无论如何,我们将不胜感激!

How to 如何

When you need to print your array you need two conditions in order to avoid those that are not yellow. 当需要打印阵列时,需要两个条件以避免出现非黄色的条件。 The first condition is obvious. 第一个条件是显而易见的。

if(i==j){
 // do not print
}

Second condition is a little less obvious but never the less. 第二种情况不太明显,但永远不会少。

else if(i+j == maxCellSize-1){ //in your case maxCellSize=7
 //do not print
}

Finally after checking those two conditions(preferable combined into one if-statement), you can then print the cell of the array. 最后,在检查了这两个条件(最好合并为一个if语句)之后,即可打印该数组的单元格。

else{
    printf("%d", matrix[i][j]);
}

The entire code 整个代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{

    int i,j,n;

    printf("Matrix dimension: ");
    scanf("%d",&n);

    int matrix[n][n];

    srand((unsigned)time(NULL));

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            matrix[i][j]=100+(float)rand()/RAND_MAX*900;  //filling out matrix with random integers
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(!(i==j || i+j==n-1)){
             printf("%d\t",matrix[i][j]);    //printing out matrix itself
            }
            else{ printf("X\t");}
        }
        printf("\n");
    }
    printf("\n\n");

    //left
    printf("Left triangle: ");
    for(i=0; i <n; i++){
        for(j=0; j<n/2; j++){
            if(i+j<n && i>j && !(i+j==n-1))
            {
                printf("%d\t",matrix[i][j]); 
            }
        }
    }
    printf("\n");
    //right
    printf("Right triangle: ");
    for(i=1; i <n; i++){
        for(j=n/2; j<n; j++){
            if(!(i==j || i+j==n-1)){
                if(i+j>=n && i<j)
                {
                    printf("%d\t",matrix[i][j]); 
                }

            }
        }
    }

    //top
    printf("\n");
    printf("Top triangle: ");
    for(i=0; i < n/2; i++){
        for(j=1; j<n; j++){
            if(!(i==j || i+j==n-1)){
                if(i+j<n && i<j)
                {
                    printf("%d\t",matrix[i][j]); 
                }

            }
        }
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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