• Transpose of a matrix bash script

    Miranda65M Member

    Can anyone explain this code ??

    echo "Enter Number of rows"
    read  r
    echo "Enter Number of columns"
    read  c
    i=0
    echo "Enter elements"
    until [ $i -eq expr $r \* $c ]
    do
           read a[$i]
           i=expr $i + 1
    done
    i=0 ;  k=0
    echo "Transpose of a Matrix"
    until [ $i 'eq $c ]
    do
           j=0;
           until [ $j 'eq $r ]
           do
                  n= expr $j \* $c
                  m= `expr $n + $i
                  b[$k]=${a[$m]}
                  echo "${b[$k]} \t"
                  k=expr $k + 1
                  j=expr $j + 1
           done
           i=expr $i + 1
           echo "\n"
    done
    
  • ShikhaTan Member

    IDK if this helps but it’s pretty simple code

    r is the number of row  you entered
    c is the number of columns you enterd
    
    until [ $i -eq expr $r \* $c ]
    
    this means while (i != r * c) (bcz its until not while..)
    read a[$i]  // here you simply read and store into a[i].
    i=expr $i + 1  // i++.
    done // exit the loop
    
    until [ $i `eq $c ]
    do
           j=0;
           until [ $j `eq $r ]  //  j != r
           do
                  n= expr $j \* $c  // n = j * c
                  m= `expr $n + $i   // m=n+i
                  b[$k]=${a[$m]}    // b[k] = a[m]
                  echo "${b[$k]} \t"    // cout << b[k];
                  k=expr $k + 1    // k++
                  j=expr $j + 1    // j++
           done
           i=expr $i + 1     //i++
           echo "\n"
    done
    
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish