Rookie needs help~~~

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asgard0422
    New Member
    • Apr 2008
    • 2

    Rookie needs help~~~

    i am just starting c++ few weeks ago, and i am facing a matrix problem now.

    the program has to be built so that the user can select the size of the matrix but it will only be odd numbers.such as 1,3,7 etc

    then the out put of the matrix needs to be

    9 2 3
    8 1 4
    7 6 5

    in this form
    the middle element needs to be the 1 and then it assign values in a cyclic format.

    Thanks~~
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by asgard0422
    i am just starting c++ few weeks ago, and i am facing a matrix problem now.

    the program has to be built so that the user can select the size of the matrix but it will only be odd numbers.such as 1,3,7 etc

    then the out put of the matrix needs to be

    9 2 3
    8 1 4
    7 6 5

    in this form
    the middle element needs to be the 1 and then it assign values in a cyclic format.

    Thanks~~
    Can we see what have you tried so far?

    Comment

    • asgard0422
      New Member
      • Apr 2008
      • 2

      #3
      sure. but i tried ages . but it worked for 3. and 5. but wont work for more than 5..i am so ......

      here is the program i had
      #include<stdio. h>
      #include<math.h >

      int main (void)
      {
      int i=0,j=0,k=0,l,m , a[100][100],sum=0,z=0,p=0, b=1,q,c=0;
      int j_increment, i_increment;

      printf("Please define the size of the matrix:");
      scanf("%d",&m);


      l=(m-1)/2;
      a[l][l]=1;
      k=1;
      q=1;
      i=l-1;
      j=l;
      do{


      for (b=0;b<2;b++)
      {
      j_increment = pow(-1,p);
      i_increment = pow(-1,p);
      p=p++;
      for (j=l;(j<=l-1+2*k)&(j>=l+1-2*k);j=j+j_incr ement)
      {
      a[i][j]=q+1;
      q=q++;
      }
      //have something more here before to make the program work for 3 and 5, but i deleted as it actually was only for 3 and 5.......and making errors for the size greater than 5;

      for (i=l;(i<=l-1+2*k)&(i>=l+1-2*k);i=i+i_incr ement)
      {
      a[i][j]=q+1;
      q=q++;
      }

      }
      l=j;
      i=l-1;
      k=k++;
      }while (2*k<m);

      for(i=0;i<m;i=i ++)
      {
      for(j=0;j<m;j=j ++)
      {
      if (c%m==0)
      {
      printf("\n");
      }
      c=c++;
      printf("%d ",a[i][j]);
      }
      }


      return 0;
      }
      i wonder if my logic was right, and besides. if you can tell me some hints for this, i would be more appreciated

      Comment

      Working...