Home c++ C++ Enter the matrix using diagonals

C++ Enter the matrix using diagonals

Author

Date

Category

Enter a square matrix using diagonals. That is, first the main diagonal is introduced, then the diagonal is one longer below and the last element of this string (which does not enter the matrix range must be transferred to the upper right angle), etc. Please in solving the task not to use the Faction vector.

Example (numbers are simply presented for visualing the work of diagonals, the matrix can be of any size):

1 *** 1 ** 2 1 * 32
* 1 ** 21 ** 21 * 3
** 1 * * 21 * 321 *
*** 1 ** 21 * 321

My code but without transferring items not included in the matrix range:

# include & lt; iostream & gt;
INT MAIN ()
{
int n;
CIN & GT; & gt; n;
INT MAT [N] [N];
for (int i = 0; i & lt; (n); i ++)
 for (int j = 0; j & lt; (n); j ++)
{
  if (j + i & gt; j + j) {i = 0;} // this part does not work
  CIN & GT; & GT; MAT [j + i] [j];
}
}

Answer 1

It is possible without the functions of zeroing the array and its output.

# include & lt; iostream & gt;
Using Namespace STD;
// Control of the exit index for the size of the array
INT ControlIndex (Int MatrixIndex, Uint Sizematrix)
{
  if (MatrixIndex & GT; = SizeMatrix)
  {
    Return 0;
  }
  ELSE.
  {
    // that is, do nothing with the index
    RETURN MATRIXINDEX;
  }
}
// show the matrix
Void ShowMatrix (Int ** Matrix, Int SizeMatrix)
{
  for (int i = 0; i & lt; sizematrix; i ++)
  {
    for (int j = 0; j & lt; sizematrix; j ++)
    {
      COUT & LT; & LT; Matrix [i] [J];
    }
    COUT & LT; & LT; Endl;
  }
}
// Fill the matrix by zeros
Void Fillmatrix (Int ** Matrix, Int SizeMatrix)
{
  for (int i = 0; i & lt; sizematrix; i ++)
  {
    for (int j = 0; j & lt; sizematrix; j ++)
    {
      Matrix [i] [j] = 0;
    }
  }
}
INT MAIN ()
{
  // variable corrective row transition, in case of an array size
  int rowcorrection = 0;
  UINT SIZE;
  COUT & LT; & LT; "Size:";
  CIN & GT; & GT; Size;
  // Select the memory of the array of two-dimensional
  int ** matrix = new int * [size];
  // Select the memory to each one-dimensional array of two-dimensional array
  for (int i = 0; i & lt; size; i ++)
  {
    Matrix [i] = New int [Size];
  }
  // immediately fill the matrix with zeros and show its initial state
  Fillmatrix (Matrix, Size);
  ShowMatrix (Matrix, Size);
  for (int i = 0; i & lt; size; i ++)
  {
    For (int j = 0; j & lt; Size; j ++)
    {
      // Input of the number diagonally
      // If the index & gt; = Size, then the string becomes 0 and each
      // Iteration increases by 1
      // and the columns independently continue to increase
      CIN & GT; & GT; Matrix [ControlIndex (J + I, Size) + RowCorrection] [J];
      if (J + I & GT; = Size)
      {
        // Due to this, our lines index continues to increase
        // Even after it is zeroing after the size of the array
        RowCorrection ++;
      }
      // remove the matrix every time after entering the number
      ShowMatrix (Matrix, Size);
      COUT & LT; & LT; Endl;
    }
    // After entering the diagonal, reset the correction
    rowcorrection = 0;
  }
   ShowMatrix (Matrix, Size);
  getchar ();
  Return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions