首页 > 解决方案 > 冒泡排序未在 c 中正确排序数组

问题描述

我正在尝试为我的 C 类编写一个程序来跟踪在银行进行的存款。它为您提供了一个菜单,其中包含输入存款的选项、显示所有存款的总和、从最高到最低的存款(使用冒泡排序)、显示平均存款、显示最低存款,然后是退出选项. 据我所知,输入、总和和退出选项工作得很好,但其他三个选项都坏了。当你选择它们时,无论你对数组做了什么输入,它的作用就像它们都等于零。这是我到目前为止所拥有的:

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



int main()

{
    int sortCount, sortCount2, sortCount3, swap;// variables for sort
    int depositCount = 0, sumCount, lowestCount;
    int averageCount, avgSum = 0, avg; //variables for average
    char switchInput = 0;//menu input
    double deposits[100] = { 0 }, sum = 0, average;

    do 
    {
        printf("BANKING MAIN MENU\n\n");
        printf("[M]ake a new deposit\n");
        printf("[S]um of all deposits\n");
        printf("[D]eposits will be displayed from highest to lowest\n");
        printf("[A]verage of all deposits\n");
        printf("[L]owest deposit will be displayed\n");
        printf("[Q]uit\n\n");
        printf("Please enter selection:\n\n");
        scanf(" %c", &switchInput);

        switch (switchInput)
        {
        case 'm': case 'M'://Deposit screen

            printf("\nPlease enter deposit:\n\n");
                scanf("%lf", &deposits[depositCount++]);//deposit input
                ;

            for (sortCount = 0; sortCount < depositCount; sortCount++)//Should sort the array highest to lowest
                for (sortCount2 = 0; sortCount2 < depositCount - sortCount - 1; sortCount2++)
                    if (deposits[sortCount] < deposits[sortCount+1])
                    {
                        swap = deposits[sortCount];
                        deposits[sortCount] = deposits[sortCount+1];
                        deposits[sortCount+1] = swap;
                    }

                break;

        case 's': case 'S'://Total of deposits screen

            for (sumCount = 0; sumCount < depositCount; sumCount++)//depositCount should have it only use parts of the array where there are inputs.
                sum = sum + deposits[sumCount];
                printf("\nYour total deposits equal $%.2lf\n\n", sum);

                break;

        case 'd': case 'D'://Highest to lowest screen


            for (sortCount3 = 0; sortCount3 < depositCount; sortCount3++)//depositCount should have it only use parts of the array where there are inputs.
            {
                printf("$%d \n", deposits[sortCount3]);
            }
            break;

        case 'a': case 'A'://Average screen

            for (sumCount = 0; sumCount < depositCount; sumCount++) //depositCount should have it only use parts of the array where there are inputs.
        {
                avgSum = avgSum + deposits[sumCount];
                avg = avgSum / depositCount;
            }
            printf("Your average deposit is $%.2lf.\n", avg);
            break;

        case 'l': case 'L'://Lowest screen

            printf("The lowest deposit is $%.2lf.\n", deposits[depositCount]);//If the numbers are ordered from highest to lowest, the then entry in the array at the position equal to the number of deposits should be the lowest

            break;

        case 'q': case 'Q'://quit screen

            printf("\nThank you for using our bank!\n\n");
            system("pause");

            return 0;
            break;

        default ://invalid option

            printf("\nInvalid selection!\n\n");
        }

    } while (switchInput != 'q'||'Q');

}

标签: carraysswitch-statementaveragebubble-sort

解决方案


冒泡排序未在 c 中正确排序数组

      for (sortCount = 0; sortCount < depositCount; sortCount++)//Should sort the array highest to lowest
           for (sortCount2 = 0; sortCount2 < depositCount - sortCount - 1; sortCount2++)
               if (deposits[sortCount] < deposits[sortCount+1])
               {
                   swap = deposits[sortCount];
                   deposits[sortCount] = deposits[sortCount+1];
                   deposits[sortCount+1] = swap;
               }

sortCount2在内部没有使用因为你总是独立于它做同样的事情。此外,您在最后一个索引之后进入 1 个索引

有很多冒泡排序的实现,所以我让你搜索和更正

交换必须是双重的


case 'a': case 'A'://Average screen
       for (sumCount = 0; sumCount < depositCount; sumCount++) //depositCount should have it only use parts of the array where there are inputs.
   {
           avgSum = avgSum + deposits[sumCount];
           avg = avgSum / depositCount;
       }

除法必须在总和之后进行,不是每次,所以

   case 'a': case 'A'://Average screen
        for (sumCount = 0; sumCount < depositCount; sumCount++) //depositCount should have it only use parts of the array where there are inputs.
        {
            avgSum = avgSum + deposits[sumCount];
        }
         avg = avgSum / depositCount;

并且avgSumavg必须为double


while (switchInput != 'q'||'Q');

一定是

while ((switchInput != 'q') && (switchInput != 'Q'));

推荐阅读