c程序中需要的常量表达式
Constant Expression Required in c Program
下面的代码显示错误“需要常量表达式”。我尝试了一些解决方案,但仍然无法解决。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int coins( int S[], int m, int n ) {
int i, j, x, y;
int table[n+1][m];
for (i=0; i<m; i++)
table[0][i] = 1;
for (i = 1; i < n+1; i++) {
for (j = 0; j < m; j++) {
x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
y = (j >= 1)? table[i][j-1]: 0;
table[i][j] = x + y;
}
}
return table[n][m-1];
}
int main() {
int arr[20],n;
int m,i;
printf("********* MAKING CHANGE PROBLEM *********");
printf("\nEnter size of array:");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",arr[i]);
}
m = sizeof(arr)/sizeof(arr[0]);
printf("The total number of combinations of coins that sum up to %d",n);
printf(" is %d ", coins(arr, m, n));
getch();
return 0;
}
根据不同版本的C语言标准,不允许出现以下错误,大部分错误都出自这里
int table[n+1][m];
改为使用#define NSIZE 10
和#define MSIZE 20
和int table[NSIZE+1][MSIZE];
等等
和scanf("%d",arr[i]);
会导致问题使用scanf("%d",&arr[i]);
下面的代码显示错误“需要常量表达式”。我尝试了一些解决方案,但仍然无法解决。
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int coins( int S[], int m, int n ) {
int i, j, x, y;
int table[n+1][m];
for (i=0; i<m; i++)
table[0][i] = 1;
for (i = 1; i < n+1; i++) {
for (j = 0; j < m; j++) {
x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;
y = (j >= 1)? table[i][j-1]: 0;
table[i][j] = x + y;
}
}
return table[n][m-1];
}
int main() {
int arr[20],n;
int m,i;
printf("********* MAKING CHANGE PROBLEM *********");
printf("\nEnter size of array:");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",arr[i]);
}
m = sizeof(arr)/sizeof(arr[0]);
printf("The total number of combinations of coins that sum up to %d",n);
printf(" is %d ", coins(arr, m, n));
getch();
return 0;
}
根据不同版本的C语言标准,不允许出现以下错误,大部分错误都出自这里
int table[n+1][m];
改为使用#define NSIZE 10
和#define MSIZE 20
和int table[NSIZE+1][MSIZE];
等等
和scanf("%d",arr[i]);
会导致问题使用scanf("%d",&arr[i]);