missing template arguments before '[' token - rcpp

I am very new in R and cpp. These are my cpp codes with Rcpp.
#include <cmath>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector getPerformance(NumericVector accProfitSeries, NumericVector dateSeries, double capital, double riskFreeRate)
{
NumericVector retult(8);
int Length = accProfitSeries.length();
double strategyProfit = accProfitSeries[Length - 1];
double returnRate = log(strategyProfit / capital);
double tradingPeriod = (dateSeries[dateSeries.length() - 1] - dateSeries[0]) / 365;
double returnRateYear = returnRate / tradingPeriod;
double sharpeRatio;
for (int i = 0; i < 8; i++) {
result[i] = 0;
}
NumericVector capitalLevel(Length), capitalReturn(Length);
double meanReturn, sumReturn, stdReturn;
double tmp;
for (int i = 0; i < Length; i++)
{
capitalLevel[i] = accProfitSeries[i] + capital;
capitalReturn[i] = log(capitalReturn[i] / capitalReturn[0]);
sumReturn = sumReturn + capitalReturn[i];
}
meanReturn = sumReturn / Length;
for (int i = 0; i < Length; i++)
tmp += pow(capitalReturn[i] - meanReturn, 2);
stdReturn = sqrt(tmp / Length);
sharpeRatio = (meanReturn - riskFreeRate) / stdReturn;
double maxCapital = 0, drawback = 0, maxDrawback = 0, maxDrawbackPercent = 0;
int drawbackPeriod = 0, maxDrawbackPeriod = 0;
for (int i = 0; i < Length; i++)
{
maxCapital = max(NumericVector::create(maxCapital, capitalLevel[i]));
drawback = capitalLevel[i] - maxCapital;
maxDrawback = min(NumericVector::create(maxDrawback, drawback));
maxDrawbackPercent = maxDrawback / (maxCapital - capital);
if (drawback >= 0)
{
drawbackPeriod = 0;
}
else
{
drawbackPeriod++;
maxDrawbackPeriod = max(NumericVector::create(maxDrawbackPeriod, drawbackPeriod));
}
}
result[0] = returnRate;
result[1] = tradingPeriod;
result[2] = returnRateYear;
result[3] = sharpeRatio;
result[4] = maxDrawback;
result[5] = maxDrawbackPeriod;
result[6] = strategyProfit;
result[7] = maxDrawbackPercent;
return(result);
}
I got one of the error messages, others are similar:
getPerformance.cpp:17:9: error: missing template arguments before '[' token
result[i] = 0;
How do I solve it?

Mainly, you have two issues:
As pointed out by #DirkEddelbuettel, you need to change retult(8) to result(8) to proper setup a variable referenced elsewhere in the script.
You failed to initialize several double variables.
The following should work...
#include <cmath>
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector getPerformance(NumericVector accProfitSeries,
NumericVector dateSeries,
double capital, double riskFreeRate)
{
NumericVector result(8); // retult -> result
int Length = accProfitSeries.length();
double strategyProfit = accProfitSeries[Length - 1];
double returnRate = log(strategyProfit / capital);
double tradingPeriod = (dateSeries[dateSeries.length() - 1] - dateSeries[0]) / 365;
double returnRateYear = returnRate / tradingPeriod;
double sharpeRatio = 0.0; // Initialize value
for (int i = 0; i < 8; i++) {
result[i] = 0;
}
NumericVector capitalLevel(Length), capitalReturn(Length);
// Initialize values
double meanReturn = 0.0, sumReturn = 0.0, stdReturn = 0.0;
double tmp = 0.0;
for (int i = 0; i < Length; i++) {
capitalLevel[i] = accProfitSeries[i] + capital;
capitalReturn[i] = log(capitalReturn[i] / capitalReturn[0]);
sumReturn = sumReturn + capitalReturn[i];
}
meanReturn = sumReturn / Length;
for (int i = 0; i < Length; i++)
tmp += pow(capitalReturn[i] - meanReturn, 2);
stdReturn = sqrt(tmp / Length);
sharpeRatio = (meanReturn - riskFreeRate) / stdReturn;
double maxCapital = 0, drawback = 0, maxDrawback = 0, maxDrawbackPercent = 0;
int drawbackPeriod = 0, maxDrawbackPeriod = 0;
for (int i = 0; i < Length; i++) {
maxCapital = max(NumericVector::create(maxCapital, capitalLevel[i]));
drawback = capitalLevel[i] - maxCapital;
maxDrawback = min(NumericVector::create(maxDrawback, drawback));
maxDrawbackPercent = maxDrawback / (maxCapital - capital);
if (drawback >= 0) {
drawbackPeriod = 0;
} else {
drawbackPeriod++;
maxDrawbackPeriod = max(NumericVector::create(maxDrawbackPeriod, drawbackPeriod));
}
}
result[0] = returnRate;
result[1] = tradingPeriod;
result[2] = returnRateYear;
result[3] = sharpeRatio;
result[4] = maxDrawback;
result[5] = maxDrawbackPeriod;
result[6] = strategyProfit;
result[7] = maxDrawbackPercent;
return(result);
}

Related

Why is my integer variable being added to my other integer variable?

I am writing a program that counts the amount of letters and words in a string given by the user. For some reason, the number of words is being added to the number of letters. If there is 3 words in the sentence and 12 letters, then it says that there is 15 words. My code is below:
#include <cs50.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
int storeLetters[] = {};
int storeWords[] = {};
// declare functions
int count_letters(string text);
int count_words(string text);
int main(void)
{
// ask user for text passage
string text = get_string("Text: ");
int numOfLetters = count_letters(text);
printf("%d",numOfLetters);
printf(" letters\n");
int numOfWords = count_words(text);
printf("%d",numOfWords);
printf(" words\n");
}
int count_letters(string text)
{
int amountOfLetters = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isalpha(text[i]))
{
storeLetters[i] += 1;
amountOfLetters += storeLetters[i];
}
else
{
storeLetters[i] += 0;
amountOfLetters += storeLetters[i];
}
}
return amountOfLetters;
}
int count_words(string text)
{
int amountOfWords = 0;
for (int x = 0, n = strlen(text); x < n; x++)
{
if (text[x] == '?' || text[x] == '!' || text[x] == '.' || text[x] == ' ')
{
storeWords[x] += 1;
amountOfWords += storeWords[x];
}
else
{
storeWords[x] += 0;
amountOfWords += storeWords[x];
}
}
return amountOfWords;
}
storeLetters and storeWords are arrays, which you initialize to zero length. If you access storeLetters[0] (or any other index), you go past the end. It's not gonna work.
You don't need those variables at all. Just increment amountOfLetters directly.
int count_letters(string text)
{
int amountOfLetters = 0;
for (int i = 0, n = strlen(text); i < n; i++)
{
if (isalpha(text[i]))
{
amountOfLetters += 1;
}
}
return amountOfLetters;
}

(C++) How to round decimals like 1.05000?

I have a problem where in my algorhithm I should round the result down to 5 decimal places, with the zeros included even after the last number.
The only test-case not working in my algorhithm is:
Input:
milk 1
4
bread
meat
milk
aaaaa
Output:
1.05000 // and my output displays 1.5
For an example; my result of 1.05 should display as 1.05000 or 1.2 as 1.20000. Now, the rest of the algorhithm is working fine, so the only problem is the rounding part:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
break;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
// here is where the problem starts
result_circle = result * 1000000; //temporarily expanding the number to the 6th decimal place
if ((int)result_circle % 10 > 4) { // checking if the 6th decimal place is bigger than 4
result_circle += 10; // increasing the 5th decimal place
}
result_circle = (int)result_circle / 10; // removing the 6th decimal place which we were checking
cout << (int)result_circle / 100000 << '.' << (int)result_circle % 100000; // here is the main problem, where 105000 % 100000 is seen as 5000 not 05000
return 0;
}
I assume the main problem here is that ‘105000 % 100000 = 5000’ because the 0 after the decimal point is unfortunately left out.
If anyone could display the simplest way to fix this problem it would be great.
#include <iomanip>
.
.
.
cout << fixed << setprecision(5) << result;
This is the code that worked for me, answered by _Bob.
The full code:
#include <iostream>
#include <string.h>
#include <iomanip>
using namespace std;
int main()
{
char name[50];
cin >> name;
double price = 0;
cin >> price;
int N;
cin >> N;
char check_name[50];
double result = 0;
bool found = false;
double result_circle = 0;
int finally_found = 0;
for (int k = 0; k < N; k++) {
cin >> check_name;
for (int i = 0; i < strlen(name); i++) {
if (name[i] == check_name[i]) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
finally_found++;
}
}
if (finally_found > 0) {
result = price + (price * 0.05);
} else {
result = price + (price * 0.18);
}
cout << fixed << setprecision(5) << result;
return 0;
}

Exception thrown: read access violation. this->pt_mat was 0x1110112

I got this error while using a double pointer in a class function to set coefficient and using it in the main.
The class function is used in a stand-alone function to set the coefficient using a class type pointer.
The class code is:
class Matrix
{
private:
int size_m, size_n;
double **pt_mat;
public:
Matrix() {};
Matrix(int m, int n)
{
pt_mat = new double *[m];
for (int i = 0; i < m; i++)
{
pt_mat[i] = new double[n];
}
}
void set_size(int m, int n)
{
size_m = m;
size_n = n;
}
void set_coef(int i, int j, double x)
{
pt_mat[i][j] = x;
}
void get_size(int *pt_m, int *pt_n)
{
*pt_m = size_m;
*pt_n = size_n;
}
double get_coef(int i, int j)
{
return pt_mat[i][j];
}
void print(ofstream &fout)
{
fout << size_m << " " << size_n << endl;
int i, j;
for (i = 0; i < size_m; i++)
{
for (j = 0; j < size_n; j++)
{
fout << pt_mat[i][j] << " ";
}
fout << endl;
}
}
void max_min(int *pt_imax, int *pt_jmax, double *pt_max, int *pt_imin, int *pt_jmin, double *pt_min)
{
double max, min;
int i, j, imax = 0, imin = 0, jmax = 0, jmin = 0;
max = pt_mat[0][0];
for (i = 0; i < size_m; i++)
{
for (j = 0; j < size_n; j++)
{
if (pt_mat[i][j] > max)
{
max = pt_mat[i][j];
imax = i;
jmax = j;
}
}
}
*pt_max = max;
*pt_imax = imax;
*pt_jmax = jmax;
min = pt_mat[0][0];
for (i = 0; i < size_m; i++)
{
for (j = 0; j < size_n; j++)
{
if (pt_mat[i][j] < min)
{
min = pt_mat[i][j];
imin = i;
jmin = j;
}
}
}
*pt_min = min;
*pt_imin = imin;
*pt_jmin = jmin;
}
};
The stand-alone function:
void mat_add(Matrix a, Matrix b, Matrix *pt_c)
{
Matrix c;
pt_c = &c;
int a_m, a_n, *p_am = &a_m, *p_an = &a_n;
a.get_size(p_am, p_an);
c.set_size(a_m, a_n);
int m, n, *pt_m = &m, *pt_n = &n;
double coef;
a.get_size(pt_m, pt_n);
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
{
coef = a.get_coef(i, j) + b.get_coef(i, j);
c.set_coef(i, j, coef);
}
}
please help me in this regard if you can. I have no syntax error showing.enter code here

Import FBX Vertex And Index Buffer To DirectX 11

Ok, I'm still trying to figure out how to correctly import FBX vertex and index buffer into DirectX 11. I wrote a controller for doing that and passing the vertex and index buffer to the DX11 renderer, the output should look like a cube but it is not, I only see triangles that don't make sense.
The code is shown below. I did multiply the Z values by -1, though.
What do I need to modify to get the render right?
#pragma once
#include "Array.h"
#include "Vector.h"
#include "fbxsdk.h"
#include <assert.h>
#include "constants.h"
class FbxController
{
public:
FbxController();
~FbxController();
void Import(const char* lFilename)
{
lImporter = FbxImporter::Create(lSdkManager, "");
bool lImportStatus = lImporter->Initialize(lFilename, -1, lSdkManager->GetIOSettings());
if (!lImportStatus) {
printf("Call to FbxImporter::Initialize() failed.\n");
printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString());
exit(-1);
}
lScene = FbxScene::Create(lSdkManager, "myScene");
lImporter->Import(lScene);
FbxNode* lRootNode = lScene->GetRootNode();
int childCount = lRootNode->GetChildCount();
FbxNode *node1 = lRootNode->GetChild(0);
const char* nodeName1 = node1->GetName();
fbxsdk::FbxMesh *mesh = node1->GetMesh();
int cpCount1 = mesh->GetControlPointsCount();
fbxsdk::FbxVector4 *controlPoints = mesh->GetControlPoints();
for (int i = 0; i < cpCount1; i++)
{
fbxsdk::FbxVector4 cpitem = controlPoints[i];
printf("%d, %d, %d, %d", cpitem[0], cpitem[1], cpitem[2], cpitem[3] );
VERTEXPOSCOLOR vpc;
vpc.Color.x = 0.5f;
vpc.Color.y = 0.5f;
vpc.Color.z = 0.5f;
vpc.Position.x = cpitem[0];
vpc.Position.y = cpitem[1];
vpc.Position.z = cpitem[2] * -1.0f;
m_vertices.add(vpc);
}
int pvCount = mesh->GetPolygonVertexCount();
int polyCount = mesh->GetPolygonCount();
for (int i = 0; i < polyCount; i++)
{
int polyItemSize = mesh->GetPolygonSize(i);
assert(polyItemSize == 3);
for (int j = 0; j < polyItemSize; j++)
{
int cpIndex = mesh->GetPolygonVertex(i, j);
m_indices.add(cpIndex);
float x = controlPoints[cpIndex].mData[0];
float y = controlPoints[cpIndex].mData[1];
float z = controlPoints[cpIndex].mData[2];
}
}
fbxsdk::FbxMesh *mesh2;
bool isT = mesh->IsTriangleMesh();
FbxNode *node2 = lRootNode->GetChild(1);
FbxNode *node3 = lRootNode->GetChild(2);
//lImporter->Destroy();
}
Array<VERTEXPOSCOLOR> GetVertexPosColors()
{
return m_vertices;
}
Array<unsigned int> getIndexBuffer()
{
return m_indices;
}
protected:
FbxManager *lSdkManager;
FbxIOSettings *ios;
FbxImporter *lImporter;
bool lImportStatu;
FbxScene *lScene;
private:
Array<VERTEXPOSCOLOR> m_vertices;
Array<unsigned int> m_indices;
};
I think you have some problems in your index buffer creation.
You simply gives an index for each vertex, and index buffer not working that way.
let me know if you solve this.

char in string is not in order wanted

I am a beginner and I need some help here. This program prints out the frequency of char in the string, e.g. if user enters zzaaa it prints out a3z2 and what I need to print is z2a3 since z is entered first before a. But I am having a hard time switching the order around. Thanks in advance!
int main
{
int ib, i=0, j=0, k=0;
int count[26] = {0};
char chh[3][10];
for (ib = 0; ib < 3; ib++) // get 3 input
gets(chh[ib]);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 10; j++)
{
if (chh[i][j] >= 'a' && chh[i][j] <= 'z')
{
count[chh[i][j] - 'a']++;
}
}
for (k = 0; k < 26; k++)
{
if (count[k] != 0) // if array location is not equals to 0
printf("%c%d", k + 'a', count[k]);
}
memset(count, 0, sizeof(count)); //reset integer array
printf("\n");
}
It prints a before z because you arranged count from a to z by alphabetic priority not entering priority:
count[chh[i][j] - 'a']
if you want to print them by entering priority you should change it. there are several ways to do this. like this:
#include <stdio.h>
#include <string.h>
int main()
{
int ib, i=0, j=0,k=0, kk=0,c=0,found=0;
int count[26][2];
char chh[3][10];
for (ib = 0; ib < 3; ib++) // get 3 input
gets(chh[ib]);
printf("output is:\n");
for (i=0;i<26;i++)
{
count[i][0]=0;
count[i][1]=0;
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 10; j++)
{
if (chh[i][j] >= 'a' && chh[i][j] <= 'z')
{
found=0;
for (c=0;c<kk;c++)
if (count[c][0]==chh[i][j])
{
count[c][1]++;
found=1;
break;
}
if (!found)
{
count[c][0]=chh[i][j];
count[c][1]++;
kk++;
}
}
}
for (k = 0; k < 26; k++)
{
if (count[k][1] != 0) // if array location is not equals to 0
printf("%c%d", count[k][0], count[k][1]);
}
memset(count, 0, sizeof(count)); //reset integer array
printf("\n");
}
}

Resources