![](/img/trans.png)
[英]I was trying to run a simple code of c in VS ,but this shows,what to do?
[英]When I run this code on VScode, it shows “test.c has stopped working”. I was trying to Practice merge sort
C/C++
我正在使用 VScode。 当我运行这个程序文件时立即停止。
我以前遇到过这种问题。 但它发生了,因为我扫描了一个没有“&”寻址的数组。
但这次我找不到问题。
这个 function 将打印一个数组。
#include <stdio.h>
void print_array ( int size, int data[], char *str) // this function will print array.
{
int i;
printf("%s", str);
for (i = 0; i < size; i++)
{
printf("%d\t", data[i]);
}
}
这个 function 将采用两个已排序的 arrays,对它们进行合并排序,它将给出一个排序数组。
void merge(int a[], int b[], int c[], int x, int y) //this function will merge-sort two sorted arrays.
{
int i = 0, j = 0, k = 0;
while ( i < x && j < y)
{
if (a[i] < b[j])
{
c[k++] = a[i++];
}
else
c[k++] = b[j++];
}
while (i < x)
{
c[k++] = a[i++];
}
while(j < y)
{
c[k++] = b[j++];
}
}
这个主要的 function 将获取数组的元素,并执行上述函数中定义的所有任务
我认为问题出在int main() function 中。 因为问题根本没有开始运行。
int main (void)
{
int x, y, i, j;
int a[x];
int b[y];
int c[x+y];
printf("Enter size of first array: ");
scanf("%d", &x);
printf("\nEnter sorted elements of first array:\n");
for(i = 0; i < x; i++)
{
scanf("%d", &a[i]); //it will take elements of array.
}
print_array( x, a, "\nFirst array\n"); //print the first array.
printf("\n\n");
printf("Enter size of second array: ");
scanf("%d", &y);
printf("\nEnter sorted elements of Second array:\n");
for(j = 0; j < y; j++)
{
scanf("%d", &b[i]);
}
print_array( y, b, "\nSecond array\n"); //print second array.
printf("\n\n");
merge(a, b, c, x, y); /merge sort
print_array( x+y, c, "\nSorted Data\n"); //print sorted data
printf("\n\n");
return 0;
}
您实际上要实施什么? 如果您尝试实现合并排序算法,这不是实现合并排序的方法。
你的问题很模糊。 您没有指定是否遇到运行时错误或编译时错误。
如果您正确复制了代码,我认为您会收到编译错误,因为缺少注释/
。 修正评论。 编辑行merge(a, b, c, x, y); /merge sort
merge(a, b, c, x, y); /merge sort
merge(a, b, c, x, y); //merge sort
merge(a, b, c, x, y); //merge sort
您还可能会遇到运行时错误或未定义的行为,因为您声明了三个数组int a[x]
, int b[y]
& int c[x+y];
没有初始化x
& y
的值。 一种可能的解决方案是初始化x
& y
的值。
int main(void) {
int x, y, i, j;
printf("Enter size of first array: ");
scanf("%d", &x);
//declare array a[x] here
int a[x];
printf("\nEnter sorted elements of first array:\n");
for (i = 0; i < x; i++) {
scanf("%d", &a[i]); //it will take elements of array.
}
print_array(x, a, "\nFirst array\n"); //print the first array.
printf("\n\n");
printf("Enter size of second array: ");
scanf("%d", &y);
//declare array b[y] here
int b[y];
printf("\nEnter sorted elements of Second array:\n");
for (j = 0; j < y; j++) {
scanf("%d", &b[i]);
}
print_array(y, b, "\nSecond array\n"); //print second array.
printf("\n\n");
//declare array c[x+y] here
int c[x + y];
merge(a, b, c, x, y); //merge sort
print_array(x + y, c, "\nSorted Data\n"); //print sorted data
printf("\n\n");
return 0;
}
最后一个问题的另一个解决方案是使用宏MAX
声明数组,假设x
和y
的值小于MAX
。 注意:这个过程是 mot memory 高效的。
#define MAX 100000
int main(void) {
int x, y, i, j;
int a[MAX];
int b[MAX];
int c[MAX + MAX];
.....
.....
.....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.