Thread: Structure Pointer Problem! Help Plss! :D

1. 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("\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");
x=1;//assigning matrix 1
matrixone(x);
print(x);
x=2;//assigning matrix 2
matrixone(x);
print(x);
x=3;//assigning matrix 3 (sum)
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!

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.

3. 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. 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. Originally Posted by nosyaj
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. 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. > 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.