GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Programming
Next Question 
 Programming  |  Question 1 of 7    Print  
How to write Spiral matrix program?

  
Total Answers and Comments: 2 Last Update: June 17, 2008     Asked by: Swati 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
April 27, 2007 01:25:33   #1  
Abhijit Singh        

RE: How to write Spiral matrix program?
/*************  Creates a spiral matrix square or rectangular .************/

/* It first finds all the indices where items are to be placed. After one iteration of
  the 4 loops, matrix size is reduced. "Start" variable points to the lcation from
  where the filling has to start
*/

typedef struct
{
 int m,n;
}Index;

int Create_Spiral_Matrix ( int Linear[],int Size )
{
 int **Spiral;
 int m = 0 ,n = 0, p = m , q = n, i;
 int c = 0, Start = 0;
 Index *I;

    while ( m*n != Size )
 {
  printf("nFeed in the dimensions of the Spiral Matrix to be formed := ");
  scanf("%d %d",&m,&n);
 }
    p = m;   q = n;
 
 Spiral = (int **) malloc( m * sizeof(int *));   //Check Mem Allocation To Prevent Crash.
 for ( i = 0; i < m; i++)
    Spiral[i] = (int *) malloc( m * sizeof(int));     //Check Mem Allocation To Prevent Crash.

 I = (Index *) malloc(sizeof(Index) * m * n);   //Check Mem Allocation To Prevent Crash.

 while ( c < Size  )
 {
  for ( i = Start; i < n; i++,c++)
  {
   I[c].m = Start;
   I[c].n = i;
  }
  for ( i = Start+1; i < m; i++,c++)
  {
   I[c].m  = i;
   I[c].n = n-1;
  }
  for ( i = n-2; i >= Start; i--,c++)
  {
   I[c].m = m-1;
   I[c].n = i;
  }
  for ( i = m-2; i >= Start+1; i--,c++)
  {
   I[c].m = i;
   I[c].n = Start;
  }

  m --;  n --;     
  Start++;

        if ( m-1 == Start )
  {
   if ( m > n )
    for ( i = Start; i < n; i++,c++)
    {
     I[c].m = Start; 
     I[c].n = n - Start;
    }
   if ( n < m )
    for ( i = Start; i < m; i++,c++)
    {
     I[c].m = Start; 
     I[c].n = n - Start;
    }
  }
 }

 i = 0;
 for (  m = 0; m < p ; m++,printf("n"))
  for (  n = 0; n < q ; n++)
      Spiral [I[i].m] [I[i].n] = Linear[i++];

 for (  m = 0; m < p ; m++,printf("n"))
  for (  n = 0; n < q ; n++)
   printf("%dt",Spiral[m][n]);
return 0;
}

int main()
{
 int Linear1[] = { 1, 5, 8, 9, 5, 7, 4, 8, 0, 2, 3, 6,
               4, 1, 7, 2, 5, 9, 3, 2, 5, 6, 1, 2,
      5, 7, 3, 8, 5, 3, 6, 1, 2, 4, 9, };
   
 int Linear2[] = { 1, 5, 8, 5, 7, 8, 0, 6, 2,
               4, 1, 7, 2, 9, 3, 1, 2, 9,
      5, 7, 3, 8, 5,  2, 4,  };


 Create_Spiral_Matrix(Linear1, sizeof(Linear1)/4);
 Create_Spiral_Matrix(Linear2, sizeof(Linear2)/4);
 printf("n");
 return 0;
}


 
Is this answer useful? Yes | No
April 27, 2007 05:29:51   #2  
Abhijit Singh Member Since: April 2007   Contribution: 4    

RE: How to write Spiral matrix program?

/*~~~~~~~~~ Creates square and  rectangular matrices ~~~~~~~~*/
/* Input required is dim of the spiral matrix. Just feed in two factors of the size of array. i.e If Items are 35 then m = 5 n = 7 or m = 7 n = 5 or m = 35 n = 1 or m = 1 n = 35.  If Items are 36 then m = 1/3/4/6/9/12/36  n = 36/12/9/6/4/3/1
*/

typedef struct
{
 int m,n;
}Index;

int Create_Spiral_Matrix ( int Linear[],int Size )
{
 int **Spiral;
 int m = 0 ,n = 0, p = m , q = n, i;
 int c = 0, Start = 0;
 Index *I;

    while ( m*n != Size )
 {
  printf("nFeed in the dimensions of the Spiral Matrix to be formed := ");
  scanf("%d %d",&m,&n);
 }
    p = m;   q = n;
 
 Spiral = (int **) malloc( m * sizeof(int *));   //Check Mem Allocation To Prevent Crash.
 for ( i = 0; i < m; i++)
    Spiral[i] = (int *) malloc( n * sizeof(int));     //Check Mem Allocation To Prevent Crash.

 I = (Index *) malloc(sizeof(Index) * m * n);   //Check Mem Allocation To Prevent Crash.

 while ( c < Size  )
 {
  for ( i = Start; i < n; i++,c++)
  {
   I[c].m = Start;
   I[c].n = i;
  }
  for ( i = Start+1; i < m; i++,c++)
  {
   I[c].m  = i;
   I[c].n = n-1;
  }
  for ( i = n-2; i >= Start; i--,c++)
  {
   I[c].m = m-1;
   I[c].n = i;
  }
  for ( i = m-2; i >= Start+1; i--,c++)
  {
   I[c].m = i;
   I[c].n = Start;
  }

  m --;  n --;     
  Start++;

        if ( m-1 == Start )
  {
   if ( m > n )
    for ( i = Start; i < n; i++,c++)
    {
     I[c].m = Start; 
     I[c].n = n - Start;
    }
   if ( n < m )
    for ( i = Start; i < m; i++,c++)
    {
     I[c].m = Start; 
     I[c].n = n - Start;
    }
  }
 }

 i = 0;
 for (  m = 0; m < p ; m++,printf("n"))
  for (  n = 0; n < q ; n++)
      Spiral [I[i].m] [I[i].n] = Linear[i++];

 for (  m = 0; m < p ; m++,printf("n"))
  for (  n = 0; n < q ; n++)
   printf("%dt",Spiral[m][n]);
return 0;
}

int main()
{
 int Linear1[] = { 1, 5, 8, 9, 5, 7, 4, 8, 0, 2, 3, 6,
                4, 1, 7, 2, 5, 9, 3, 2, 5, 6, 1, 2,
       5, 7, 3, 8, 5, 3, 6, 1, 2, 4, 9 };
   
 int Linear2[] = { 1, 5, 8, 5, 7, 8, 0, 6, 2,
                4, 1, 7, 2, 9, 3, 1, 2, 9,
       5, 7, 3, 8, 5,  2, 4  };


 Create_Spiral_Matrix(Linear1, sizeof(Linear1)/4);
 Create_Spiral_Matrix(Linear2, sizeof(Linear2)/4);
 printf("n");
 return 0;
}



 
Is this answer useful? Yes | No

 Related Questions

Latest Answer : /*~~~~~~~~~ Creates square and  rectangular matrices ~~~~~~~~*//* Input required is dim of the spiral matrix. Just feed in two factors of the size of array. i.e If Items are 35 then m = 5 n = 7 or m = 7 n = 5 or m = 35 n = 1 or m = 1 n = 35.  ...
Read Answers (2) | Asked by : Swati

Write a program to read a four digit integer and print the sum of its digits.Write a program that reads a floating point number and then displays the right-most digit of the integral part of the number.
Read Answers (3) | Asked by : rishi


 Sponsored Links

 
Related Articles

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

How to develop compile and run a C program

The steps involved in building a C program are: 1. First program is created by using any text editor and the file is stored with extension as .c 2. Next the program is compiled. There are many compilers available&nbsp;like GNU C compiler called as gcc, Sun compiler, Borland compiler which is pop
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school&rsquo;s al
 

The Interview Snafu

How to turn someone else&rsquo;s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

Winning a Job Interview with a Winning Resume

Does your resume unlock your potential, take your skills to the highest level and win you the interview and the job you want now? The job market today is highly competitive and even if you think you have what it takes to get an interview you won&rsquo;t get over the line without a polished, prof
 

WinRunner Programming Concepts

If you want to create WinRunner scripts that are highly efficient, there are important programming concepts that you will want to become familiar with. Understanding these concepts will provide you with a large number of key benefits. In addition to understanding these concepts, you must also learn
 

Programming Languages Certification

IT Certification programs have several options that will offer you the best knowledge.&nbsp; By learning everything that you need to know about information technology you will be able to open new doors to your career and personal business desires.&nbsp; IT Certification offers several vari
 

Neuro-linguistic Programming Methods

Neuro linguistic Programming Methods There are several methods used for performing Neuro linguistic Programming on an individual for obtaining insights into the psyche of the person in order to correct to modify certain patterns of behavior These techniques are also used for Neuro linguistic trainin
 

Importance of Proper English during Job Interview

Importance of Proper English during Job Interview Your job interview is crucially important and it will determine whether or not you will get the job Depending on the type of job you re going for it is very important for you to use proper English In most cases jobs which offer higher salaries will h
 

The Difficult Past of Neuro-linguistic programming

The Difficult Past of Neuro linguistic programming Neuro linguistic programming has had a rocky past with a number of lawsuits rivalry unsystematic development and intermittent progress During the 1980s the two founders separated after the lawsuit filed by Bandler Bandler went on to file several law
 





About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape