spiral matrix

Hi guys.I need to make a function that generates a spiral matrix but i have troubles with allocating it dynamically..
Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include<iostream.h>
int a[10][10],n,m;
void f(int a[10][10],int m,int n)
		 {int i,j,p,q,k=0;
cout<<"m,n"<<endl;
cin>>m>>n;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
    {cout<<"a["<<i<<"]["<<j<<"]=";
    cin>>a[i][j];
    }

p=m;
q=n;
i=j=1;

while(k<p*q)
{
    for(;j<=n&&k<p*q;j++)
    {
	cout<<a[i][j];
        k++;
    }
    j--;  
    i++;
    for(;i<=m && k<p*q;i++)
    {
        cout<<a[i][j];
        k++;
    }
    i--;
    j--;    
    for(;j>=i-m+1 && k<p*q;j--)
    {
        cout<<a[i][j];
        k++;
    }    
    j++;
    i--;
    for(;i>1 && k<p*q;i--)
    {
        cout<<a[i][j];
        k++;
    }
    if(k<p*q)
    {
        j++;
        i++;
        n--;
        m--;
    }
}
       }



main()
{
 f(a,m,n);
}



And btw my function must look like this void f(int *a,int m,int n)
Last edited on
What is a "spiral matrix"?
What do you mean with "dynamic allocation" in this context?
Use of code tags would be neat.
Well for example if i have a 3x3 matrix and i read the values 1 2 3 4 5 6 7 8 9 the program should generate 1 2 3 6 9 8 7 4 5..but thats not important since my program works fine.Regarding dynamic allocation.....I think i need to use pointers thats why i said i need a function with this prototype void f (int *a,int m,int n) and not something like i did void f(int a[10][10],int m,int n)..No matter what you change i need to have this prototype void f(int *a,int m,int n) and i dont know how to do that.
Last edited on
Anyone?
Edit: just spotted you note way down at the bottom:

And btw my function must look like this void f(int *a,int m,int n)

The int* suggests you need to allocate a linear array and then access the elements by calculating the offset like this: a[10 * i + j]

The other method that I mis-suggested does not allocate contiguous memory so cannot be passed through a char*

Are you expected to use [][] to access the array?

Andy

Edit: old answer -- would only works if your signature was void f(int **a,int m,int n);

See this thread

Allocating and deallocating 2D arrays
http://www.cplusplus.com/forum/unices/61848/

(found by sseaching this site for "dynamic 2D array")

Andy

PS It's in the Unices forum, but the code is standard C++, not *nix specific.

Last edited on
A linear block. Literal "10" would be black magic.

If the 2D matrix should have Columns columns and Rows rows, then it must have Columns*Rows cells. You could play with where in array each cell[row, col] is, but lets stick to the traditional row-major.

1
2
3
4
5
size_t Columns = 42;
size_t Rows = 42;
int * foo = new [Columns * Rows];
// use foo
delete [] foo;

Setting values:
1
2
3
4
5
for ( size_t row = 0; row < Rows; ++row ) {
  for ( size_t col = 0; col < Columns; ++col ) {
    foo[ Columns*row + col ] = rand();
  }
}
Ok .I repaired my program and now it looks at it should be but its not working for 3x3 matrices and above and i still dont get it why because i checked every line and my code seems correct.Some help would be really nice
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<cstdio>
#include<iostream>
using namespace std;

void f(int *a,int m,int n)
		 {int i,j,p,q,k=0;

         for(i=1;i<=m;i++)
         for(j=1;j<=n;j++)
                          {
                 cout<<"a["<<i<<"]["<<j<<"]=";
                 cin>>a[i*n+j];
                               }

         p=m;
         q=n;
         i=j=1;

         while(k<p*q)
                     {
                     for(;j<=n&&k<p*q;j++)
                     {
	                 cout<<a[i*n+j]<<' ';
                     k++;
                     }
                     j--;  
                     i++;
                     for(;i<=m && k<p*q;i++)
                     {
                     cout<<a[i*n+j]<<' ';
                     k++;
                     }
                     i--;
                     j--;    
                     for(;j>=i-m+1 && k<p*q;j--)
                     {
                     cout<<a[i*n+j]<<' ';
                     k++;
                     }    
                     j++;
                     i--;
                     for(;i>1 && k<p*q;i--)
                     {
                     cout<<a[i*n+j]<<' ';
                     k++;
                     }
                     if(k<p*q)
                     {
                     j++;
                     i++;
                     n--;
                     m--;
                     }
                     }

       }



int main()
{int m,n;
cout<<"m=";cin>>m;
cout<<"n=";cin>>n;
int *a = new int[m * n];
system("pause");

 f(a,m,n);

 system("pause");
 return 0;
 

}




..For example if i have a 3x3 matrix and i enter 1 2 3 4 5 6 7 8 9 my program generates 1 2 3 6 9 8 7 4 3 instead of 1 2 3 6 9 8 7 4 5
Last edited on
Please edit your posts to use code tags (using the <> button to the right of the cplusplus.com's post- editing text box.

Then we'll be able to see what's up!

Andy

PS See the article "How to use tags" for more info
http://www.cplusplus.com/forum/beginner/102835/#msg554256
The first, elementary issue is the for(i=1;i<=m;i++). That uses indices in range from 1 to m. However, the valid indices are in range from 0 to m-1.
And if you look at my first post the program looks very similar with the last i posted....Why the first works fine since i havent changed much in"for loops"?

Ps:just added code tags
run your original with n=m=10. Should fail.
Topic archived. No new replies allowed.