I have a two datetime in string format. for example
string str1 = "20160302101710"; //YYYYMMDDHHMMSS
string str2 = "20160302101715"; //same as above
Now, i need difference between two datetime is greater that 24Hours.
I have tried my problem as follows
I parsed the both string and put as follows:
#include <iostream>
using namespace std;
int main() {
time_t rawtime, rawtime1;
struct tm * timeinfo, *timeinfo1;
timeinfo->tm_year = 2016 ;
timeinfo->tm_mon = 03;
timeinfo->tm_mday = 02;
timeinfo->tm_hour = 10;
timeinfo->tm_min = 17;
timeinfo->tm_sec = 10;
rawtime = mktime(timeinfo);
timeinfo1->tm_year = 2016 ;
timeinfo1->tm_mon = 03;
timeinfo1->tm_mday = 02;
timeinfo1->tm_hour = 10;
timeinfo1->tm_min = 17;
timeinfo1->tm_sec = 15;
rawtime1 = mktime(timeinfo1);
cout<<"Diff: "<< difftime(rawtime1,rawtime);
return 0;
}
But After execution output is nothing.
Can anyone help me how to get the correct answer.
Once I will get the diff in second. then i will convert into hours.
Thanks,
You have to initialize the pointer with valid buffer before dereferencing them.
#include <iostream>
using namespace std;
int main() {
time_t rawtime, rawtime1;
struct tm buf1, buf2;
struct tm * timeinfo = &buf1, *timeinfo1 = &buf2;
timeinfo->tm_year = 2016 ;
timeinfo->tm_mon = 03;
timeinfo->tm_mday = 02;
timeinfo->tm_hour = 10;
timeinfo->tm_min = 17;
timeinfo->tm_sec = 10;
rawtime = mktime(timeinfo);
timeinfo1->tm_year = 2016 ;
timeinfo1->tm_mon = 03;
timeinfo1->tm_mday = 02;
timeinfo1->tm_hour = 10;
timeinfo1->tm_min = 17;
timeinfo1->tm_sec = 15;
rawtime1 = mktime(timeinfo1);
cout<<"Diff: "<< difftime(rawtime1,rawtime);
return 0;
}
Or you may just use structs without pointer variables.
#include <iostream>
using namespace std;
int main() {
time_t rawtime, rawtime1;
struct tm timeinfo, timeinfo1;
timeinfo.tm_year = 2016 ;
timeinfo.tm_mon = 03;
timeinfo.tm_mday = 02;
timeinfo.tm_hour = 10;
timeinfo.tm_min = 17;
timeinfo.tm_sec = 10;
rawtime = mktime(&timeinfo);
timeinfo1.tm_year = 2016 ;
timeinfo1.tm_mon = 03;
timeinfo1.tm_mday = 02;
timeinfo1.tm_hour = 10;
timeinfo1.tm_min = 17;
timeinfo1.tm_sec = 15;
rawtime1 = mktime(&timeinfo1);
cout<<"Diff: "<< difftime(rawtime1,rawtime);
return 0;
}
As indicated by R_Kapp, you need to allocate memory or just define a variable.
Modified program:
int main() {
time_t rawtime, rawtime1;
struct tm timeinfo, timeinfo1;
timeinfo.tm_year = 2016 ;
timeinfo.tm_mon = 03;
timeinfo.tm_mday = 02;
timeinfo.tm_hour = 10;
timeinfo.tm_min = 17;
timeinfo.tm_sec = 10;
rawtime = mktime(&timeinfo);
timeinfo1.tm_year = 2016 ;
timeinfo1.tm_mon = 03;
timeinfo1.tm_mday = 02;
timeinfo1.tm_hour = 10;
timeinfo1.tm_min = 17;
timeinfo1.tm_sec = 15;
rawtime1 = mktime(&timeinfo1);
cout<<"Diff: "<< difftime(rawtime1,rawtime);
return 0;
}
You can use Boost.Date_Time.
Examples
This can be done more easily with Howard Hinnant's free, open-source header-only datetime library:
#include "date/date.h"
#include <string>
#include <iostream>
#include <sstream>
int
main()
{
using namespace std;
using namespace std::chrono;
using namespace date;
string str1 = "20160302101710"; //YYYYMMDDHHMMSS
string str2 = "20160302101715"; //same as above
istringstream in{str1 + ' ' + str2};
sys_seconds t1, t2;
string fmt = " %Y%m%d%H%M%S";
in >> parse(fmt, t1) >> parse(fmt, t2);
cout << "Diff: " << t2 - t1 << '\n';
}
Output:
Diff: 5s
Related
This is a follow up question to dqrng with Rcpp for drawing from a normal and a binomial distribution. I tried to implement the answer but instead of drawing from a single distribution I'm drawing from 3. This is the code that I wrote:
// [[Rcpp::depends(dqrng, BH, RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <boost/random/binomial_distribution.hpp>
#include <xoshiro.h>
#include <dqrng_distribution.h>
// [[Rcpp::plugins(openmp)]]
#include <omp.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
arma::mat parallel_random_matrix(int n, int m, int ncores, double p=0.5) {
dqrng::xoshiro256plus rng(42);
arma::mat out(n*m,3);
// ok to use rng here
#pragma omp parallel num_threads(ncores)
{
dqrng::xoshiro256plus lrng(rng); // make thread local copy of rng
lrng.jump(omp_get_thread_num() + 1); // advance rng by 1 ... ncores jumps
int iter = 0;
#pragma omp for
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
iter = i * n + j;
// p can be a function of i and j
boost::random::binomial_distribution<int> dist_binomial(1,p);
auto gen_bernoulli = std::bind(dist_binomial, std::ref(lrng));
boost::random::normal_distribution<int> dist_normal1(2.0,1.0);
auto gen_normal1 = std::bind(dist_normal1, std::ref(lrng));
boost::random::normal_distribution<int> dist_normal2(4.0,3.0);
auto gen_normal2 = std::bind(dist_normal2, std::ref(lrng));
out(iter,0) = gen_bernoulli();
out(iter,1) = gen_normal1();
out(iter,2) = gen_normal2();
}
}
}
// ok to use rng here
return out;
}
/*** R
parallel_random_matrix(5, 5, 4, 0.75)
*/
When I try to run it Rstudio crashes. However, when I change the code like follows it does work:
// [[Rcpp::depends(dqrng, BH, RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <boost/random/binomial_distribution.hpp>
#include <xoshiro.h>
#include <dqrng_distribution.h>
// [[Rcpp::plugins(openmp)]]
#include <omp.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
arma::mat parallel_random_matrix(int n, int m, int ncores, double p=0.5) {
dqrng::xoshiro256plus rng(42);
arma::mat out(n*m,3);
// ok to use rng here
#pragma omp parallel num_threads(ncores)
{
dqrng::xoshiro256plus lrng(rng); // make thread local copy of rng
lrng.jump(omp_get_thread_num() + 1); // advance rng by 1 ... ncores jumps
int iter = 0;
#pragma omp for
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
iter = i * n + j;
// p can be a function of i and j
boost::random::binomial_distribution<int> dist_binomial(1,p);
auto gen_bernoulli = std::bind(dist_binomial, std::ref(lrng));
boost::random::normal_distribution<int> dist_normal1(2.0,1.0);
auto gen_normal1 = std::bind(dist_normal1, std::ref(lrng));
boost::random::normal_distribution<int> dist_normal2(4.0,3.0);
auto gen_normal2 = std::bind(dist_normal2, std::ref(lrng));
out(iter,0) = gen_bernoulli();
out(iter,1) = 2.0;//gen_normal1();
out(iter,2) = 3.0;//gen_normal2();
}
}
}
// ok to use rng here
return out;
}
/*** R
parallel_random_matrix(5, 5, 4, 0.75)
*/
What am I doing wrong?
Here lies the problem:
boost::random::normal_distribution<int> dist_normal1(2.0,1.0);
^^^
This distribution is meant for real types, not integral types, c.f. https://www.boost.org/doc/libs/1_69_0/doc/html/boost/random/normal_distribution.html. Correct would be
boost::random::normal_distribution<double> dist_normal1(2.0,1.0);
I have looked extensively for the problem in this code, but I can't seem to figure out what tragic error I made and why it is triggering a breakpoint.
(After 3 or 4 inputs, it triggers and I don't know why it doesn't trigger at the start or what is causing it)
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout", for example.
string convertDecToBin(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 2;
r = r / 2;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
string convertDecToOct(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 8;
r = r / 8;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
int main()
{
int input = 0;
while (input != -1)
{
cout << "\nEnter a decimal number (-1 to exit loop): ";
cin >> input;
if (input != -1)
{
cout << "Your decimal number in binary expansion: " << convertDecToBin(input);
cout << "\nYour decimal number in octal ecpression: " << convertDecToOct(input);
}
}
cout << "\n\nPress any key to exit. . .";
_getch();
return 0;
}
arrayHex = new int[] is your problem - C\C++ does not support dynamic sizing arrays. You need to specify a size for the array to allocation, otherwise you'll get memory block overruns.
here is my code, I can not figure out why it won't work as a function when the exact code in main() produces the correct answer. The assignment is to convert binary number to decimal.
#include <iostream>
#include <cstdlib>
#include "std_lib_facilities.h"
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
enter code here`while (biNum > 0)
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
return biNum;
}
corrected code:
#include <iostream>
#include <cstdlib>
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
getchar();
return 0;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
return dec;
}
As you are not using { and } in your while loop may be its going in infinite loop. As its working for this line only
while (biNum > 0)
rem = biNum % 10; // running this line infinite as `biNum > 0`
Use
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
maybe this?
while (biNum > 0)
{
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
int main() or int main(int argc, char** argv) must return a value. If you return 0 then it means that there is no problem with code. Another numbers 1,2 etc means there is an error.(Returned numbers are error numbers)
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;
}
I have created a Fibonacci program that runs correctly. However I can not figure out how to format the output window the way the problem would like. The rows and spacing are correct but the program should display 6 columns, as it is now the program outputs nine with the ninth cut off. Am I doing something wrong or missing something? I am using the Visual Studio C++ compiler.
#include <iostream>
#include <iomanip>
using namespace std;
void main ()
{
int FirstNum = 1;
int SecondNum = 0;
int Count = 1;
int Answer;
do
{
Answer = FirstNum + SecondNum;
FirstNum = SecondNum;
SecondNum = Answer;
cout << FirstNum << setw (10);
Count++;
} while (Count < 40);
}
This code will generated only 6 columns.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int FirstNum = 1;
int SecondNum = 0;
int Count = 1;
int Answer;
do
{
Answer = FirstNum + SecondNum;
FirstNum = SecondNum;
SecondNum = Answer;
cout << setw (10)<< FirstNum ;
Count++;
if(Count%6==0)
cout<<endl;
} while (Count < 40);
return 0;
}