I use R by rinside
std::string cmd = "m <-matrix(c(1:9),nrow=3))";
SEXP proxy = m_R.parseEvalNT(cmd);
Rcpp::NumericMatrix m(proxy);
the runtime to terminate it in an unusual way
anyone please help me
thanks
Odd. I played with this and also got some run-time errors; now they no longer arise. Try this:
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
//
// cf http://stackoverflow.com/questions/18403402/
//
// GPL'ed
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
std::string cmd = "m <- matrix(c(1:9),nrow=3)";
Rcpp::NumericMatrix M = R.parseEval(cmd); // assign mat. M to NumericMatrix
int n = M.nrow();
int k = M.ncol();
std::cout << "All good:" << n << " by " << k << std::endl;
exit(0);
}
edd#max:~/svn/rinside/pkg/inst/examples/standard$ ./so20130823
All good:3 by 3
edd#max:~/svn/rinside/pkg/inst/examples/standard$
which I just copied into the examples/standard/ directory as a simple call to make will then build it.
Related
I'm using vs2019 and I have a problem the compiler give me an error however I cant solving that
please help me.
the error is : argument of type"const char*" is incompatible with parameter of type"char*"
Blockquote
*** #include <iostream>
#include<cstring>
using namespace std;
class part
{
public:
char partname[30];
int partnumber;
double cost;
void setpart(char pname[], int pn, double c)
{
strcpy_s(partname, pname);
partnumber = pn;
cost = c;
}
void showpart()const
{
cout << "\npartname : " << partname;
cout << "\npartnumber : " << partnumber;
cout << "\ncost($) : " << cost;
}
};
int main()
{
part part1, part2;
part1.setpart("handle bolt", 467, 4500);
part2.setpart("start lever", 549, 2300);
cout << "\nFirst part : "; part1.showpart();
cout << "\nSecond part : "; part2.showpart();
}***
The strings you are giving to setpart are const strings ( const char *) . But set part take a char * as parameter. Since pname will not be modified in your example you can replace void setpart(char pname[], int pn, double c) by void setpart(const char pname[], int pn, double c)
Threaded quick sort method:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "MD5.h"
#include <thread>
using namespace std;
template<typename T>
void quickSort(vector<T> &arr, int left, int right) {
int i = left, j = right; //Make local copys to modify
T tmp; //Termorary variable to use for swaping.
T pivot = arr[(left + right) / 2]; //Find the centerpoint. if 0.5 truncate.
while (i <= j) {
while (arr[i] < pivot) //is i < pivot?
i++;
while (arr[j] > pivot) //Is j > pivot?
j--;
if (i <= j) { //Swap
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
thread left_t; //Left thread
thread right_t; //Right thread
if (left < j)
left_t = thread(quickSort<T>, ref(arr), left, j);
if (i < right)
right_t = thread(quickSort<T>, ref(arr), i, right);
if (left < j)
left_t.join();
if (left < j)
right_t.join();
}
int main()
{
vector<int> table;
for (int i = 0; i < 100; i++)
{
table.push_back(rand() % 100);
}
cout << "Before" << endl;
for each(int val in table)
{
cout << val << endl;
}
quickSort(table, 0, 99);
cout << "After" << endl;
for each(int val in table)
{
cout << val << endl;
}
char temp = cin.get();
return 0;
}
Above program lags like mad hell and Spams "abort()" has been called.
Im thinking it has something to do with vectors and it Having threading issues
Iv seen the Question asked by Daniel Makardich, His Utilizes a Vector int While mine uses Vector T
You don't have any problem with quick sort, but with passing a templated function to a thread. There is no function quickSort. You need to explicitly give type, to instantiate the function template:
#include <thread>
#include <iostream>
template<typename T>
void f(T a) { std::cout << a << '\n'; }
int main () {
std::thread t;
int a;
std::string b("b");
t = std::thread(f, a); // Won't work
t = std::thread(f<int>, a);
t.join();
t = std::thread(f<decltype(b)>, b); // a bit fancier, more dynamic way
t.join();
return 0;
}
I suspect in your case this should do:
left_t = thread(quickSort<T>, ref(arr), left, j);
And similar for right_t. Also, you have mistake there trying to use operator()() instead of constructing an object. That is why the error is different.
Can't verify though, cause there's no minimal verifiable example =/
I don't know if it's possible to make compiler to use automatic type deduction for f passed as a param, if anyone knows that would probably make it a better answer.
Problem was with thread joins and what #luk32 said
Needed to convert the threads to pointers to threads.
thread* left_t = nullptr; //Left thread
thread* right_t = nullptr; //Right thread
if (left < j)
left_t = new thread(quickSort<T>, ref(arr), left, j);
if (i < right)
right_t = new thread(quickSort<T>, ref(arr), i, right);
if (left_t)
{
left_t->join();
delete left_t;
}
if (right_t)
{
right_t->join();
delete right_t;
}
Seems like if you create a default constructed thread object. But don't use it, it still wants to be joined. and if you do join it, it will complain.
Using Bottom to up DP approach, I am able to solve the problem How to solve http://www.spoj.com/problems/MST1/ upto 10^8.
If input is very large n upto 10^9. I will not be able to create lookup table for upto 10^9. So what will be better approach to solve the problem ?
Is there any heuristic solution ?
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int main()
{
const int N_MAX = 20000001;
int *DP = new int[N_MAX];
DP[1] = 0;
for (int i = 2; i < N_MAX; i++) {
int minimum = DP[i - 1];
if (i % 3 == 0) minimum = min(minimum, DP[i/3]);
if (i % 2 == 0) minimum = min(minimum, DP[i/2]);
DP[i] = minimum + 1;
}
int T, N; cin >> T;
int c = 1;
while (T--) {
cin >> N;
cout << "Case " << c++ << ": " << DP[N] << endl;
}
delete[] DP;
}
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
class TheNumberGameDivTwo{
private:
public:
string find( int n ){
string n1 = "John";
string n2 = "Brus";
//cout << "Mahesh"<< n2 << n1;
int i,q;
q = 0;
int k = n;
i = n-1;
for ( i = 2 ; i < k ; ++i ){
if ( !(k % i) ){
q += k / i;
k = k % i;
}
}
if ( q % 2 )
return "John";
else
return "Brus";
}
};
int main(){
TheNumberGameDivTwo T;
cout << endl << T.find(6);
cout << endl << "End of Program " << endl;
system("PAUSE");
}
Error 1
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
d:\my documents\visual studio 2012\c++\topcoder\topcoder\source1.cpp 34 1 TopCoder
You are missing #include <string>
I have an array of a large structure that I am trying to output to hard drive. I seem to be able to write to hard drive just fine (though it's difficult to verify by looking at the binary data), however when I try to read it back, I always end up with a garbled mess. Any ideas what I'm doing wrong?
here's the structure configuration:
class xyz
{
public:
double x, y, z;
};
class trianglePackage
{
public:
int score;
int position;
xyz contactCoordinates;
xyz normalVector;
xyz locatorOffset;
};
class quadanglesOutput
{
public:
int locator1position, locator2position, locator3position, locator4position;
xyz centroid;
int surfaceAreaScore;
int centroidDifferance1Score;
int centroidDifferance2Score;
int minDistance1Score;
int minDistance2Score;
int totalLocatorScore;
int totalHullScore;
int totalScore;
double surfaceArea;
double centroidDifferance1;
double centroidDifferance2;
double minDistance1;
double minDistance2;
int hull;
trianglePackage locator1, locator2, locator3, locator4;
};
and here are the read/write functions I'm using:
void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
{
string outputName = parameters.fileName + " " + description + ".bin";
cout << "Output " << outputName.c_str() << "...";
ofstream output2;
output2.open(outputName.c_str());
output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
output2.close();
cout << "done" << endl;
}
void readIn(quadanglesOutput* pointer, param parameters, string description)
{
string fileName = parameters.fileName + " " + description + ".bin";
cout << "openining " << fileName << "...";
ifstream readFile;
readFile.open(fileName.c_str());
readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
readFile.close();
cout << "done" << endl;
}
Typically the arrays of structures are about 100 in length, but usually only about the first 25 read back correctly, everything else is default uninitialized data.
I'm 99% sure that it's something wrong with my code, however is there a possibility it has something to do with four byte alignment?
Thanks.
It may be an issue with byte alignment, use pragma.
try wrap classes around with
#PRAGMA PACK PUSH(1)
....
#PRAGMA PACK POP
or
#PRAGMA PACK(1)
struct{
..
}
Try those as well:
Force binary flag for the stream.
ios_base::binary
readFile.open(fileName.c_str(), ios_base::binary);
Try to flush the the stream.
stream.write(...)
stream.flush()
//i know that close() should flush it.
UPDATE:
Everything works for me:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
#pragma pack(1)
class xyz
{
public:
double x, y, z;
};
#pragma pack(1)
class trianglePackage
{
public:
int score;
int position;
xyz contactCoordinates;
xyz normalVector;
xyz locatorOffset;
};
#pragma pack(1)
class quadanglesOutput
{
public:
int locator1position, locator2position, locator3position, locator4position;
xyz centroid;
int surfaceAreaScore;
int centroidDifferance1Score;
int centroidDifferance2Score;
int minDistance1Score;
int minDistance2Score;
int totalLocatorScore;
int totalHullScore;
int totalScore;
double surfaceArea;
double centroidDifferance1;
double centroidDifferance2;
double minDistance1;
double minDistance2;
int hull;
trianglePackage locator1, locator2, locator3, locator4;
};
class param
{
public:
string fileName;
int topXlist;
};
void outputQuadangleOutput(quadanglesOutput* output, string description, param parameters)
{
string outputName = parameters.fileName + " " + description + ".bin";
cout << "Output " << outputName.c_str() << "...";
ofstream output2;
output2.open(outputName.c_str());
output2.write(reinterpret_cast<char*>(output), streamsize(parameters.topXlist * sizeof(quadanglesOutput)));
output2.close();
cout << "done" << endl;
}
void readIn(quadanglesOutput* pointer, param parameters, string description)
{
string fileName = parameters.fileName + " " + description + ".bin";
cout << "openining " << fileName << "...";
ifstream readFile;
readFile.open(fileName.c_str());
readFile.read(reinterpret_cast<char*>(pointer), (parameters.topXlist * sizeof(quadanglesOutput)));
readFile.close();
cout << "done" << endl;
}
int main(int argc, char *argv[])
{
quadanglesOutput a = {0};
cout<<"total score:"<<a.totalScore<<endl;
cout<<"locator position:"<<a.totalScore<<endl;
cout<<"locator position:"<<a.locator1.position<<endl;
cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
cout<<"sizeof quadangsomething:"<<sizeof(quadanglesOutput)<<endl;
a.totalScore=1;
a.locator1.position=333445;
a.locator2.normalVector.y = 999.3224;
cout<<"total score:"<<a.totalScore<<endl;
cout<<"locator position:"<<a.locator1.position<<endl;
cout<<"locator position:"<<a.locator2.normalVector.y <<endl;
param p = {"C:/", 1};
outputQuadangleOutput(&a, "file1", p);
quadanglesOutput *b = new quadanglesOutput();
readIn(b, p, "file1");
cout<<"new total score:"<<b->totalScore<<endl;
cout<<"new locator position:"<<b->locator1.position<<endl;
cout<<"new locator position:"<<b->locator2.normalVector.y <<endl;
delete b;
string asdf;
cin>>asdf;
};
OUTPUT:
total score:0
locator position:0
locator2.normalVector.y :0
sizeof quadangsomething:436
total score:1
locator position:333445
locator2.normalVector.y :999.322
Output C:/ file1.bin...done
openining C:/ file1.bin...done
new total score:1
new locator position:333445
new locator2.normalVector.y :999.322
without pragma it's still correct but you can see the difference in size:
sizeof quadangsomething:440
But packing it is good when sending structures over network.
Because here system alligns it always in the same fashion.