Using an array to hold temporary information

I am taking a programming C++ class for my IT degree requirement, Most of what I have learned I have taught myself. I am really struggling with the final project he is having us do. I really don't understand why I am having such a problem with this it seems so simple.

The goal of this project is to add to the first project the following functionality:
1. The user will be allowed to enter several values.
2. From all the values entered, you will present at the end of your program the following information:
a. Your minimal savings is:
b. Your maximal savings is:
c. Your average savings is:
Note: You MUST use an array structure to hold the information temporarily.

I feel like I am very close to an answer here. I just can't get the sort to work correctly.

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

//function prototype
void sortSavings(double savings[10]);

int main()
{
//declare variables
double A = 0.0; //accumulated savings plan balance
double PMT = 0.0; //regular payment (deposit) amount
double APR = 0.0; //annual percentage rate (as a decimal)
double n = 12.0; //number of payment periods per year (monthly deposits - 12 per year)
double Y = 0.0; //number of years
double savings[10] = {0.0}; //array for storage of PMT
char yesNo = 'Y'; //calculate again (Y/y or N/n)?
int i = 0; //savings position
double AVG = 0.0; //average of max and min

while (toupper(yesNo) == 'Y') //begin loop
{
cout << "Investment Strategy Module" << endl; //Display the title

//enter input items
cout << "Enter years until retirement: "; cin >> Y;
cout << "Enter estimated annual average interest(%): "; cin >> APR;
cout << "Enter your retirement goal: $"; cin >> A;

//convert percentage to decimal
APR = APR / 100;

//Calculate the monthly contribution
PMT = (A * (APR / n)) / (pow((1 + APR / n),(n * Y)) - 1);

//display monthly contribution and input value into array
cout << fixed << setprecision(2);
cout << "To reach your goal you need to save each month: $" << PMT << endl;

//input PMT into array savings
savings[i] = PMT;
i += 1; //increment i

//Determine if user wants to repeat
cout << "Do you want to calculate again? (Y/y or N/n) "; cin >> yesNo;
system ("cls");
} //end while

//function call
sortSavings(savings);

//calculate the average PMT
AVG = (savings[0] + savings[i - 1]) / 2;

//display output items
cout << "\n\nYour minimal savings is: $" << savings[0] << endl;
cout << "\nYour maximal savings is: $" << savings[i - 1] << endl;
cout << "\nYour average savings is: $" << AVG << endl;

system("pause");
return 0;
} //end of main function

//*****function definitions*****
void sortSavings(double numbers[10])
{
//declare variables
int sub = 0.0; //keeps track of subscripts
double temp = 0.0; //variable used for swapping
int maxSub = 0.0; //maximum subscript
int lastSwap = 0.0; //position of last swap
char swap = 'Y'; //indicates if a swap was made

//repeat loop instructions as long as a swap was made
while(swap == 'Y')
{
swap = 'N'; //assume no swaps are necessary

sub = 0; //begin comparing with first element
//array element

//compare adjacent array elements to determine whether a swap is necessary
while (sub < maxSub)
{
if (numbers[sub] > numbers[sub + 1])
{
//a swap is necessary
temp = numbers[sub];
numbers[sub] = numbers[sub + 1];
numbers[sub + 1] = temp;
swap = 'Y';
lastSwap = sub;
} //end if
sub += 1; //increment subscript
} //end while
maxSub = lastSwap; //reset maximum subscript
} //end while
} //end of sortSavings function


I can't figure this out and I don't know what i'm doing wrong. If someone could please just point me in the right direction. I honestly don't know what i'm doing wrong and I really need to get this done by Monday 4/29/2013.

Can someone please help?
Thank you so much if you do.
Last edited on
Topic archived. No new replies allowed.