CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: spiral matrix

  1. #1
    Join Date
    May 2013
    Posts
    1

    spiral matrix

    Hi guys.Although it may sound like a noob question ,how can i make a function that generates a spiral matrix but it MUST have this prototype void f(int *a,int m,in n).My program works fine but like i said i have no idea how to make it with that prototype.Here is my code

    #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);
    sytem("pause");
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: spiral matrix

    Before posting code, please format properly with indents etc and use code tags (Go Advanced, select code and click '#').

    My program works fine
    No it doesn't - even if it seems to!

    Your code as posted doesn't conform to standard c++.

    Code:
    #include<iostream.h>
    ..
    main()
    In standard c++, the correct include would be #include <iostream>. Also the function main returns an int.

    Also, your current program has problems with array bounds. In c++ an array index starts at 0 - not 1. So the code

    Code:
    for(i=1;i<=m;i++)
    for(j=1;j<=n;j++)
    {cout<<"a["<<i<<"]["<<j<<"]=";
    cin>>a[i][j];
    }
    does not use the first element (0, 0) and tries to access elements past the end of the array causing buffer overflow problems.

    This code should be

    Code:
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    {cout<<"a["<<i<<"]["<<j<<"]=";
    cin>>a[i][j];
    }
    c++ doesn't check for buffer overflow errors - it just goes ahead and tries to do what the program code says. Where there is a memory problem the program may appear to work sometimes but is actually corrupting memory which may cause problems later on in the program and which can be hard to trace as the problem may have occured in a different part of the program to that which is showing an error.
    Last edited by 2kaud; May 23rd, 2013 at 11:48 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: spiral matrix

    Quote Originally Posted by tqit View Post
    My program works fine
    You will need to prove that. There are things wrong with the code you posted:

    1) Use code tags when posting code, as the code you posted is almost unreadable without code tags.

    2) What compiler are you using?
    Code:
    #include<iostream.h>
    There is no such header file as <iostream.h> in any version of Visual Studio beyond 2002. The correct, standard header is <iostream>, not <iostream.h>

    3) What happens if more than 10 numbers are entered? You go over the edge of your array's boundaries, causing undefined behaviour, more than likely, a crash.

    4) Arrays in C++ start at 0, not 1. Why are your loops starting at 1 when accessing the array?
    Code:
       int a[10][10], m, n;
       //...
       for(i=1;i<=m;i++)
           for(j=1;j<=n;j++)
       //...
    Code like this is asking for trouble. As stated, arrays in C++ start from 0 and go up to n-1, where "n" is the number of elements.

    Trying to trick C++ into thinking that arrays start at 1 invariably leads to a buffer overrun on the far end of the array, or if not that, using the 0 index of the array which hasn't been properly initialized (in other words, a[0] has junk data, but somewhere in the code, it accesses a[0]).

    In your example, you have a bug -- if m is 10, you are now writing at "a[10][whatever]" at the last iteration of the loop, causing a buffer overrurn. The reason is that the highest index is 9, not 10. The same with the column index (n).

    You should be starting your loops at 0, and then going up to 9 (one less than the total number of columns/rows)

    5) Again, what compiler are you using?
    Code:
    main()
    All functions must specify the return type. The main() function returns an int.
    Code:
    int main()
    The above is the correct code.

    So your program is not "fine". It doesn't compile, and even if it did, there is a buffer overrun if m or n is 10.

    As to your question -- you can't use a two-dimensional array if the prototype takes just an int*. You need to simulate a two dimensional array by creating a 1-dimensional array, and then using the indexing to simulate a 2-d array's row and column structure. There are many examples all over the Internet and even here showing how to mimic a 2-d array using a 1-d array.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 23rd, 2013 at 11:39 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: spiral matrix

    If you want to use a prototype (why??) of

    Code:
    void f(int *a,int m,in n);
    then a is a pointer to the first memory address where the integers are stored, and m * n is the number of integers to which a points. So a[0] accesses the first number and a[m * n - 1] accesses the last number. On this basis, a 2 dimensional array can be considered as sequential memory and can be accessed as such.

    As you have m and n as parameters to function f, then IMO it would be better if function main asked for the array size and then passed these sizes to function f. Also, a, m and n would be defined in function main so you would not need any global variables.

    Also as you are inputting the size of the array, then rather than using a fixed size array (currently 10 by 10 - what happens if the user inputs 12 and 12?) the array could be dynamically allocated in main giving

    Code:
    //Ask user for m and n
    
    int *a = new int[m * n];
    f(a, m, n);
    delete [] a;
    where f has the required prototype.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured