Structure Pointer Problem! Help Plss! :D

This is a discussion on Structure Pointer Problem! Help Plss! :D within the C Programming forums, part of the General Programming Boards category; As the rules and as person having dignity just tell me what the problem is and a probable solution and ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Unhappy Structure Pointer Problem! Help Plss! :D

    As the rules and as person having dignity just tell me what the problem is and a probable solution and I'll do the rest. I absolutely have no idea what the problem is and I am confused with structure pointers. Here is the code and the problem is in the part where I add the values of the structures.

    Code:
    /*Variety of Program for Matrices*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    
    struct matrix{				//Reference of function for the Matrices
           double** pElements[10][10]; //Pointer for the elements of Matrices
           int MRow;
           int NColumn;   
    }val[10];
    
    int rowcol(int &row, int &col, int &x){//Asks for the values of Row and Column
        printf("\n\nEnter value for row: ");
        scanf("%d",&row);
        printf("Enter value for col: ");
        scanf("%d",&col);
        val[x].MRow=row;    //Stores assigned values of Row and Columns
        val[x].NColumn=col;
    }
    
    int matrixone(int &x){//Asking for values for elements of matrix
        int row,col,i,j;
        printf("\n\nMATRIX %d: ",x);
        rowcol(row,col,x);
        for(i=1;i<=row;i++){
           for(j=1;j<=col;j++){
           printf("Enter value for row [%d] and col [%d]: ",i,j);
           scanf("%d",&val[x].pElements[i][j]);//Stores elements of matrix 
                              }
                              }
    }
    
    int print(int &x){//Printing of the matrix elements
        int row,col,i,j;
        printf("\nMatrix %d is: \n",x);
        for(i=1;i<=row;i++){
        for(j=1;j<=col;j++){
        printf("%d  ",val[x].pElements[i][j]);
        }
        printf("\n");
        }
    }
    
    int adding(int &x){      <------------------------NEED HELP HERE!!!!!!!!!!!!!!!
        int row,col,i,j;  
        if(val[1].MRow==val[2].MRow&val[1].NColumn==val[2].NColumn){
            for(i=1;i<=row;i++){    
               for(j=1;j<=col;j++){
               **val[3].pElements[i][j] = **val[1].pElements[i][j] + **val[2].pElements[i][j];
                                  }
                                  }
               }//end of If Statement
               else
               printf("\nRows/Columns for 2 matrices are not equal! \n");
    }
    
    int subtract(int &x){
        int row,col,i,j;
        if(val[1].MRow==val[2].MRow&val[1].NColumn==val[2].NColumn){
            for(i=1;i<=row;i++){
               for(j=1;j<=col;j++){
              // val[3].pElements[i][j] = val[1].pElements[i][j] - val[2].pElements[i][j];
                                  }
                                  }
               }//end of If Statement
               else
               printf("\nRows/Columns for 2 matrices are not equal! \n");
    }
    
    int mult(int &x){
        int row,col,i,j;
        if(val[1].NColumn==val[2].MRow){
            for(i=1;i<=row;i++){
               for(j=1;j<=col;j++){
              // val[3].pElements[i][j] = val[1].pElements[i][j] - val[2].pElements[i][j];
                                  }
                                  }
               }//end of If Statement
               else
               printf("\nColumn of Matrix is not equal to Row of Matrix 2!\n");
    }
    
    int summate(int &x){
       int row,col,i,j=1,inc=0;
       do{
           for(i=1;i<=row;i++){
              //val[x].pElements[i][j+inc] = val[x].pElements[i][j+inc] + val[x].pElements[i][j];
                                }
             inc++;    
        }while((j+inc)<=col);
    }
    
    int transpose(int &x){
        int row,col,i,j,temp;
        for(i=1;i<=row;i++){
           for(j=1;j<=col;j++){
           val[2].pElements[j][i]= val[1].pElements[i][j];
                              }
                              }
           
        printf("\nMatrix %d is: \n",x);
        x=2;
        for(i=1;i<=col;i++){
        for(j=1;j<=row;j++){
        printf("%d  ",val[x].pElements[i][j]);
        }
        printf("\n");
        }
    }
    main(){
       int choice;
       int row,col;
       int i,j,x;
       
       printf(" !MATRIX!MATRIX!MATRIX!MATRIX!MATRIX!MATRIX!MATRIX!MATRIX!MATRIX!MATRIX!MATRIX!\n");
       printf("--------------------------------------------------------------------------------\n");
    
       printf("OPERATION OF MATRICES\n");
       printf("\n1)Matrix Addition");
       printf("\n2)Matrix Subtraction");
       printf("\n3)Matrix Multiplications");
       printf("\n4)Computation of determinant");
       printf("\n5)Summation of rows and columns");
       printf("\n6)Matrix Inverse");
       printf("\n7)Matrix Transpose");
       printf("\n\nWhich operation to work on: ");
       scanf("%d",&choice);
       
       switch(choice){
                         case 1:
                              system("cls");
                              printf("MATRIX ADDITION");
                              x=1;//assigning matrix 1
                              matrixone(x);
                              print(x);
                              x=2;//assigning matrix 2
                              matrixone(x);
                              print(x);
                              x=3;//assigning matrix 3 (sum)
                              adding(x);
                              print(x);
                              break;
                         case 2:
                              system("cls");
                              printf("MATRIX SUBTRACTION");
                              x=1;//assigning matrix 1
                              matrixone(x);
                              print(x);
                              x=2;//assigning matrix 2
                              matrixone(x);
                              print(x);
                              x=3;//assigning matrix 3 (difference)
                              subtract(x);
                              print(x);
                              break;
                         case 3:
                              system("cls");
                              printf("MATRIX MULTIPLICATION");
                              x=1;//assigning matrix 1
                              matrixone(x);
                              print(x);
                              x=2;//assigning matrix 2
                              matrixone(x);
                              print(x);
                              x=3;//assigning matrix 3 (sum)
                              mult(x);
                              print(x);
                              break;
                         case 4:
                              system("cls");
                              printf("DETERMINANT OF A MATRIX");
                              break;
                         case 5:
                              system("cls");
                              printf("SUMMATION OF ROWS and COLUMNS");
                              break;
                         case 6:
                              system("cls");
                              printf("MATRIX INVERSE");
                              break;
                         case 7:
                              system("cls");
                              printf("MATRIX TRANSPOSE");
                              x=1;//assigning a matrix
                              matrixone(x);
                              print(x);
                              transpose(x);
                              break;
                         default:printf("Invalid Entry");
       }
    
    getch();
    }
    I only need help in knowing the problem in the passed by value of adding(). The program just stops working when the addition process takes place. Need Help Thanks!
    Last edited by Salem; 04-05-2011 at 03:54 AM. Reason: Moved code tags, so prose wraps properly

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Please read:

    How To Ask Questions The Smart Way

    Take the question out of the "code" tags, it is impossible to read because there are no line breaks. Use the edit button. Then reply.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    struct matrix{				//Reference of function for the Matrices
           double** pElements[10][10]; //Pointer for the elements of Matrices
           int MRow;
           int NColumn;   
    }val[10];
    Why are you using pointers for pElements? That's totally unnecessary.

    Code:
    struct matrix{				//Reference of function for the Matrices
           double Elements[10][10]; 
           int MRow;
           int NColumn;   
    }val[10];
    Ok... so now you have an array of 10 val structs... use them like this...
    Code:
    int adding(int &x){      <------------------------NEED HELP HERE!!!!!!!!!!!!!!!
        int row,col,i,j;  
        if(val[1].MRow==val[2].MRow & val[1].NColumn==val[2].NColumn){
            for(i=0;i < row;i++)    
               for(j=0;j < col;j++)
                  val[3].Elements[i][j] = val[1].Elements[i][j] + val[2].Elements[i][j];
               }//end of If Statement
               else
               printf("\nRows/Columns for 2 matrices are not equal! \n");
    }
    Notice that you never acttualy add x to anything in this function... in fact x is unused.

    Also note that in C array indexes begin at 0, not 1 ... an array of 5 elements will have valid indexes 0, 1, 2, 3 and 4 ... go ahead count them, that's 5 elements.

    You will probably want to make similar changes throughout the program...

    Simple strategy... if you want life to be much simpler, don't use pointers unless you absolutely have to.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Sorry for not writing properly, I am new here. Will right properly next time.

    I forgot to mention that the variables in the structures are given parameters (for our project) in which we should use and implement which are double** pElements, MRow and NColumn (we can rename variables but the pointer stays the same).

    I tried your version of removing the pointer but since I declared pElements as a pointer, removing the ** will not operate because (a friend of mine said) I am adding addresses which is invalid. Although thanks for the advice.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nosyaj View Post
    I forgot to mention that the variables in the structures are given parameters (for our project) in which we should use and implement which are double** pElements, MRow and NColumn (we can rename variables but the pointer stays the same).
    Ok... if they HAVE to be pointers... you need to allocate memory for each element in the matrix.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Can you give an example? we weren't taught dynamic allocation but rather we store values using structures.

    If for example, val[1].pElements[1][1] = 20
    Am i assigning 20 to address of val[1]?

    So should it be **val[1].pElements[1][1] = 20 ? wherein I am storing 20 as a value of val[1]?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > int rowcol(int &row, int &col, int &x)
    The whole code (apart from this) reads as 'C' code.
    The & notation here are C++ reference parameters.

    So choose your language, and set your compiler to compile that language.
    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. Creating a Pointer to a Structure
    By Char*Pntr in forum C Programming
    Replies: 5
    Last Post: 08-21-2010, 05:36 PM
  2. Pointer to Structure error explanation
    By hellogamesmaste in forum C Programming
    Replies: 5
    Last Post: 08-15-2009, 10:37 AM
  3. Problem with custom structure
    By stickman in forum C++ Programming
    Replies: 9
    Last Post: 10-14-2006, 02:57 PM
  4. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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