1. thinking process

Hey all!
So in the code I have posted below, the user can assign upto 4 matrices A,B,C or D. Now I want to do amtrices addition between them. I am using a switch statement but I just don't know what to do. I need some help thinking of a way.

Code:
```#include<stdio.h>
#include <stdlib.h>

void matrix_display(int ****matrix, int row_count, int column_count);

int main (void)
{
int row_size1,col_size1,row_size2,col_size2,flag =1;
int ****A[4];
int i = 0, j = 0;
char a,c;

do
{

printf("\ncmd> ");
c = getchar();

if(c == 'A')
{
scanf("%d %d",&row_size1,&col_size1);

A[0]= malloc(row_size1*sizeof(int*));

for(i=0;i<row_size1;i++)
{
A[0][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[0][i][j]);
//      if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}

matrix_display(A[0], row_size1, col_size1);
}

if(c == 'B')
{
scanf("%d %d",&row_size1,&col_size1);

A[1]= malloc(row_size1*sizeof(int*));

for(i=0;i<row_size1;i++)
{
A[1][i]=malloc(col_size1*sizeof(int));
}

for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[1][i][j]);
//      if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}

matrix_display(A[1], row_size1, col_size1);
}

if(c == 'C')
{
scanf("%d %d",&row_size1,&col_size1);

A[2]= malloc(row_size1*sizeof(int*));

for(i=0;i<row_size1;i++)
{
A[2][i]=malloc(col_size1*sizeof(int));
}

for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[2][i][j]);
//      if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}

matrix_display(A[2], row_size1, col_size1);
}

if(c == 'D')
{
scanf("%d %d",&row_size1,&col_size1);

A[3]= malloc(row_size1*sizeof(int*));

for(i=0;i<row_size1;i++)
{
A[3][i]=malloc(col_size1*sizeof(int));
}

for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[3][i][j]);
//      if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[3], row_size1, col_size1);
}

if ( c == '+')
{
scanf("%c %c", &ptr1, ptr2);
switch(ptr1);

case 'A':
ptr1=A[0];
break;

case 'B':
ptr1=A[1];
break;

case 'C':
ptr1=A[2];
break;

case 'D':
ptr1=A[3];
break;

switch(ptr2);

case 'A':
ptr2=A[0];
break;

case 'B':
ptr2=A[1];
break;

case 'C':
ptr2=A[2];
break;

case 'D':
ptr2=A[3];
break;
}
}
while(flag == 1);
//return 0;
}

void matrix_display(int ****A, int row_size1, int col_size1)
{
int i,j;
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}```

2. OMG a Four Star Programmer!

That code is all so horribly wrong. Just stop what you're doing and try something a lot simpler.

3. Please don't hate on my program it took me foreverrrrrrrr to get it working
but if you really think its necessary for me to change it, im willing to go through that long arduous journey again.
but if its not that important could you help me with the addition bit

4. come on guys cut some slack

5. look it took me a really long time and effort to come up with this code. I know that having 3 star and 4 star programs isn't advised, but is there any way I could still use this code and add the matrices. I have been working on this for a while now and it would be really frustrating if I had to go back and fix the code again. But I trust your expertise and if you really think I should change it, then could you tell me how I should convert my code. the article doesn't really help they're just complaining in that

6. zafy: if you want to be helped, help us help you.

Make your code nice (mind the indentation, horizontal and vertical spacing).
Break your code into smaller functions (searching ~200 lines of code for a problem is very very different than searching ~20 lines of code).

7. We basically did all this last time.
http://cboard.cprogramming.com/c-pro...-question.html

8. Sorry I honestly thought i indented it right this time.
well this is my original code that works and only asks for matrices. The code above just has the switch statements added.
Code:

Code:
```#include<stdio.h>
#include <stdlib.h>

void matrix_display(int ****matrix, int row_count, int column_count);

int main (void)
{
int row_size1,col_size1,row_size2,col_size2,flag =1;
int ****A[4];
int i = 0, j = 0;
char a,c;

do
{
printf("\ncmd> ");
c = getchar();

if(c == 'A')
{
scanf("%d %d",&row_size1,&col_size1);
A[0]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[0][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[0][i][j]);

}
}
matrix_display(A[0], row_size1, col_size1);
}

if(c == 'B')
{
scanf("%d %d",&row_size1,&col_size1);
A[1]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[1][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[1][i][j]);

}
}
matrix_display(A[1], row_size1, col_size1);
}

if(c == 'C')
{
scanf("%d %d",&row_size1,&col_size1);
A[2]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[2][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[2][i][j]);

}
}
matrix_display(A[2], row_size1, col_size1);
}

if(c == 'D')
{
scanf("%d %d",&row_size1,&col_size1);
A[3]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[3][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[3][i][j]);

}
}
matrix_display(A[3], row_size1, col_size1);
}

}
while(flag == 1);
}

void matrix_display(int ****A, int row_size1, int col_size1)
{
int i,j;
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}```

9. yeah now I want to add the matrices A and B and I need some advice on how to do it
I used switch statements like in the code above but I just think I am going in the wrong direction

10. So should I use a switch statement or what?

11. Any sane programmer takes one look at this

Code:
`int ****A[4];`
and says to his or herself: "This is a person may have an inkling of what they're doing, but just started throwing *s and []s around to get the compiler to shut the hell up."

12. Okay fine I'll change the code.
where should i start from?

13. some input guys?

14. You mention 4 matrices A B C D. So two idea's:

1) Using 4 matrices: A, B, C, D.

You have one function which adds two matrices together, and call it twice with the different matrix names.

or

2) Using one large matrix with 4 ranges of indices within it to separate out the 4 divisions you want.
low0 to high0, low1 to high1, low2 to high2, low3 to high3.

In a nested set of loops, that would be easy to code up.