【发布时间】:2017-06-09 04:33:47
【问题描述】:
我是编程新手,我们刚刚在课堂上学习了数组。
我们的任务是创建一个不使用全局变量的 C 程序。
我创建了两个函数,一个用于输入数据,一个用于操作(包含一个菜单)。让用户在操作菜单中选择想要进行的操作后,会显示结果并返回菜单。
由于不允许使用全局变量,我无法弄清楚如何使操作函数可以读取某些变量。
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();
}
另一个用于操作
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;
}
}
【问题讨论】:
-
将他们的地址传递给您调用的函数。然后它可以引用它们。
-
并考虑在您的代码中添加一些空白行。你会发现阅读一本没有段落的书就像大多数人(包括你自己在内)会找到你的代码一样困难。
-
你是怎么做到的?
-
@WilyFreddie 您要访问的代码在哪里?可以评论指定吗!
-
@CoolVirus 我想从 matrix() 访问 m、n、p、q、first[MAX][MAX] 和 second[MAX]
标签: c arrays parameter-passing