I'm trying to write a program for my C class that tracks deposits made at a bank. It gives you a menu with the options to input a deposit, show the sum of all the deposits, show the deposits from highest to lowest (using a bubble sort), show the average deposit, show the lowest deposit, and then a quit option. The input, the sum, and the quit options are working just fine, as far as I can tell, but the other three options are broken. When you choose them, no matter what inputs you have made to the array, it acts like they're all equal to zero. This is what i have so far:
#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');
}
Bubble sort is not sorting array properly in c
in
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
is unused inside the inner for where you do always the same thing independently of it. Furthermore you go 1 index after the last
There are plenty implementations of bubble sort on S.O. I let you search and correct
swap must be a double
in
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; }
the division must be done after the sums, no each time, so
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;
and avgSum and avg must be double
while (switchInput != 'q'||'Q');
must be
while ((switchInput != 'q') && (switchInput != 'Q'));