Hello guys, so I need some help with these calculations. so I have a 2d int array that has dates and prices(of mutual funds) stored as int, and I need to calculate the percent gain YTD. just to give you guys the quick rundown The percent gain YTD (year to date) represents the gain since the last date of the previous year (2016). The gain (YTD) is calculated by subtracting the last close of the last year from the current close. The percent gain (YTD) is then calculated by dividing the gain (YTD) by the last close of the last year and expressing that result as a percent.so now we know how to calculate it right, however my problem is that im not sure how to write a function that calculates that without just printing my array and finding what row the last years close is located and starting like data[267][0]... and the problem with that is my program is going to be tested with a updated mutual funds file so if last years close isnt on the same spot then it would calculate the YTD wrong... Also I started to work on calculating the 50 day moving average (which is calculated by taking the average of the last 50 close values) and since its the last 50 i just started from the 449 row.. since my rows is a const of 500 rows im assuming that even with the new updated file it wont cause problems with the calculations.However while I tested it I realized that I need to convert the ints back two floats/doubles in order to get the correct calculation and I tried different things but to no avail.
so how can I convert the ints into doubles/floats? and also how can I write the function that calculates the YTD without printing the array and manually counting the rows?
int main() {
int data[numRows][cols] ;
double averageTotal=0;
constint nrecs = modifyAndgetDataFromFile( data ) ; //gets array from a different function
sortArray(data,0);//sorts the 1st column (the dates)
printArray(data);
cout << "second column sorted" <<endl;
sortArray(data,1); // sorts 2nd column (the price)
averageTotal=calcMovingAverage(data); //put this here to see what it printed
cout <<"this it" <<averageTotal;
//printArray(data);
// verify that things are ok by printing out nrecs rows
// for( int i = 0 ; i < nrecs ; ++i ) std::cout << data[i][0] << ' ' << data[i][1] << '\n' ;
return 0;
}
double calcMovingAverage(int data[][cols])
{
double average=0;
for(int i=449; i < numRows; i++)
{
for(int j=1; j < cols;j++)
{
average += data[i][j]static_cast<double>(j/100); // my attempt to convert back to double and send it to average
}
}
return average;
}