1. Engineering
  2. Computer Science
  3. this c program works mostly the way i need it...

Question: this c program works mostly the way i need it...

Question details

This C program works mostly the way I need it to with a few exceptions. The deposits currently display lowest to highest but I need them to display highest to lowest instead. The function to determine the average also gives me trouble from time to time and the default statement (Invalid entry...) doesn't display properly. Any assistance would be appreciated.

#include<stdio.h>

// Swap function using call by reference

int swap(float *a, float *b) {

float temp;

temp = *a;

*a = *b;

*b = temp;

}

// Bubble sort

int bubble_sort(float a[], int n) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (a[j] > a[j + 1]) {

swap(&a[j], &a[j + 1]);

}

}

}

}

// Function to print array entries

int print(float a[], int n) {

int i;

printf(" Deposits are: ");

for (i = 0; i < n; i++)

{

printf(" %.2f ", a[i]);

}

}

// Function to find minimum

float findMin(float a[], int n) {

int i;

float min = a[0];

for (i = 1; i < n; i++) {

if (min > a[i])

min = a[i];

}

return(min);

}

//function to find average

float findAvg(float a[], int n) {

int i;

float sum = 0;

for (i = 0; i < n; i++) {

sum += a[i];

}

return(sum / n);

}

//function to find sum

float findSum(float a[], int n) {

int i;

float sum = 0;

for (i = 0; i < n; i++) {

sum += a[i];

}

return(sum);

}

//main driver function

int main() {

float deposit[100], min, avg, sum;

int n = 0, flag = 1;

char op;

while (flag) {

printf(" \n***BANKING MAIN MENU*** ");

printf("\n [M]ake new deposit\n ");

printf(" [S]um of all deposits\n ");

printf(" [D]eposits displayed highest to lowest\n ");

printf(" [A]verage of all deposits\n ");

printf(" [L]owest deposit displayed\n ");

printf(" [Q]uit the program\n ");

printf("\nPlease enter selection: ");

scanf_s(" %c", &op);

switch (op)

{

case('M'): case('m'):

{

printf(" Enter amount: ");

scanf_s("%f", &deposit[n]);

n++;

break;

}

case('S'): case('s'):

{

sum = findSum(deposit, n);

printf(" Sum of deposits: %.2f\n", sum);

break;

}

case('D'): case('d'):

{

bubble_sort(deposit, n);

print(deposit, n);

break;

}

case('A'): case('a'):

{

avg = findAvg(deposit, n);

printf(" Average deposit: %.2f\n", avg);

break;

}

case('L'): case('l'):

{

min = findMin(deposit, n);

printf(" Minimum deposit: %.2f\n", min);

break;

}

case('Q'): case('q'):

{

flag = 0;

break;

}

default:

printf(" Invalid entry. Please make another selection.\n");

}

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution