for循环中的赋值问题
Problem with value assignment inside a for loop
我的代码中有一个愚蠢的错误。我将不胜感激任何帮助。我知道这是特定于语言 (C) 的(因为我对 Python 和 JS 有一些经验)。
我试图将源数组分成两部分。
当我尝试将源数组的当前索引的值分配给另一个数组时,它工作正常但只能到 for 循环结束。循环完成后,这些数组的值完全不同。
请在下面查看我的代码:
#include <stdio.h>
//#include <cs50.h>
int merge_sort(int unsorted_array[], int size_of_arr);
int main(void)
{
int unar[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; // unsorted array
int len_of_arr = sizeof(unar) / sizeof(int); // finding lenght of array
merge_sort(unar, len_of_arr);
}
int merge_sort(int unsorted_array[], int size_of_arr)
{
// dividing
int ls_len; // lenght of left side of an array
int rs_len; // lenght of right side of an array
if (size_of_arr % 2 == 0) // if lenght of an array is even then lenght of each half equals: lenght of arr / 2
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2;
}
else // else lenght of arr / 2 and right half++
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2 + 1;
}
int unsorted_ls[ls_len]; // unsorted_array_length // 2
int unsorted_rs[rs_len]; // if array_len is odd ? array_len / 2 + 1 : array_len / 2
printf("left half len: %i\nRight half len: %i\n", ls_len, rs_len);
for (int i = 0; i < size_of_arr; i++)
{
if (i < ls_len)
{
unsorted_ls[i] = unsorted_array[i];
printf("Current value of original array: %i\n", unsorted_array[i]);
printf("Currently assigned value of left half: %i\n\n", unsorted_ls[i]); // current
} else
{
unsorted_rs[i] = unsorted_array[i];
printf("Current value of original array: %i\n", unsorted_rs[i]);
printf("Currently assigned value of right half: %i\n\n", unsorted_array[i]);
}
}
printf("Left half outside of for loop: ");
for (int i = 0; i < ls_len; i++)
{
printf("%i", unsorted_ls[i]);
}
printf("\n");
printf("Right half outside of for loop: ");
for (int i = 0; i < rs_len; i++)
{
printf("%i", unsorted_rs[i]);
}
printf("\n");
// sorting // not implemented yet
// int sorted_left_side[] = merge_sort(unsorted_left_half);
// int sorted_right_side[] = merge_sort(unsorted_right_half);
// merging // not implemented yet
// // for (int i = 0; )
// return sorted_array;
return 0;
}
这个赋值语句
unsorted_rs[i] = unsorted_array[i];
调用未定义的行为,因为当索引 i
的值大于或等于 ls_len
.[=17 时,试图访问数组 unsorted_rs
之外的内存=]
你应该写
unsorted_rs[i - ls_len] = unsorted_array[i];
注意那个而不是这个 if-else 语句
if (size_of_arr % 2 == 0) // if lenght of an array is even then lenght of each half equals: lenght of arr / 2
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2;
}
else // else lenght of arr / 2 and right half++
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2 + 1;
}
你可以写
ls_len = size_of_arr / 2;
rs_len = size_of_arr - ls_len;
我的代码中有一个愚蠢的错误。我将不胜感激任何帮助。我知道这是特定于语言 (C) 的(因为我对 Python 和 JS 有一些经验)。 我试图将源数组分成两部分。 当我尝试将源数组的当前索引的值分配给另一个数组时,它工作正常但只能到 for 循环结束。循环完成后,这些数组的值完全不同。 请在下面查看我的代码:
#include <stdio.h>
//#include <cs50.h>
int merge_sort(int unsorted_array[], int size_of_arr);
int main(void)
{
int unar[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; // unsorted array
int len_of_arr = sizeof(unar) / sizeof(int); // finding lenght of array
merge_sort(unar, len_of_arr);
}
int merge_sort(int unsorted_array[], int size_of_arr)
{
// dividing
int ls_len; // lenght of left side of an array
int rs_len; // lenght of right side of an array
if (size_of_arr % 2 == 0) // if lenght of an array is even then lenght of each half equals: lenght of arr / 2
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2;
}
else // else lenght of arr / 2 and right half++
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2 + 1;
}
int unsorted_ls[ls_len]; // unsorted_array_length // 2
int unsorted_rs[rs_len]; // if array_len is odd ? array_len / 2 + 1 : array_len / 2
printf("left half len: %i\nRight half len: %i\n", ls_len, rs_len);
for (int i = 0; i < size_of_arr; i++)
{
if (i < ls_len)
{
unsorted_ls[i] = unsorted_array[i];
printf("Current value of original array: %i\n", unsorted_array[i]);
printf("Currently assigned value of left half: %i\n\n", unsorted_ls[i]); // current
} else
{
unsorted_rs[i] = unsorted_array[i];
printf("Current value of original array: %i\n", unsorted_rs[i]);
printf("Currently assigned value of right half: %i\n\n", unsorted_array[i]);
}
}
printf("Left half outside of for loop: ");
for (int i = 0; i < ls_len; i++)
{
printf("%i", unsorted_ls[i]);
}
printf("\n");
printf("Right half outside of for loop: ");
for (int i = 0; i < rs_len; i++)
{
printf("%i", unsorted_rs[i]);
}
printf("\n");
// sorting // not implemented yet
// int sorted_left_side[] = merge_sort(unsorted_left_half);
// int sorted_right_side[] = merge_sort(unsorted_right_half);
// merging // not implemented yet
// // for (int i = 0; )
// return sorted_array;
return 0;
}
这个赋值语句
unsorted_rs[i] = unsorted_array[i];
调用未定义的行为,因为当索引 i
的值大于或等于 ls_len
.[=17 时,试图访问数组 unsorted_rs
之外的内存=]
你应该写
unsorted_rs[i - ls_len] = unsorted_array[i];
注意那个而不是这个 if-else 语句
if (size_of_arr % 2 == 0) // if lenght of an array is even then lenght of each half equals: lenght of arr / 2
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2;
}
else // else lenght of arr / 2 and right half++
{
ls_len = size_of_arr / 2;
rs_len = size_of_arr / 2 + 1;
}
你可以写
ls_len = size_of_arr / 2;
rs_len = size_of_arr - ls_len;