Help with calculation functions and how to convert ints stored in a 2d array back to floats/doubles

closed account (EAk1vCM9)
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  int main() {

	int data[numRows][cols] ;
	double averageTotal=0;

	    const int 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;

}



closed account (EAk1vCM9)
btw this is my array sorted by day
20151016 5086
20151019 5088
20151020 5081
20151021 5044
20151022 5120
20151023 5172
20151026 5161
20151027 5142
20151028 5213
20151029 5205
20151030 5183
20151102 5250
20151103 5264
20151104 5249
20151105 5244
20151106 5245
20151109 5194
20151110 5204
20151111 5183
20151112 5107
20151113 5054
20151116 5125
20151117 5118
20151118 5202
20151119 5194
20151120 5215
20151123 5213
20151124 5224
20151125 5229
20151127 5235
20151130 5211
20151201 5263
20151202 5207
20151203 5130
20151204 5226
20151207 5183
20151208 5151
20151209 5110
20151210 5123
20151211 5023
20151214 5037
20151215 5093
20151216 5168
20151217 5093
20151218 4981
20151221 5019
20151222 5063
20151223 5128
20151224 5122
20151228 5108
20151229 5162
20151230 5125
20151231 5078
20160104 4999
20160105 5008
20160106 4942
20160107 4821
20160108 4766
20160111 4765
20160112 4797
20160113 4673
20160114 4747
20160115 4648
20160119 4640
20160120 4594
20160121 4614
20160122 4711
20160125 4632
20160126 4703
20160127 4650
20160128 4669
20160129 4790
20160201 4788
20160202 4695
20160203 4719
20160204 4732
20160205 4636
20160208 4563
20160209 4557
20160210 4559
20160211 4503
20160212 4592
20160216 4675
20160217 4754
20160218 4732
20160219 4735
20160222 4803
20160223 4745
20160224 4769
20160225 4824
20160226 4823
20160229 4788
20160301 4900
20160302 4926
20160303 4949
20160304 4968
20160307 4978
20160308 4914
20160309 4939
20160310 4935
20160311 5022
20160314 4992
20160315 4974
20160316 5005
20160317 5043
20160318 5068
20160321 5072
20160322 5069
20160323 5028
20160324 5028
20160328 5031
20160329 5086
20160330 5106
20160331 5101
20160401 5131
20160404 5112
20160405 5061
20160406 5117
20160407 5055
20160408 5071
20160411 5057
20160412 5105
20160413 5165
20160414 5164
20160415 5162
20160418 5195
20160419 5211
20160420 5215
20160421 5188
20160422 5197
20160425 5184
20160426 5199
20160427 5210
20160428 5161
20160429 5133
20160502 5174
20160503 5123
20160504 5092
20160505 5089
20160506 5107
20160509 5112
20160510 5174
20160511 5126
20160512 5122
20160513 5081
20160516 5132
20160517 5083
20160518 5086
20160519 5065
20160520 5103
20160523 5094
20160524 5166
20160525 5202
20160526 5200
20160527 5226
20160531 5225
20160601 5236
20160602 5255
20160603 5238
20160606 5267
20160607 5276
20160608 5296
20160609 5284
20160610 5230
20160613 5166
20160614 5156
20160615 5151
20160616 5163
20160617 5149
20160620 5183
20160621 5194
20160622 5183
20160623 5257
20160624 5065
20160627 4963
20160628 5053
20160629 5143
20160630 5215
20160701 5228
20160705 5187
20160706 5217
20160707 5216
20160708 5299
20160711 5321
20160712 5362
20160713 5360
20160714 5385
20160715 5382
20160718 5394
20160719 5384
20160720 5411
20160721 5391
20160722 5418
20160725 5402
20160726 5408
20160727 5401
20160728 5411
20160729 5421
20160801 5413
20160802 5374
20160803 5395
20160804 5398
20160805 5446
20160808 5442
20160809 5445
20160810 5429
20160811 5455
20160812 5451
20160815 5472
20160816 5440
20160817 5447
20160818 5463
20160819 5457
20160822 5456
20160823 5471
20160824 5440
20160825 5436
20160826 5426
20160829 5456
20160830 5448
20160831 5435
20160901 5436
20160902 5464
20160906 5479
20160907 5484
20160908 5472
20160909 5333
20160912 5384
20160913 5303
20160914 5300
20160915 5355
20160916 5336
20160919 5341
20160920 5339
20160921 5400
20160922 5440
20160923 5409
20160926 5364
20160927 5395
20160928 5427
20160929 5375
20160930 5418
20161003 5400
20161004 5374
20161005 5399
20161006 5399
20161007 5379
20161010 5407
20161011 5336
20161012 5342
20161013 5323
20161014 5321
20161017 5307
20161018 5339
20161019 5353
20161020 5345
20161021 5344
20161024 5370
20161025 5345
20161026 5331
20161027 5309
20161028 5295
20161031 5298
20161101 5260
20161102 5221
20161103 5199
20161104 5195
20161107 5311
20161108 5332
20161109 5401
20161110 5416
20161111 5422
20161114 5435
20161115 5474
20161116 5468
20161117 5495
20161118 5487
20161121 5526
20161122 5543
20161123 5551
20161125 5573
20161128 5539
20161129 5546
20161130 5533
20161201 5510
20161202 5513
20161205 5553
20161206 5578
20161207 5649
20161208 5670
20161209 5697
20161212 5684
20161213 5717
20161214 5667
20161215 5690
20161216 5682
20161219 5662
20161220 5686
20161221 5670
20161222 5654
20161223 5666
20161227 5680
20161228 5631
20161229 5631
20161230 5606
20170103 5652
20170104 5697
20170105 5686
20170106 5703
20170109 5681
20170110 5688
20170111 5703
20170112 5688
20170113 5703
20170117 5679
20170118 5693
20170119 5670
20170120 5689
20170123 5674
20170124 5718
20170125 5765
20170126 5758
20170127 5750
20170130 5711
20170131 5713
20170201 5714
20170202 5717
20170203 5764
20170206 5749
20170207 5747
20170208 5753
20170209 5792
20170210 5815
20170213 5845
20170214 5868
20170215 5898
20170216 5891
20170217 5901
20170221 5938
20170222 5929
20170223 5926
20170224 5936
20170227 5949
20170228 5925
20170301 6008
20170302 5968
20170303 5971
20170306 5949
20170307 5928
20170308 5913
20170309 5914
20170310 5933
20170313 5940
20170314 5919
20170315 5974
20170316 5967
20170317 5963
20170320 5948
20170321 5864
20170322 5874
20170323 5848
20170324 5844
20170327 5841
20170328 5883
20170329 5893
20170330 5913
20170331 5904
20170403 5888
20170404 5891
20170405 5867
20170406 5887
20170407 5882
20170410 5888
20170411 5887
20170412 5858
20170413 5817
20170417 5869
20170418 5855
20170419 5850
20170420 5897
20170421 5879
20170424 5943
20170425 5981
20170426 5981
20170427 5985
20170428 5966
20170501 5979
20170502 5981
20170503 5969
20170504 5970
20170505 5998
20170508 5994
20170509 5989
20170510 6001
20170511 5986
20170512 5975
20170515 6006
20170516 6003
20170517 5892
20170518 5913
20170519 5954
20170522 5986
20170523 5997
20170524 6012
20170525 6037
20170526 6038
20170530 6027
20170531 6026
20170601 6082
20170602 6105
20170605 6094
20170606 6077
20170607 6086
20170608 6096
20170609 6093
20170612 6088
20170613 6120
20170614 6111
20170615 6096
20170616 6096
20170619 6146
20170620 6076
20170621 6070
20170622 6071
20170623 6086
20170626 6090
20170627 6039
20170628 6097
20170629 6046
20170630 6055
20170703 6073
20170705 6077
20170706 6018
20170707 6060
20170710 6061
20170711 6060
20170712 6105
20170713 6116
20170714 6143
20170717 6144
20170718 6145
20170719 6183
20170720 6182
20170721 6178
20170724 6175
20170725 6196
20170726 6193
20170727 6183
20170728 6174
20170731 6168
20170801 6182
20170802 6177
20170803 6164
20170804 6178
20170807 6187
20170808 6172
20170809 6163
20170810 6074
20170811 6083
20170814 6147
20170815 6140
20170816 6151
20170817 6056
20170818 6045
20170821 6051
20170822 6112
20170823 6094
20170824 6085
20170825 6097
20170828 6100
20170829 6105
20170830 6137
20170831 6177
20170901 6193
20170905 6144
20170906 6161
20170907 6159
20170908 6153
20170911 6220
20170912 6244
20170913 6248
20170914 6243
20170915 6257
20170918 6271
20170919 6276
20170920 6284
20170921 6241
20170922 6248
20170925 6236
20170926 6238
20170927 6271
20170928 6282
20170929 6303
20171002 6334
20171003 6348
20171004 6355
20171005 6389
20171006 6383
20171009 6369
20171010 6384

Topic archived. No new replies allowed.