通过命令参数的 C 基本排序算法
C Basic Sorting Algorithm through Command Arguments
我在使用下面的代码时遇到了一些问题。
我的目标是对从命令参数 ex 的 int 值创建的数组进行排序,使用参数 Hello 125 17 -6 应该给出:
Unsorted: 0 125 17 -6
Sorted: -6 0 17 125
问题是包含了一个额外的值并被打印为:
未分类:-858993460 0 125 17 -6
排序:-6 0 17 125
我不知道这个值代表什么,也不知道为什么我的 data[] 数组会注册它。
基本分类代码:
void mySort(int d[], unsigned int n) {
int i, j, key;
for (i = 0; i < n; i++) {
key = d[i];
j = i - 1;
while (j >= 0 && d[j] > key) {
d[j + 1] = d[j];
j = j - 1;
}
d[j + 1] = key;
}
}
主要代码:
int main(int argc, char* argv[]) {
int data[1000];
int nDataItems;
int i;
if (argc > 1) {
nDataItems = argc;
fprintf(stderr, "The command line arguments are:\n");
for (i = 1; i < argc; i++) { /* Prints each arg as a string, int and hex */
fprintf(stderr, " argv[%d] (as string): %s\n"
" (as int): %d\n"
" (as int in hex): %X\n\n",
i, argv[i], atoi(argv[i]), atoi(argv[i]));
data[i] = atoi(argv[i]);/* stores integer of each argument value */
}
}else {
fprintf(stderr, "There were no command line arguments.\n");
/* Default Test data */
nDataItems = 4;
data[0] = 10;
data[1] = 20;
data[2] = 30;
data[3] = 40;
}
printf("Unsorted Array: ");
for (i = 0; i < nDataItems; i++) {
printf(" %d ", data[i]);
}
mySort(data, nDataItems);
/* Check that the data array is sorted. */
for (i = 0; i < nDataItems - 1; i++) {
if (data[i] > data[i + 1]) {
fprintf(stderr, "Sort error: data[%d] (= %d)"
" should be <= data[%d] (= %d)- -aborting\n",
i, data[i], i + 1, data[i + 1]);
exit(1); /* exit(1) = exit failure*/
}
}
/* Print sorted array to stdout */
printf("\nSorted Array: ");
if (argc > 1) {
for (i = 1; i < nDataItems; i++) {
printf(" %d ", data[i]);
}
}
else {
for (i = 0; i < nDataItems; i++) {
printf(" %d ", data[i]);
}
}
exit(0); /* exit(0) = exit success*/
}
不胜感激
您应该初始化数据[0]。如果您不赋值,它将取一个随机值。
这一行
data[i] = atoi(argv[i]);/* stores integer of each argument value */
应该是,
data[i - 1] = atoi(argv[i]);/* stores integer of each argument value */
我在使用下面的代码时遇到了一些问题。
我的目标是对从命令参数 ex 的 int 值创建的数组进行排序,使用参数 Hello 125 17 -6 应该给出:
Unsorted: 0 125 17 -6
Sorted: -6 0 17 125
问题是包含了一个额外的值并被打印为: 未分类:-858993460 0 125 17 -6 排序:-6 0 17 125
我不知道这个值代表什么,也不知道为什么我的 data[] 数组会注册它。
基本分类代码:
void mySort(int d[], unsigned int n) {
int i, j, key;
for (i = 0; i < n; i++) {
key = d[i];
j = i - 1;
while (j >= 0 && d[j] > key) {
d[j + 1] = d[j];
j = j - 1;
}
d[j + 1] = key;
}
}
主要代码:
int main(int argc, char* argv[]) {
int data[1000];
int nDataItems;
int i;
if (argc > 1) {
nDataItems = argc;
fprintf(stderr, "The command line arguments are:\n");
for (i = 1; i < argc; i++) { /* Prints each arg as a string, int and hex */
fprintf(stderr, " argv[%d] (as string): %s\n"
" (as int): %d\n"
" (as int in hex): %X\n\n",
i, argv[i], atoi(argv[i]), atoi(argv[i]));
data[i] = atoi(argv[i]);/* stores integer of each argument value */
}
}else {
fprintf(stderr, "There were no command line arguments.\n");
/* Default Test data */
nDataItems = 4;
data[0] = 10;
data[1] = 20;
data[2] = 30;
data[3] = 40;
}
printf("Unsorted Array: ");
for (i = 0; i < nDataItems; i++) {
printf(" %d ", data[i]);
}
mySort(data, nDataItems);
/* Check that the data array is sorted. */
for (i = 0; i < nDataItems - 1; i++) {
if (data[i] > data[i + 1]) {
fprintf(stderr, "Sort error: data[%d] (= %d)"
" should be <= data[%d] (= %d)- -aborting\n",
i, data[i], i + 1, data[i + 1]);
exit(1); /* exit(1) = exit failure*/
}
}
/* Print sorted array to stdout */
printf("\nSorted Array: ");
if (argc > 1) {
for (i = 1; i < nDataItems; i++) {
printf(" %d ", data[i]);
}
}
else {
for (i = 0; i < nDataItems; i++) {
printf(" %d ", data[i]);
}
}
exit(0); /* exit(0) = exit success*/
}
不胜感激
您应该初始化数据[0]。如果您不赋值,它将取一个随机值。
这一行
data[i] = atoi(argv[i]);/* stores integer of each argument value */
应该是,
data[i - 1] = atoi(argv[i]);/* stores integer of each argument value */