c - 冒泡排序未在 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');
}
解决方案
冒泡排序未在 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;
并且avgSum和avg必须为double
while (switchInput != 'q'||'Q');
一定是
while ((switchInput != 'q') && (switchInput != 'Q'));
推荐阅读
- android - 在 android studio 中更改名称 AVD
- node.js - Gulp 我需要 /dist 来拥有 node_modules 和 .git
- php - 如何使用 PHP 仅指定特定网页的用户帐户
- laravel - 作曲家更新失败
- r - 动态创建的辅助表中的闪亮观察事件
- php - PrestaShop 在 web 服务中返回 302
- html - 如何更改活动导航器的背景颜色
- c++ - 如何使用 std::string 作为 QHash 的键?
- python - Python字数(2个包含单词的文件)(1个用于字数的文件)(最后一个文件写在他里面字+计数)
- c# - 如何将登录密码保存到 xamarin 中的钥匙串