debug spiral char matrix printing

FratBro23
Category:
Computer Science
Price: $40 USD

Question description

I already wrote this program and it works for const size of my matrix but when it comes to get input from use for MxN matrix of char and printout the output as clockwise spiral form when i want to get input it doesnt do that for even indexes and also it doesnt read the 00. can you please check this pogram and see what is wrong??? Also i need to make an equivalent c make file for this to run in linux later



here is what i wrote :



#include <stdio.h>

void printSpiralMatrix(int m, int n, char a[m][n]) {

  enum {
  up, down, left, right
  } direction = right;
  int up_limit = 0,
  down_limit = m - 1,
  left_limit = 0,
  right_limit = n - 1,
  downcount = m * n,
  row = 0,
  col = 0;

  while (printf("%c",a[row][col]) && --downcount) {
  if (direction == right) {
  if (++col > right_limit) {
  --col;
  direction = down;
  ++up_limit;
  ++row;

  }
  } else if (direction == down) {
  if (++row > down_limit) {
  --row;
  direction = left;
  --right_limit;
  --col;

  }
  } else if (direction == left) {
  if (--col < left_limit) {
  ++col;
  direction = up;
  --down_limit;
  --row;

  }
  } else /* direction == up */
  if (--row < up_limit) {
  ++row;
  direction = right;
  ++left_limit;
  ++col;

  }

  }


}

int main() {
   
  int i, j, rows, cols;

   
   
  printf("enter number of rows:\n");
  scanf("%d", &rows);
  printf("enter number of columns:\n");
  scanf("%d", &cols);
  char spiralMatrix[rows][cols];
  printf("Enter the elements of the spiral matrix:\n");


   

  for (i = 0; i < rows; i++) {
  for (j =0; j < cols; j++) {
   
  printf("  spiralMatrix[%d][%d] :", i,j);
  scanf("%c", &spiralMatrix[i][j]);
   
  }
  printf("\n");
   
  }
//  printf("your matrix is :\n");
//
//  for (i = 0; i < rows; i++) {
//
//  for (j = 0; j < cols; j++) {
//
//  printf("spiralMatrix[%d][%d]= %c",i,j, spiralMatrix[i][j]);
//
//  }
//
//
//  printf("\n");
//
//
//  }


  printf("your spiral clockwise rotation output is:\n");
  printSpiralMatrix(rows, cols, spiralMatrix);
 
   
   
  return 0;
}

Tutor Answer

(Top Tutor) Daniel C.
(997)
School: UIUC
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1822 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors