Thread: Structure Pointer Problem! Help Plss! :D

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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