Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i have problem with spiral shape matrix elements printing

+1 vote
asked Sep 22 by KRISHNA T (130 points)
#include <stdio.h>

#define m 9
void display2();
void display();
void create();
int main()
{
   
   create();
   display();
   
   return 0;
}
void create(){
    int arr[m][m]={00,01,02,03,04,05,06,07,8,10,11,12,13,14,15,16,17,18,20,21,22,23,24,25,26,27,28,30,31,32,33,34,35,36,37,38,40,41,42,43,44,45,46,47,48,50,51,52,53,54,55,56,57,58,60,61,62,63,64,65,66,67,68,70,71,72,73,74,75,76,77,78,80,81,82,83,84,85,86,87,88};
    
}
void display(){
   // int i=0,j=0,highest=m,lowest=0,arr[m][m];
    //for (i=lowest;i<highest;i++){
      //  for(j=0;j<highest;j++){
        //    printf("\n%d",arr[i][j]);
        //}
    //}
  display2();
    printf("\ndisplay elements succed");
    
}
void display2(){
   int i=0,j=0,highest=m,lowest=0;
    int arr[m][m];
    for(;highest==lowest;)
    {
    for(j=0;j<=highest;)
    {
       printf("\n%d",arr[i][j]);
        j++;
    }
     for(i=0;i<=highest;){
       printf("\n%d",arr[i][j]);
        i++;
    }
    
    
    
    
     for(j=highest;j<=lowest;){
        printf("\n%d",arr[i][j]);
        j--;
    }
     for(i=highest;i<lowest;){
        printf("\n%d",arr[i][j]);
        i--;
    }
    highest--;
    lowest++;
    
    }
    

    
    
}

1 Answer

0 votes
answered 6 days ago by Peter Minarik (58,220 points)

I'm not exactly sure what you're trying to achieve (what would you like to print, what range of the matrix, what is the highest and the lowest as a range should be minRow, maxRow, minColumn, maxColumn).

You're also declaring your arrays as local variables in your various functions, but they lose their value as you leave the function. So either declare the array globally or pass them (as pointers) to the respective functions.

#include <stdio.h>

#define m 9

int arr[m][m] = { 00, 01, 02, 03, 04, 05, 06, 07, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 86, 87, 88 };

void display(int minRow, int maxRow, int minCol, int maxCol)
{
    for (int row = minRow; row < maxRow; row++)
    {
        for (int col = minCol; col < maxCol; col++)
            printf("%3d", arr[row][col]); // Display numbers on 3 width

        printf("\n"); // New line after every row
    }
}

int main()
{
   display(0, m, 0, m);
   return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...