查找数组

时间:2017-10-22 11:02:44

标签: c arrays

Hi Code of Coders / Programmers,

我正在尝试解决混合所有问题的问题,从排序到使用数组,再到找到数组的长度。

我试图找出字符串数组中输入的数字的中位数,然后转换为int数组。偶数数组和奇数数组都有不同的中位数。

在干运行时,我输入数组1,2,3,4,5,6,7,然后按 Enter 。但我得到的结果是0。在调试它时可能出现什么错误,任何未初始化的变量?

感谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1;
#define FALSE 0;

int arrayeven_median(int[], int);
int arrayodd_median(int[], int);
int stringlen(char[]);
int *sort(int[], int);

int num[100];
char numbers[100];
char string[100];
int sortednum[100];

int main()
{
    printf("Enter array of numbers separated by comma\n");

    gets(numbers);

    char *pt;
    /* get the first token */
    pt = strtok(numbers, ",");

    /* walk through other tokens */
    while (pt != NULL)
    {
        int i = 0;
        num[i] = atoi(pt);
        sortednum[i] = *sort(&num[i], stringlen(numbers));
        pt = strtok(NULL, ",");
        i++;
    }

    if (stringlen(numbers) % 2 == 0)
        printf("%d", arrayeven_median(sortednum, stringlen(numbers)));
    else
        printf("%d", arrayodd_median(sortednum, stringlen(numbers)));
    return 0;
}

int stringlen(char string[])
{
    int i;
    for (i = 0; string[i] != '\0'; i++)
        ;
    return i;
}

int *sort(int x[], int n)
{
    int hold, j, pass;
    int switched = TRUE
    ;

    for (pass = 0; pass < n - 1; pass++)
    {
        switched = FALSE
        ;
        for (j = 0; j < n - pass - 1; j++)
            if (x[j] > x[j + 1])
            {
                switched = TRUE
                ;
                hold = x[j];
                x[j] = x[j + 1];
                x[j + 1] = hold;
            }
    }
    return x;
}

int arrayeven_median(int x[], int n)
{
    int m1 = n / 2;
    int m2 = (n / 2) + 1;
    int median = (x[m1] + x[m2]) / 2;
    return median;
}

int arrayodd_median(int x[], int n)
{
    int m1 = (n / 2) + 1;
    int median = x[m1];
    return median;
}

2 个答案:

答案 0 :(得分:4)

您的代码存在问题:

  • 您将转换后的数字存储到数组的第一个元素。 i应在while循环之外初始化。

  • sortednum[i] = *sort(&num[i], stringlen(numbers));毫无意义:stringlen(numbers)以字符计算输入字符串的长度,但此输入字符串被strtok()截断,与{sort无关1}}函数无论如何都需要int数组中的元素数量。您应该将值存储在int数组的末尾,并在存储所有值后对此数组进行排序。

  • #define TRUE 1;不正确:TRUE将扩展为1;而不是1,可能会导致语法错误。

  • 如果数组已排序,sort函数不会使用switched标志从外循环中提前中断。

  • 函数gets()已从C标准中删除,因为它没有给出目标数组的大小,因此无法安全使用。请改用fgets()

  • 数组应该在main函数的本地定义,除非它们非常大并且应该被分配。

  • arrayodd_median()不返回中位数元素。如果n == 1

  • ,它甚至会超出数组的边界
  • 您应该测试无效或不完整的输入。

以下是更正后的版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

int arrayeven_median(int[], int);
int arrayodd_median(int[], int);
void sort(int[], int);

int main(void) {
    char numbers[100];
    int num[100];
    int i;
    char *pt;

    printf("Enter array of numbers separated by comma\n");

    if (!fgets(numbers, sizeof numbers, stdin)) {
        printf("no input\n");
        return 0;
    }

    /* get the first token */
    pt = strtok(numbers, ", \n");
    if (pt == NULL) {
        printf("no numbers\n");
        return 0;
    }

    /* walk through all tokens */
    for (i = 0; i < 100 && pt != NULL; i++) {
        /* convert the token string to an `int` and store at the end of the array */
        num[i] = atoi(pt);
        /* parse the next token */
        pt = strtok(NULL, ", \n");
    }

    /* sort the int array in place */
    sort(num, i);

    if (i % 2 == 0) {
        /* array length is even, find the median value */
        printf("%d\n", arrayeven_median(num, i));
    } else {
        /* array length is odd, find the median element */            
        printf("%d\n", arrayodd_median(num, i));
    }
    return 0;
}

void sort(int x[], int n) {
    int hold, j, pass;

    for (pass = 0; pass < n - 1; pass++) {
        int switched = FALSE;
        for (j = 0; j < n - pass - 1; j++) {
            if (x[j] > x[j + 1]) {
                switched = TRUE;
                hold = x[j];
                x[j] = x[j + 1];
                x[j + 1] = hold;
            }
        }
        if (!switched) {
            break out of the loop if the rest of the array is sorted */
            break;
        }
    }
}

int arrayeven_median(int x[], int n) {
    int m = n / 2;
    return (x[m] + x[m + 1]) / 2;
}

int arrayodd_median(int x[], int n) {
    return x[n / 2];
}

注意:

  • atoi不处理溢出:传递表示非常大数字的字符串可能会导致意外行为。
  • arrayeven_median有一个潜在的算术溢出:在32位系统上,它会为输入2147483647,2147483647生成错误的结果

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

int arrayeven_median(int *, int);
int arrayodd_median(int *, int);
int *sort(int[], int);

int num[100];
char numbers[100];
char string[100];
int index1;

int main()
{
    printf("Enter array of numbers separated by comma\n");

    if (!fgets(numbers, sizeof numbers, stdin)) {
        printf("no input\n");
        return 0;
    }

    char *pt;
    /* get the first token */
    pt = strtok(numbers, ",");
    int i = 0;
    /* walk through other tokens */
    while (pt != NULL)
    {    
        num[i] = atoi(pt);
        pt = strtok(NULL, ",");
        i++;
        if(pt == NULL)
            index1 = i;
    }



    if (index1 % 2 == 0)
        printf("%d", arrayeven_median(sort(num, index1), index1));
    else
        printf("%d", arrayodd_median(sort(num, index1), index1));

    return 0;
}

int *sort(int x[], int n)
{
    int hold, j, pass;
    int switched = TRUE;

    for (pass = 0; pass < n - 1; pass++)
    {
        switched = FALSE;
        for (j = 0; j < n - pass - 1; j++)
            if (x[j] > x[j + 1])
            {
                switched = TRUE;
                hold = x[j];
                x[j] = x[j + 1];
                x[j + 1] = hold;
            }
    }

    return x;
}

int arrayeven_median(int *x, int n)
{
    int m1 = n / 2;
    int m2 = (n / 2) + 1;
    printf("%d\n", m1);
    int median = (x[m1-1] + x[m2-1]) / 2;
    return median;
}

int arrayodd_median(int *x, int n)
{
    int m1 = (n/2) + 1;
    int median = x[m1-1];
    return median;
}