Major problem with arrays

This is a discussion on Major problem with arrays within the C Programming forums, part of the General Programming Boards category; My main issue is trying to make one array take the values of an other the problem starts on line ...

  1. #1
    CJM
    CJM is offline
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Major problem with arrays

    My main issue is trying to make one array take the values of an other

    the problem starts on line 164 of my code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    
    int bMATRIX[10][10];
    void DETINV(int mSIZE, double **MATB);
    void arg(int *a,int *b,int *n,int x,int y);
    int det(int *p,int *n);
    double **MATa;
    double **MATB;
    double *MATb;
    double *MATc;
    double *MATcBT;
    
    int main()
    {
      int nSIZE = 1;
      int mSIZE = 1;
      int n1SIZE = 1;
    
      int i, j;
    
          printf("REVERSE SIMPLEX METHOD \n\nEnter size [n] : ");
          scanf("%d", &nSIZE);
    	  printf("Enter size [m] : ");
          scanf("%d", &mSIZE);
    
    	  n1SIZE = nSIZE-mSIZE;
    	  while ( n1SIZE <= 0 )
    	  {
    		printf("\n!!INVALID MATRIX SIZE!!\nPlease reenter size [n] : ");
    		scanf("%d", &nSIZE);
    		printf("Please reenter size [m] : ");
    		scanf("%d", &mSIZE);
    		n1SIZE = nSIZE-mSIZE;
    	  }
    
    ////////////////////////////////////////////
          MATa = malloc(mSIZE*sizeof*MATa);
    
          for(i=0;i<mSIZE;++i)
    	  {
    		  MATa[i]=malloc(nSIZE*sizeof**MATa);
    	  }
    ////////////////////////////////////////////
    	  MATB = malloc(mSIZE*sizeof*MATB);
    
          for(i=0;i<mSIZE;++i)
    	  {
    		  MATB[i]=malloc(mSIZE*sizeof**MATB);
    	  }
    
    ////////////////////////////////////////////	
    	  MATc = malloc(nSIZE*sizeof*MATc);
    ////////////////////////////////////////////	
    	  MATb = malloc(mSIZE*sizeof*MATb);
    ////////////////////////////////////////////
    
    ////////////////////////////////////////////
          printf("Please enter values of\n");
    	  for(i=0;i<mSIZE;i++)
    	  {
    		for(j=0;j<nSIZE;j++)
    		  {	
    			printf("a%d,%d: ", i+1, j+1);
    			scanf( "%lf", &MATa[i][j]);
    		  }
    	  }
    
          printf("Please enter values of\n");
    	  for(i=0;i<mSIZE;i++)
    	  {
    		printf("b%d,1: ", i+1);
    		scanf( "%lf", &MATb[i]);
    	  }
    	  printf("Please enter values of\n");
    	  for(i=0;i<nSIZE;i++)
    	  {
    		printf("c1,%d: ", i+1);
    		scanf( "%lf", &MATc[i]);
    	  }
    ///////////////////////////////////////
    	  printf("This is called matrix 'A'=\n");
    	  for(i=0;i<mSIZE;i++)
    		{
    			for(j=0;j<nSIZE;j++)
    				{
    					printf("%1.0lf  ", MATa[i][j]);
    				}
    			printf("\n");
    		}
    	  printf("\n");
    	  printf("This is called matrix 'b'=\n");
    	  for(i=0;i<mSIZE;i++)
    		{
    			printf("%1.0lf", MATb[i]);
    			printf("\n");
    		}
    	  	  printf("This is called matrix 'c'=\n");
    	  for(i=0;i<nSIZE;i++)
    		{
    			printf("%1.0lf  ", MATc[i]);
    			
    		}
    ////////////////////////////////////////
    
    ////////////////////////////////////////
    	  printf("\nThis gives matrix [identy] 'B':\n");
    	  for(i=0;i<mSIZE;i++)
    	  {
    		for(j=n1SIZE;j<nSIZE;j++)
    		{
    			MATB[i][j] = MATa[i][j];
    			printf("[%d][%d] ", i, j); 
    			printf("%1.0lf  ", MATB[i][j]);
    		}
    	    printf("\n");
    	  }
    	  printf("\n");	
    //////////////////////////////////////////	  
    	  printf("\nThis gives matrix 'K':\n");
    	  for(i=0;i<mSIZE;i++)
    	  {
    		for(j=0;j<n1SIZE;j++)
    		{
    			printf("%1.0lf  ", MATa[i][j]);
    		}
    	    printf("\n");
    	  }
    	  printf("\n");
    ///////////////////////////////////////
    
    ///////////////////////////////////////
    
    printf("\nNow to caculate matrix 'cB'=\n");
    	  for(i=0;i<n1SIZE;i++)
    		{
    			printf("%1.0lf  ", MATc[i]);
    			
    		}
    printf("\nTransposing matrix 'cB' gives 'cBT'=\n");
    	  MATcBT = malloc(n1SIZE*sizeof*MATcBT);
    	  for(i=0;i<n1SIZE;i++)
    		{
    			MATcBT[i] = MATc[i];
    			printf("%1.0lf\n", MATcBT[i]);
    		}
    
    printf("\n\n\n!!ALL WORKING TO HERE!!\n\n\n");
    DETINV(mSIZE, MATB);//this function call doesnt work :(
    
    
        //  for(i=0;i<mSIZE;++i) free(MATa[i]);
         // free(MATa);
        }
       
    void DETINV(int mSIZE, double **MATB)
    {
    int a[10][10],b[10][10],c[10][10];
    int n,n1,i,j,m,d,q;
    n = mSIZE;
    n1= mSIZE;
    	  for(i=1;i<(mSIZE+1);i++)
    	  {
    		for(j=1;j<(mSIZE+1);j++)
    		{
    			q = (double)MATB[i][j];
    
    		}
    	    printf("\n");
    	  }
    
    	   printf("\setting up data for the functions\n");
    	  for(i=1;i<(mSIZE+1);i++)
    	  {
    		for(j=1;j<(mSIZE+1);j++)
    		{
    			printf("[%d] [%d] ", i, j); 
    			printf("%d ", a[i][j]);
    			a[i][j] = q;
    
    		}
    	    printf("\n");
    	  }
    
    if(n==2)
    {
    c[0][0]=a[1][1];
    c[1][1]=a[0][0];
    c[0][1]=-a[0][1];
    c[1][0]=-a[1][0];
    d=a[0][0]*a[1][1]-a[0][1]*a[1][0];
    printf("Determinant is:%d\n",d);
    if(d==0)
    {
    getch();
    exit(d-'0');
    }
    
    for(i=0;i<n;i++)
    {
    printf("\n");
    for(j=0;j<n;j++)
    printf(" %f",c[i][j]/(float)d);
    
    }
    }
    else
    {
    m=n;
    for(i=0;i<m;i++)
    {
    for(j=0;j<m;j++)
    {
    n=m;
    arg(&a[0][0],&b[0][0],&n,i,j);
    c[j][i]=pow(-1,(i+j))*det(&b[0][0],&n);
    }
    }
    n=m;
    d=det(&a[0][0],&n);
    printf("Determinant is :%d\n",d);
    if(d==0)
    {
    printf("INVERSE DOES NOT EXIST");
    getch();
    exit(d-'0');
    }
    printf("Inverting matrix 'B' gives 'B-1'=\n");
    for(i=0;i<m;i++)
    {
    printf("\n");
    for(j=0;j<m;j++)
    printf(" %f",c[i][j]/(float)d);
    }
    }	 getch();
    }
    
    void arg(int *a,int *b,int *n,int x,int y)
    {
    int k,l,i,j;
    for(i=0,k=0;i<*n;i++,k++)
    {
    for(j=0,l=0;j<*n;j++,l++)
    {
    if(i==x)
    i++;
    if(j==y)
    j++;
    *(b+10*k+l)=*(a+10*i+j);
    
    }
    }
    *n=*n-1;
    }
    
    int det(int *p,int *n)
    {
    int d[10][10],i,j,m,sum=0;
    m=*n;
    if(*n==2)
    return(*p**(p+11)-*(p+1)**(p+10));
    for(i=0,j=0;j<m;j++)
    {
    *n=m;
    arg(p,&d[0][0],n,i,j);
    sum=sum+*(p+10*i+j)*pow(-1,(i+j))*det(&d[0][0],n);
    }
    
    return(sum);
    }
    Generic help will also be appreciated.
    my arrays are of different type as well one double and one int.
    specific answers preferred.
    thanks!!!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    How about the fact that you are passing a double* as second argument to a function that takes a double**. Your compiler should be screaming at you and you should always post the errors your compiler throws at you to save us some time in reading over the code.

    Cheers.

    EDIT: NEVERMIND I MISREAD.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What exactly is the problem with the call? Compile error? Not doing what you want it to do? WHAT do you want it to do? If you ask SPECIFIC questions you will get SPECIFIC answers.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Here is another tip: fix the indentation. Nobody is going to bother looking through the code otherwise, and it will help you identify problems more quickly
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Next time you want something from line 161... how about you mark the line for us. I'm not going to spend my time counting lines beyond about 10.

    However; I did spot this...
    Code:
    c[0][0]=a[1][1];
    c[1][1]=a[0][0];
    c[0][1]=-a[0][1];
    c[1][0]=-a[1][0];
    In the last two lines were you trying to subract? -= is one thing =- is something else.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Your code, courtesy of indent -kr -nut -ts2 foo.c

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<stdlib.h>
    
    int bMATRIX[10][10];
    void DETINV(int mSIZE, double **MATB);
    void arg(int *a, int *b, int *n, int x, int y);
    int det(int *p, int *n);
    double **MATa;
    double **MATB;
    double *MATb;
    double *MATc;
    double *MATcBT;
    
    int main()
    {
        int nSIZE = 1;
        int mSIZE = 1;
        int n1SIZE = 1;
    
        int i, j;
    
        printf("REVERSE SIMPLEX METHOD \n\nEnter size [n] : ");
        scanf("%d", &nSIZE);
        printf("Enter size [m] : ");
        scanf("%d", &mSIZE);
    
        n1SIZE = nSIZE - mSIZE;
        while (n1SIZE <= 0) {
            printf("\n!!INVALID MATRIX SIZE!!\nPlease reenter size [n] : ");
            scanf("%d", &nSIZE);
            printf("Please reenter size [m] : ");
            scanf("%d", &mSIZE);
            n1SIZE = nSIZE - mSIZE;
        }
    
    ////////////////////////////////////////////
        MATa = malloc(mSIZE * sizeof *MATa);
    
        for (i = 0; i < mSIZE; ++i) {
            MATa[i] = malloc(nSIZE * sizeof **MATa);
        }
    ////////////////////////////////////////////
        MATB = malloc(mSIZE * sizeof *MATB);
    
        for (i = 0; i < mSIZE; ++i) {
            MATB[i] = malloc(mSIZE * sizeof **MATB);
        }
    
    ////////////////////////////////////////////
        MATc = malloc(nSIZE * sizeof *MATc);
    ////////////////////////////////////////////
        MATb = malloc(mSIZE * sizeof *MATb);
    ////////////////////////////////////////////
    
    ////////////////////////////////////////////
        printf("Please enter values of\n");
        for (i = 0; i < mSIZE; i++) {
            for (j = 0; j < nSIZE; j++) {
                printf("a%d,%d: ", i + 1, j + 1);
                scanf("%lf", &MATa[i][j]);
            }
        }
    
        printf("Please enter values of\n");
        for (i = 0; i < mSIZE; i++) {
            printf("b%d,1: ", i + 1);
            scanf("%lf", &MATb[i]);
        }
        printf("Please enter values of\n");
        for (i = 0; i < nSIZE; i++) {
            printf("c1,%d: ", i + 1);
            scanf("%lf", &MATc[i]);
        }
    ///////////////////////////////////////
        printf("This is called matrix 'A'=\n");
        for (i = 0; i < mSIZE; i++) {
            for (j = 0; j < nSIZE; j++) {
                printf("%1.0lf  ", MATa[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        printf("This is called matrix 'b'=\n");
        for (i = 0; i < mSIZE; i++) {
            printf("%1.0lf", MATb[i]);
            printf("\n");
        }
        printf("This is called matrix 'c'=\n");
        for (i = 0; i < nSIZE; i++) {
            printf("%1.0lf  ", MATc[i]);
    
        }
    ////////////////////////////////////////
    
    ////////////////////////////////////////
        printf("\nThis gives matrix [identy] 'B':\n");
        for (i = 0; i < mSIZE; i++) {
            for (j = n1SIZE; j < nSIZE; j++) {
                MATB[i][j] = MATa[i][j];
                printf("[%d][%d] ", i, j);
                printf("%1.0lf  ", MATB[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    //////////////////////////////////////////
        printf("\nThis gives matrix 'K':\n");
        for (i = 0; i < mSIZE; i++) {
            for (j = 0; j < n1SIZE; j++) {
                printf("%1.0lf  ", MATa[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    ///////////////////////////////////////
    
    ///////////////////////////////////////
    
        printf("\nNow to caculate matrix 'cB'=\n");
        for (i = 0; i < n1SIZE; i++) {
            printf("%1.0lf  ", MATc[i]);
    
        }
        printf("\nTransposing matrix 'cB' gives 'cBT'=\n");
        MATcBT = malloc(n1SIZE * sizeof *MATcBT);
        for (i = 0; i < n1SIZE; i++) {
            MATcBT[i] = MATc[i];
            printf("%1.0lf\n", MATcBT[i]);
        }
    
        printf("\n\n\n!!ALL WORKING TO HERE!!\n\n\n");
        DETINV(mSIZE, MATB);        //this function call doesnt work :(
    
    
        //  for(i=0;i<mSIZE;++i) free(MATa[i]);
        // free(MATa);
    }
    
    void DETINV(int mSIZE, double **MATB)
    {
        int a[10][10], b[10][10], c[10][10];
        int n, n1, i, j, m, d, q;
        n = mSIZE;
        n1 = mSIZE;
        for (i = 1; i < (mSIZE + 1); i++) {
            for (j = 1; j < (mSIZE + 1); j++) {
                q = (double) MATB[i][j];
    
            }
            printf("\n");
        }
    
        printf("\setting up data for the functions\n");
        for (i = 1; i < (mSIZE + 1); i++) {
            for (j = 1; j < (mSIZE + 1); j++) {
                printf("[%d] [%d] ", i, j);
                printf("%d ", a[i][j]);
                a[i][j] = q;
    
            }
            printf("\n");
        }
    
        if (n == 2) {
            c[0][0] = a[1][1];
            c[1][1] = a[0][0];
            c[0][1] = -a[0][1];
            c[1][0] = -a[1][0];
            d = a[0][0] * a[1][1] - a[0][1] * a[1][0];
            printf("Determinant is:%d\n", d);
            if (d == 0) {
                getch();
                exit(d - '0');
            }
    
            for (i = 0; i < n; i++) {
                printf("\n");
                for (j = 0; j < n; j++)
                    printf(" %f", c[i][j] / (float) d);
    
            }
        } else {
            m = n;
            for (i = 0; i < m; i++) {
                for (j = 0; j < m; j++) {
                    n = m;
                    arg(&a[0][0], &b[0][0], &n, i, j);
                    c[j][i] = pow(-1, (i + j)) * det(&b[0][0], &n);
                }
            }
            n = m;
            d = det(&a[0][0], &n);
            printf("Determinant is :%d\n", d);
            if (d == 0) {
                printf("INVERSE DOES NOT EXIST");
                getch();
                exit(d - '0');
            }
            printf("Inverting matrix 'B' gives 'B-1'=\n");
            for (i = 0; i < m; i++) {
                printf("\n");
                for (j = 0; j < m; j++)
                    printf(" %f", c[i][j] / (float) d);
            }
        } getch();
    }
    
    void arg(int *a, int *b, int *n, int x, int y)
    {
        int k, l, i, j;
        for (i = 0, k = 0; i < *n; i++, k++) {
            for (j = 0, l = 0; j < *n; j++, l++) {
                if (i == x)
                    i++;
                if (j == y)
                    j++;
                *(b + 10 * k + l) = *(a + 10 * i + j);
    
            }
        }
        *n = *n - 1;
    }
    
    int det(int *p, int *n)
    {
        int d[10][10], i, j, m, sum = 0;
        m = *n;
        if (*n == 2)
            return (*p ** (p + 11) - *(p + 1) ** (p + 10));
        for (i = 0, j = 0; j < m; j++) {
            *n = m;
            arg(p, &d[0][0], n, i, j);
            sum =
                sum + *(p + 10 * i + j) * pow(-1, (i + j)) * det(&d[0][0], n);
        }
    
        return (sum);
    }
    > for (i = 1; i < (mSIZE + 1); i++)
    Array indexing starting at 1, that's never a good sign.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21