gpt4 book ai didi

c - C 中未调用函数

转载 作者:行者123 更新时间:2023-11-30 14:49:21 25 4
gpt4 key购买 nike

嗨,我正在尝试使用函数按升序输出数组的内容,但该函数无法正确地处理我的特定结果,特别是在冒泡排序函数中有人知道为什么吗?

  • 我花了好几个小时试图解决这个问题,所以如果能得到一些强调就太好了。底部的注释是针对降序输出的。 current output of Ascending Function

    int main() 
    {
    int arrayHeight, array[100][2], xCoord, yCoord, i;
    printf ("***** Bubble Sort ***** \n");
    printf("How many items of data do you wish to enter? ");
    scanf("%d",&arrayHeight);
    for(i=0; i<arrayHeight; i++)
    {
    printf("Please enter in the X coordinate: ");
    scanf("%d", &xCoord);
    printf("Please enter in the Y coordinate: ");
    scanf("%d", &yCoord);
    array[i][0] = xCoord;
    array[i][1] = yCoord;
    }
    DisplayArray(array,arrayHeight);
    Bubblesort (array, arrayHeight);
    }

    int DisplayArray(int array[100][2],int arrayHeight, int swap)
    {
    /*Displaying Array elements*/
    int i, j;
    printf("\n The 2-D Array contains : \n");
    for(i=0; i<arrayHeight; i++)
    {
    printf("[%d][%d]\n\r", array[i][0], array[i][1]);
    }
    }

    int BubbleSort(int array[100][2], int arrayHeight) /*Start of the Function Usage*/
    {
    /*Sorts the Array elements into appropriate chosen sorting order - Ascending*/
    int swap, i, j;
    /*for(k =0; k< arrayHeight; k++) {*/
    for (i = 0; i <arrayHeight ; i++) {
    for (j = i+1; j < 3; ++j) {
    if (array[i][0] > array[i][1]) {
    swap = array[i][0];
    array[i][0] = array[i][1];
    array[i][1] = swap;
    }
    }
    }
    ("\n Printing in Asending Order: ");
    for (i=0; i<arrayHeight; i++)
    {
    for (j=0; j<arrayHeight; j++)
    {
    printf("\n %d ", swap);
    }
    }
    BubbleSort(array, swap);
    }
    for(k =0; k< 2; k++) {/* These lines of code sort out the stored array
    number into the appropriate order - Descending
    for (i = 0; i < 3; i++) {
    for (j = i+1; j < 3; ++j) {
    if (a[k][j] > a[k][i]) {
    int swap = a[k][j];
    a[k][j] = a[k][i];
    a[k][i] = swap;
    }
    }
    }
    }
    printf("\n Printing in Descending Order: ");
    for (j=0; j<2; j++)
    {
    for (i=0; i<3; i++)
    {
    printf("\n %d ", a[j][i]);
    if (i==2)
    {
    printf("\n");
    }
    }
    }

最佳答案

  1. 所有声明函数的原型(prototype)或者将函数写在main上面

  2. 首先调用 Bubblesort,然后调用 DisplayArray 函数

  3. 我认为程序的逻辑在这里被破坏了

    enter code here
    //this is wrong code logically !!!!!!!!!
    for (i = 0; i <arrayHeight ; i++) {
    for (j = i+1; j < 3; ++j) {
    if (array[i][0] > array[i][1]) {
    swap = array[i][0];
    array[i][0] = array[i][1];
    array[i][1] = swap;
    }
    }

    }

使用 j 完成循环,但它没有在程序中的任何地方使用,我认为这是一个错误

关于c - C 中未调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49635521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com