Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 仅使用指针横向打印数组(转置)_C_Arrays_Pointers_Gcc - Fatal编程技术网

C 仅使用指针横向打印数组(转置)

C 仅使用指针横向打印数组(转置),c,arrays,pointers,gcc,C,Arrays,Pointers,Gcc,我正在尝试编写一个名为“transpose”的方法,它只使用指针“侧向”打印2D数组(意味着它的列是它的行,它的行是它的列),甚至不接受int I作为索引。 每个数组的单元格0都包含其大小,在大数组的末尾有一个空地址 例如: int A[] = { 5,-5,14,5,2 }; int B[] = { 3,6,11 }; int C[] = { 4,1,-3,4 }; int D[] = { 6,2,7,1,8,2 }; int E[] = { 2,15 }; int F[] = { 3,4,-

我正在尝试编写一个名为“transpose”的方法,它只使用指针“侧向”打印2D数组(意味着它的列是它的行,它的行是它的列),甚至不接受int I作为索引。 每个数组的单元格0都包含其大小,在大数组的末尾有一个空地址

例如:

int A[] = { 5,-5,14,5,2 };
int B[] = { 3,6,11 };
int C[] = { 4,1,-3,4 };
int D[] = { 6,2,7,1,8,2 };
int E[] = { 2,15 };
int F[] = { 3,4,-2 };
int *All[] = { A,B,C,D,E,F,NULL };
方法需要打印:

-5    6   1   2   15   4
14   11  -3   7       -2
 5        4   1     
 2            8     
              2         
(注意:我们只打印单元格1中的数组,因为我们不打印大小)

这是我目前的进展:

我创建了一个返回最长数组地址的方法:

int * getMax(int **All) 
      {
          int * max = (*All);
          while (*All != NULL)
          {
              if ((*All)[0] > max[0]) 
              {
                  max = (*All);
          }
          *All++;
      }
      return max;
      }
这将返回D的地址,然后我可以通过以下方式访问值6(最大的一行):

这将运行一个外循环5次,以便我们可以打印5行。 然后,我们可以为列的数量创建一个内部循环,并且可以使用关键字NULL作为停止点

void transpose(int **All) 
     {
         int * lines = getMax(All);
         int * m = lines + lines[0];
         lines++;
         while (lines != m) 
         {
             int ** arr = All;
             while (*arr != NULL) 
             {
                *arr++;
         }
                 lines++;
     }

我遇到了一个问题,即其他数组小于5(最大行长度),如何确保它打印一个空格而不超出范围。

要解决您的问题:

问题…是其他数组小于5(最大行长度),我如何确保它打印一个空格,而不是超出范围(?)

最简单的方法是,精确地说,检查(打印时)实际行数是否小于元素数

考虑这一备选方案:

#include <stdio.h>

// I don't care WHICH one is the longest
int find_longest(int **ppi)
//                   ^^^^^ this is NOT a 2D array... 
{
    int longest = 0;
    if ( ppi )
    {
        while (*ppi)
        {
            if ( (*ppi)[0] > longest )
            {
                longest = (*ppi)[0];
            }
            ++ppi;
        }
    }
    return longest;
}

void print_transposed(int **ppi)
{
    int rows_to_be_printed = find_longest(ppi);

    for (int row = 1; row < rows_to_be_printed; ++row)
    {
        for (int i = 0; ppi[i]; ++i)
        {
            // the first element is the number of elements, so...
            if (ppi[i][0] > row)
            {
                printf("%4d", ppi[i][row]);
            }
            else
            {
                printf("    ");   
            }
        }
        puts("");
    }
}

int main()
{
    // The first element is the number of elements, including itself (odd…)
    int A[] = { 5,-5,14,5,2 };
    int B[] = { 3,6,11 };
    int C[] = { 4,1,-3,4 };
    int D[] = { 6,2,7,1,8,2 };
    int E[] = { 2,15 };
    int F[] = { 3,4,-2 };
    // Please note that this is NOT a 2D array, it's an array of pointers
    int *All[] = { A,B,C,D,E,F,NULL};

    print_transposed(All);
}

另一个转置数组的选项

#include <stdio.h>

int main ( void) {
    int A[] = { 5,-5,14,5,2 };
    int B[] = { 3,6,11 };
    int C[] = { 4,1,-3,4 };
    int D[] = { 6,2,7,1,8,2 };
    int E[] = { 2,15 };
    int F[] = { 3,4,-2 };
    int *All[] = { A,B,C,D,E,F,NULL };
    int **line = All;//used to iterate through arrays A, B, C...
    int *col = *line;//points to an array and then an element in array
    int *repeat = *line;//flag to continue do/while
    int *astart = *line;//set to first array first element ex A[0]
    int *acurr = astart;//set to track do/while element ex A[1], A[2]...

    do {
        line = All;//reset line for each iteration
        repeat = NULL;//set so as to stop loop if no more elements
        while ( line && *line) {//loop to sentinel NULL
            if ( *line == astart) {//at the first array in this example A[]
                ++acurr;//advance current pointer to next element ex A[1], A[2]...
            }
            col = *line;//set col to point to each array A[0], B[0], C[0]...
            col += acurr - astart;//advance col to each element in each array A[n]...
            if ( **line > ( acurr - astart)) {//if value at **line ( ex A[0]) greater than n
                printf ( "%-5d", *col);//print value
                if ( **line > ( acurr - astart) + 1) {//indicates are there more elements
                    repeat = col;//set so as to repeat do/while
                }
            }
            else {
                printf ( "     ");//print spaces
            }
            line++;//next array A[0], B[0], C[0]...
        }
        printf ( "\n");
    } while ( repeat);//loop exits it repeat == NULL

    return 0;
}
#包括
内部主(空){
int A[]={5,-5,14,5,2};
int B[]={3,6,11};
int C[]={4,1,-3,4};
int D[]={6,2,7,1,8,2};
int E[]={2,15};
int F[]={3,4,-2};
int*All[]={A,B,C,D,E,F,NULL};
int**line=All;//用于遍历数组A、B、C。。。
int*col=*line;//指向数组,然后指向数组中的元素
int*repeat=*line;//继续执行的标志/while
int*astart=*line;//设置为第一个数组第一个元素exa[0]
int*acurr=astart;//设置为跟踪do/while元素exa[1],A[2]。。。
做{
line=All;//为每次迭代重置行
repeat=NULL;//设置为在没有更多元素时停止循环
while(line&&*line){//循环到sentinel NULL
如果(*line==astart){//在本例中的第一个数组A[]
++acurr;//将当前指针前进到下一个元素,例如A[1],A[2]。。。
}
col=*line;//将col设置为指向每个数组A[0]、B[0]、C[0]。。。
col+=acurr-astart;//将col前进到每个数组中的每个元素A[n]。。。
if(**line>(acurr-astart)){//if值在**行(exa[0])大于n
printf(“%-5d”,*列);//打印值
如果(**line>(acurr-astart)+1){//表示还有更多的元素
repeat=col;//设置为重复do/while
}
}
否则{
printf(“”;//打印空间
}
line++;//下一个数组A[0],B[0],C[0]。。。
}
printf(“\n”);
}while(repeat);//循环退出它repeat==NULL
返回0;
}
#包括
int main()
{                                                                               
int A[]={5,-5,14,5,2};
int B[]={3,6,11};
int C[]={4,1,-3,4};
int D[]={6,2,7,1,8,2};
int E[]={2,15};
int F[]={3,4,-2};
int*All[]={A,B,C,D,E,F,NULL};
int指数=1;
int rem=6;//将其更改为返回所有[]的长度的函数
而(rem>0)
{                                                                             
for(int i=0;i<6;i++)//这里也用函数更改6
{                                                                           
int*arr=所有[i];
如果(索引
我编写了一个完全不同的工作解决方案。如果你愿意,我可以与你分享。是的,请@SoumenDones有任何想法吗?谢谢你的回答,但问题是只使用指针,这意味着不允许你声明整数值。请考虑使用arr[I],I++使用*(*arr0),(*arr)++@米凯利洛斯是可以接受的还是一种欺骗?是的,这正是我想要的!非常感谢。 -5 6 1 2 15 4 14 11 -3 7 -2 5 4 1 2 8 2
int *find_longest(int **ppi)
{
    int *longest = NULL;
    if ( ppi )
    {
        longest = *ppi;
        while (*ppi)
        {
            if ( **ppi > *longest )
            {
                longest = *ppi;
            }
            ++ppi;
        }
    }
    return longest;
}

void print_transposed(int **ppi)
{
    int *longest = find_longest(ppi);
    if ( longest )
    {
        int *end_of_longest = longest + *longest;
        for (int *row = longest + 1; row != end_of_longest; ++row)
        {
            for (int **ppj = ppi; *ppj; ++ppj)
            {
                if ( (*ppj)[0] > row - longest)
                {
                    printf("%4d", (*ppj)[row - longest]);
                }
                else
                {
                    printf("    ");   
                }
            }
            puts("");
        }
    }
}
#include <stdio.h>

int main ( void) {
    int A[] = { 5,-5,14,5,2 };
    int B[] = { 3,6,11 };
    int C[] = { 4,1,-3,4 };
    int D[] = { 6,2,7,1,8,2 };
    int E[] = { 2,15 };
    int F[] = { 3,4,-2 };
    int *All[] = { A,B,C,D,E,F,NULL };
    int **line = All;//used to iterate through arrays A, B, C...
    int *col = *line;//points to an array and then an element in array
    int *repeat = *line;//flag to continue do/while
    int *astart = *line;//set to first array first element ex A[0]
    int *acurr = astart;//set to track do/while element ex A[1], A[2]...

    do {
        line = All;//reset line for each iteration
        repeat = NULL;//set so as to stop loop if no more elements
        while ( line && *line) {//loop to sentinel NULL
            if ( *line == astart) {//at the first array in this example A[]
                ++acurr;//advance current pointer to next element ex A[1], A[2]...
            }
            col = *line;//set col to point to each array A[0], B[0], C[0]...
            col += acurr - astart;//advance col to each element in each array A[n]...
            if ( **line > ( acurr - astart)) {//if value at **line ( ex A[0]) greater than n
                printf ( "%-5d", *col);//print value
                if ( **line > ( acurr - astart) + 1) {//indicates are there more elements
                    repeat = col;//set so as to repeat do/while
                }
            }
            else {
                printf ( "     ");//print spaces
            }
            line++;//next array A[0], B[0], C[0]...
        }
        printf ( "\n");
    } while ( repeat);//loop exits it repeat == NULL

    return 0;
}
#include <stdio.h>                                                              

int main()                                                                      
{                                                                               
  int A[] = { 5,-5,14,5,2 };                                                    
  int B[] = { 3,6,11 };                                                         
  int C[] = { 4,1,-3,4 };                                                       
  int D[] = { 6,2,7,1,8,2 };                                                    
  int E[] = { 2,15 };                                                           
  int F[] = { 3,4,-2 };                                                         
  int *All[] = { A,B,C,D,E,F,NULL };                                            

  int index = 1;                                                                
  int rem   = 6;     // change this to a function which returns length of All[]                                                   

  while (rem > 0)                                                               
  {                                                                             
    for (int i = 0; i < 6; i++)      // Here also change 6 with a function                                           
    {                                                                           
      int *arr = All[i];                                                        

      if (index < arr[0])                                                       
        printf("%-4d", arr[index]);                                             
      else                                                                      
      {                                                                         
        rem--;                                                                  
        printf("    ");                                                         
      }                                                                         
    }                                                                           

    index++;                                                                    
    printf("\n");                                                               
  }                                                                             

  return 0;                                                                     
}