1

I wrote this code :

#include <stdio.h>
void transpose(int *Al[]){
int x=0;
int z=0;
int k=1;
while (*Al[z] != "\0") {
    int c=*Al[z];
    if (c>x)
        x=c;
   z++;
}
printf("%d ",x);
for(int o=0;o<6;o++){
   for(int i=0 ;i<x;i++ ) {
       int *p = Al[i];
       int l=*p;
       if(k<l)
       printf("%d ",*(p+k));
       else
           printf("   ");
   }
   k++;
   printf("\n");
   }

   }
   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};
   transpose(All);
   }

The function gets an array that points to different array I need to print the arrays using pointers the output should be :

-5 6   1 2 15 4
14 11 -3 7    -2
5      4 1     
2        8      
         2

But this code doesn't print anything. Also the arrays that it points at the first value is the size of the array. I tried this :

void transpose(int *Al[]){
int x=0;
int z=0;
int k=1;
for(int o=0;o<5;o++){
    for(int i=0 ;i<6;i++ ) {
        int *p = Al[i];
        int l=*p;
        if(k<l)
            printf("%d ",*(p+k));
        else
            printf("   ");
    }
    k++;
    printf("\n");
    }

 }

It worked only I need to replace the five and six in the loop Five is the biggest size of all he arrays -1 so I will know how many lines to print and six to how many arrays in All so I can know how many colums I should print. Is there a solution for this?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
ella
  • 55
  • 6
  • How is your code suppose to know how long `A`, `B`, etc. are? It can't. They're not null-terminated, and you aren't taking their size. Try to come up with a design for how you want to handle it. – Tom Karzes Jun 15 '21 at 11:03
  • Also, the first value from each of the `A`, `B`, etc. arrays is missing from your sample output. Is that intentional? – Tom Karzes Jun 15 '21 at 11:05
  • 1
    What do you think `while (*Al[z] != "\0") {...` does? You are comparing an `int` value with the address of a string literal. – Adrian Mole Jun 15 '21 at 11:06
  • `while (*Al[z] != "\0")` Did you compiler warn you about this line? If not, you need to throw it away and get a better compiler. If yes, you need to ask about the warning, what it means and how to fix it before asking about anything else. Better still, enable a compiler option to treat all warnings as errors, see [here](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) – n. m. could be an AI Jun 15 '21 at 11:06
  • Also please format your code and fix the indention. This is all very hard to read. – Lundin Jun 15 '21 at 11:10
  • It can know the length of the by their first value like A{5, -5, 14, 5, 2} 5 represents the length of the A array – ella Jun 15 '21 at 11:16
  • You should [edit] the question to say that the first element of each array is its total length. – TripeHound Jun 15 '21 at 11:17
  • 2
    Just changing `while (*Al[z] != "\0") {` to `while (Al[z] != NULL) {` *appears* to fix the code. There's some funny spacing in the output but the numbers are what you say you should get. – Adrian Mole Jun 15 '21 at 11:24

1 Answers1

0

The condition in the while loop

while (*Al[z] != "\0") {

does not make a sense. The expression *Al[z] has the type int while the string literal "\0" has the type char *.

Also it is unclear why there is present the magic number 6 in this loop

for(int o=0;o<6;o++){

There is no need to calculate explicitly the number of columns because you have a sentinel value equal to NULL.

I can suggest for example the following solution

#include <stdio.h>

void transpose( int * Al[] )
{
    int rows = 0;
    
    for ( int **p = Al; *p != NULL; ++p )
    {
        if ( rows < **p ) rows = **p;
    }
    
    if ( rows ) --rows;
    
    for ( int i = 0; i < rows; i++ )
    {
        for ( int **p = Al; *p != NULL; ++p )
        {
            if ( i + 1 < **p ) printf( "%2d ", *( *p + i + 1 ) );
            else printf( "   " );
        }
        
        putchar( '\n' );
    }
    
}

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 };
   
    transpose( All );
   
    return 0;
}

The program output is

-5  6  1  2 15  4 
14 11 -3  7    -2 
 5     4  1       
 2        8       
          2 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335