I'm new to programming and we just learned arrays in class.
We are tasked to create a C program without using global variables.
I created two functions, one for inputting the data and one for the operations (containing a menu). After making the user choose which operation he would like to do in the operations menu, it will display the result and return back to the menu.
I couldn't figure out how to make some of the variables readable by the operations function since we are not allowed to use global variables.
void matrix(){
int a, b, c, d, k, m, n, p, q, s=0, first[MAX][MAX], second[MAX][MAX], msum[MAX][MAX], firstt[MAX][MAX], secondt[MAX][MAX], prod[MAX][MAX];
system("CLS");
printf("/-----------------------------------------------------------------------------/\n"
"\t\t\t\tMatrix\n"
"/-----------------------------------------------------------------------------/\n");
printf("This program will multiply matrices (up to 3x3 matrix only).\n"
"Please enter the number of rows of the first matrix: ");
scanf("%d", &m);
if(m>3){
matrixerror();
}
printf("Please enter then number of columns of the first matrix: ");
scanf("%d", &n);
if(n>3){
matrixerror();
}
printf("Please enter the number of rows of the second matrix: ");//Matrix 2
scanf("%d", &p);
if(p>3){
matrixerror();
}
printf("Please enter then number of columns of the second matrix: ");
scanf("%d", &q);
if(q>3){
matrixerror();
}
printf("\nPlease enter the elements of the first matrix:\n");
for(c=0; c<m; ++c)
for(d=0; d<n; ++d){
printf("Enter element a%d%d: ",c+1, d+1);
scanf("%d", &first[c][d]);
}
printf("\nPlease enter the elements of the second matrix:\n");
for(c=0; c<p; ++c)
for(d=0; d<q; ++d){
printf("Enter element b%d%d: ",c+1, d+1);
scanf("%d", &second[c][d]);
}
matrixmenu();
}
and the other one for the operations
void matrixmenu(){
system("CLS");
char choice;
printf("/-----------------------------------------------------------------------------/\n"
"\t\t\t\tMatrix\n"
"/-----------------------------------------------------------------------------/\n");
printf("\n"
"\t1. Add Matrices\n"
"\t2. Multiply Matrices\n"
"\t3. Transpose \n"
"\tB. Back \n");
printf("\n\tFirst matrix is : \n\t");
for(a=0; a<m; ++a)
for(b=0; b<n; ++b){
printf("%d ", first[a][b]);
if (b == n-1)
printf("\n\n\t");
}
printf("\n\tSecond matrix is : \n\t");
for(a=0; a<m; ++a)
for(b=0; b<n; ++b){
printf("%d ", second[a][b]);
if (b == n-1)
printf("\n\n\t");
}
printf("\n");
printf("/------------------------------------------------------------------------------/ ");
scanf("%s", &choice);
switch(choice){
case '1':
printf("\n\tThe sum of entered matrices is: \n\t");
for (a = 0; a < m; a++){
for (b = 0 ; b < n; b++){
msum[a][b] = first[a][b] + second[a][b];
printf("%d\t", msum[a][b]);
}
printf("\n\t");
}
printf("\n\t");
system("PAUSE");
matrixmenu();
break;
case '2':
if (n != p){
printf("\n\tError! Matrix cannot be multiplied!\n\t");
system("PAUSE");
matrixmenu();
}
for (c = 0; c < m; c++){
for (d = 0; d < q; d++){
for (k = 0; k < p; k++){
s = s + first[c][k]*second[k][d];
}
prod[c][d] = s;
s = 0;
}
}
printf("\n\tThe product matrix is:\n\t");
for (c = 0; c < m; c++){
for (d = 0; d < q; d++){
printf("%d\t", prod[c][d]);
}
printf("\n\t");
}
printf("\n\t");
system("PAUSE");
matrixmenu();
break;
case '3':
for(a=0; a<m; ++a)//Tranposition
for(b=0; b<n; ++b)
firstt[b][a] = first[a][b];
printf("\n\tThe transpose of the first matrix is:\n\t");
for(a=0; a<n; ++a)
for(b=0; b<m; ++b){
printf("%d ",firstt[a][b]);
if(b==m-1)
used printf("\n\n\t");
}
for(a=0; a<p; ++a)//Tranposition
for(b=0; b<q; ++b)
secondt[b][a] = second[a][b];
printf("\n\tThe transpose of the second matrix is:\n\t");
for(a=0; a<n; ++a)
for(b=0; b<m; ++b){
printf("%d ",secondt[a][b]);
if(b==m-1)
printf("\n\n\t");
}
printf("\n\t");
system("PAUSE");
matrixmenu();
break;
case 'B':
case 'b':
mainmenu();
break;
default:
matrixmenu();
break;
}
}