C 特定数组的大小
我有一个算法,它应该显示两个数组是否相似。它可以工作,但我不知道数组的大小应该是多少 例如:C 特定数组的大小,c,arrays,C,Arrays,我有一个算法,它应该显示两个数组是否相似。它可以工作,但我不知道数组的大小应该是多少 例如: int a[10], i = 0, r = 0, n = 0; printf("Enter the amount of numbers in arrays: "; scanf("%d", &n); printf("Enter the numbers of array: "; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } i
int a[10], i = 0, r = 0, n = 0;
printf("Enter the amount of numbers in arrays: ";
scanf("%d", &n);
printf("Enter the numbers of array: ";
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
inta[10],i=0,r=0,n=0;
printf(“在数组中输入数字的数量:”;
scanf(“%d”和“&n”);
printf(“输入数组的编号:”;
对于(i=0;i
如果输入“n”变量n=11,程序将在末尾停止。我的问题是:我应该在数组a[中输入什么数字,以确保该程序与大多数硬件兼容(我听说这也取决于内存)
@更新1:
我选择了一个alk的解决方案,但它仍然不起作用。以下是我的代码:
int main()
{
int i = 0, temp_a = 0, switch_a = 0, temp_b = 0, switch_b = 0, n = 0;
printf("Enter the amount of numbers in arrays: ");
scanf("%d", &n);
{
int a[n], b[n];
printf("Enter elements of first array: ");
for (i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
}
printf("Enter elements of second array: ");
for (i = 0; i < n; ++i)
{
scanf("%d", &b[i]);
}
do
{
switch_a = 0;
for (i = 0; i < n - 1; i++)
{
if (a[i] > a[i + 1])
{
switch_a = switch_a + 1;
temp_a = a[i];
a[i] = a[i + 1];
a[i + 1] = temp_a;
}
}
} while (switch_a != 0);
//bubble sort
do
{
switch_b = 0;
for (i = 0; i < n - 1; i++)
{
if (b[i] > b[i + 1])
{
switch_b = switch_b + 1;
temp_b = b[i];
b[i] = b[i + 1];
b[i + 1] = temp_b;
}
}
} while (switch_b != 0);
//Cheks, if an arrays are the same.
for (i = 0; i < n; i++)
{
if (a[i] != b[i])
{
printf("This two arrays don't have the same elements.\n\n\n");
return 0;
}
}
printf("This two arrays have the same elements.\n\n\n");
}
return 0;
intmain()
{
int i=0,temp_a=0,switch_a=0,temp_b=0,switch_b=0,n=0;
printf(“在数组中输入数字的数量:”);
scanf(“%d”和“&n”);
{
int a[n],b[n];
printf(“输入第一个数组的元素:”);
对于(i=0;ia[i+1])
{
开关a=开关a+1;
温度a=a[i];
a[i]=a[i+1];
a[i+1]=临时工;
}
}
}while(开关a!=0);
//气泡排序
做
{
开关_b=0;
对于(i=0;ib[i+1])
{
开关_b=开关_b+1;
温度b=b[i];
b[i]=b[i+1];
b[i+1]=温度;
}
}
}while(开关_b!=0);
//Cheks,如果数组相同。
对于(i=0;i
}
你能帮我检查一下吗?我找不到有什么问题…你的数组
a
只能包含10个int
。因此,如果你将n
输入为11,那么你就有了越界访问权限
您可以使用C99(可变长度数组)或使用malloc()
系列函数使用动态内存分配
注意:VLA在C11中是可选的。因此,您可能需要使用宏\uu STDC\u NO\u VLA\uu
检查您的实现是否不支持它
VLA(具有自动存储持续时间——通常称为“堆栈”)的一个潜在问题是,很难判断大型阵列的分配是否成功。您的阵列
A
只能包含10int
。因此,如果您将n
输入为11,则您将有一个超出边界的访问权限
您可以使用C99(可变长度数组)或使用malloc()
系列函数使用动态内存分配
注意:VLA在C11中是可选的。因此,您可能需要使用宏\uu STDC\u NO\u VLA\uu
检查您的实现是否不支持它
VLA(具有自动存储持续时间——通常称为“堆栈”)的一个潜在问题是,很难确定大型阵列的分配是否成功。如果使用的C实现支持可变长度阵列,则只需执行以下操作:
int i = 0, r = 0, n = 0;
printf("Enter the amount of numbers in arrays: ";
scanf("%d", &n);
{
int a[n];
printf("Enter the numbers of array: ";
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
...
inti=0,r=0,n=0;
printf(“在数组中输入数字的数量:”;
scanf(“%d”和“&n”);
{
int a[n];
printf(“输入数组的编号:”;
对于(i=0;i
如果您使用的C实现支持可变长度数组,那么只需执行以下操作:
int i = 0, r = 0, n = 0;
printf("Enter the amount of numbers in arrays: ";
scanf("%d", &n);
{
int a[n];
printf("Enter the numbers of array: ";
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
...
inti=0,r=0,n=0;
printf(“在数组中输入数字的数量:”;
scanf(“%d”和“&n”);
{
int a[n];
printf(“输入数组的编号:”;
对于(i=0;i
如果您不能(或不想)使用VLA,请使用malloc:
例如:
int *buffer;
int dim;
printf("Enter the amount of numbers in arrays: ");
scanf("%d", &dim);
buffer = (int*) malloc(sizeof(int) * dim);
if (buffer == NULL) {
perror("Malloc error");
exit(-1);
}
有关malloc的更多信息,请点击此处:如果您不能(或不想)使用VLA,请使用malloc:
例如:
int *buffer;
int dim;
printf("Enter the amount of numbers in arrays: ");
scanf("%d", &dim);
buffer = (int*) malloc(sizeof(int) * dim);
if (buffer == NULL) {
perror("Malloc error");
exit(-1);
}
关于malloc的更多信息:您需要一个可变长度数组,幸运的是C99以后的版本都有。在声明
int a[n]之前,请先读入scanf行需要多少元素;
“…此程序将与大多数硬件兼容”-程序的编写是否应该适合它要解决的问题?如果添加#include
,更新的代码似乎可以工作。问题是什么?此外,这听起来像是一个新问题,与旧问题无关。如果你能识别问题,可能会发布一个新问题?很有效!谢谢大家太多了!我很高兴。:)Stackoverflow-我的位置:)你想要一个可变长度的数组,幸运的是C99以后已经有了。在声明int a[n];
“…这个程序将与大多数硬件兼容。”-程序的编写是否应该适合它要解决的问题?如果添加#include
,更新的代码似乎可以工作。问题是什么?此外,这听起来像是一个新问题,与旧问题无关。如果你能识别问题,可能会发布一个新问题?很有效!谢谢大家太多了!我太高兴了。:)Stackoverflow-我的地方:)