The format of the file is:
ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
32768
ITEM: BOX BOUNDS pp pp ff
0.0000000000000000e+00 3.2000000000000000e+01
0.0000000000000000e+00 3.2000000000000000e+01
0.0000000000000000e+00 3.2000000000000000e+01
ITEM: ATOMS type x y z
1 0.292418 1.13983 1.28999
......
I read the header for each timestamp into a dummy string, and value of time step into an array. My code can read two timestamps correctly (65554 lines), but tellg() sets into -1 and I only get the last read values in my output. And also, the file never reaches EOF and my code continues for eternity.
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
string f = argv[1];int ens=1;string file;
fstream xyz("readas.xyz",ios_base::out); //to write data to cross-check what I read
fstream* Pxyz = &xyz;
float x,y,z,type;
int* time = (int*) malloc(100*sizeof(int));
int step;
string dummy;
while(ens < atoi(argv[2])) //this is to open different files to read
{
file=f+to_string(ens);
cout<<"Reading "+file<<endl;
fstream fobj(file,ios_base::in);
fstream* f= &fobj; //reading from this file
if(f->is_open())
{
*time=0;step=0;
while(true)
{
*f>>dummy>>dummy;
if(f->fail())break;
*f>>*(time+(++step));
*Pxyz << "32768" << "\n" << *(time+step) << endl;
*f>>dummy>>dummy>>dummy>>dummy;
*f>>dummy;
*f>>dummy>>dummy>>dummy>>dummy>>dummy>>dummy;
*f>>dummy>>dummy;
*f>>dummy>>dummy;
*f>>dummy>>dummy;
*f>>dummy>>dummy>>dummy>>dummy>>dummy>>dummy;
for(int i=0;i<32768;i++)
{
*f>>type>>x>>y>>z;
*Pxyz <<f->tellg() << " " << *(time+step) << " " << i << " " << type << " " << x << " " << y << " " << z << " " <<endl;
}
}
f->close(); //closing this file
}
++ens;
}
return 0;
}
The point at which the problem starts:
tellg time i and other values
1754887 10 32766 2 31.3309 31.9485 31.6061
1754914 10 32767 1 31.6358 31.1965 30.9986
32768
10
-1 10 0 0 31.6358 31.1965 30.9986
-1 10 1 0 31.6358 31.1965 30.9986
.......
I'm using ta-lib c++ library to calculate MACD, but the result is totally different from what the website shows,
the real MACD is [444.39, 505.05, 248.02, -232.33, 100.39, -13.18],
but my result is [282.10, -74.12, -211.27, -460.82, -850.86]
I have set all the MAType to TA_MAType_EMA, but it makes no sense
#include <iostream>
#include <cassert>
#include <ta-lib/ta_libc.h>
using namespace std;
int main()
{
// init ta-lib context
TA_RetCode retcode;
retcode = TA_Initialize();
assert(retcode == TA_SUCCESS);
// comput moving average price
TA_Real close_price_array[100] = { 37924.41, 40849.89, 37952.37, 36564.58, 36844.22, 34719.71, 33156.65, 32858.22,
34212.01, 37118.35, 31924.17, 30327.18, 31757.38, 34459.95, 31952.8 , 31876.57,
32457.32, 31392.34, 34183.43, 37328.12, 36408.31, 35732.04, 37460.76, 35627.27,
39551.87, 34677.01, 33834.78, 31580.01, 39674.77, 40513.11, 40829.87, 38950.0 ,
34555.33, 32091.45, 31737.83, 33506.67, 31695.17, 29190.91, 28779.14, 28153.95,
26617.04, 26911.93, 27360.51, 25625.24, 24019.43, 23230.15, 23450.3 , 23341.65,
23099.56, 23873.04, 23551.1 , 22553.6 , 23329.31, 20659.69, 19406.28, 19198.7 ,
19215.36, 18401.98, 18106.72, 18134.91, 18347.36, 18806.82, 19213.0 , 19126.33,
19107.67, 18945.51, 19533.84, 18891.06, 19265.5 , 19306.92, 18116.34, 17505.0 ,
16502.76, 16905.43, 19129.39, 19358.42, 18269.55, 18294.73, 18784.06, 18655.81,
18046.78, 17871.06, 17318.57, 16450.98, 16026.15, 15950.15, 16098.79, 16122.33,
15666.22, 15168.03, 15004.24, 15354.6 , 15342.63, 15411.23, 15077.18, 13911.95,
13708.92, 13492.15, 13797.96, 13854.39 };
TA_Real *p = close_price_array;
cout.precision(8);
TA_Integer out_begin = 0;
TA_Integer out_nb_element = 0;
TA_Real outMACD[100] = { 0 };
TA_Real outMACDSignal[100] = { 0 };
TA_Real outMACDHist[100] = { 0 };
retcode = TA_MACDEXT(0, 99,
&close_price_array[0],
12, TA_MAType_EMA ,
26, TA_MAType_EMA ,
9, TA_MAType_EMA ,
&out_begin, &out_nb_element,
outMACD, outMACDSignal, outMACDHist);
assert(retcode == TA_SUCCESS);
cout << "out_begin_index: " << out_begin << endl;
cout << "out_nb_element: " << out_nb_element << endl;
cout << "outMACD array: " << endl;
for (auto &i : outMACD)
cout << i << " ";
cout << endl;
cout << "outMACDSignal array: " << endl;
for (auto &i : outMACDSignal)
cout << i << " ";
cout << endl;
cout << "outMACDSignal array: " << endl;
for (auto &i : outMACDHist)
cout << i << " ";
cout << endl;
retcode = TA_Shutdown();
assert(retcode == TA_SUCCESS);
return 0;
}
enter image description here
[After comparing TA-lib results with Excel calculations]: In your excel the 12-day EMA is calculated from the 1st day and its first value is the average on 12th day (8/13/2020) and the 26-day EMA is calculated from 1st day and first value is average on 26th day (26/13/2020). TA-Lib postpones the 12 day EMA calculation start to get its first value on the same day as first value of 26-day EMA. That means 12-day EMA is calculated from 8/16/2020 and it's first value is the average on (26/13/2020) as it's in 26-day EMA. So to adjust your excel to TA-Lib results you need to copy formula =AVERAGE(B19:B30) into the cell C30.
Another note is that TA-Lib's MACD outputs 3 arrays at once: macd, signal, histogram. And thus TA-Lib starts the output at the point it got meaningful values for all 3 result arrays. Thus you're getting result starting not from the point where 26-day EMA can be calculated, but from the point where Signal can be calculated (8 days later). So you shall compare talib_macd[1] with excel starting from cell E38 instead of E30.
enter image description here
this has all the detail of the question
how do we print the number of options
we use this formula options = (cargo / lorrysize) + 1 this gives us the numbers of options
but after that we are stuck
example cargo size is 100
100 100/30=3+1=4
option 1
30
30
30
option 2
30
30
10
10
10
option 3
10x10
option 4
30
10x7
second example
150 150/30=5+1=6
option 1
30x5
option 2
30x4
10x3
option 3
30x3
10x6
option 4
30x2
10x9
option 5
30
10x12
option 6
10x15
we trying to do that but have no idea how to code that the "for loop" part
I hope this is enough for you guys to understand
#include <iostream>
#include <vector>
#include <iterator>
#include <fstream>
#include<string>
using namespace std;
/*
Steps to complete program
Step 1: Find total options available (how many Lorrys & Vans) for the input cargo capacity in .txt files
Step 2: For each option, find its: Amount of Lorrys and Vans
Total cost for both the Lorrys and Vans
Find the max trip
Step 3: Find the top 3 cheapest/lowest cost trips and display
Step 4: Find the fastest available trip and display
*/
class CTS //cargo transport system
{
int i;
int cargo, lorryprice, vanprice, lorrysize, vansize, allOps;
//vector<double> options, lorry, vans, totalC, nooftrips;
//vector <double>::iterator option, minilorry, cost, trips, van;
public:
void set_cargo(int);
void set_lorryprice(int);
void set_vanprice(int);
void set_lorrysize(int);
void set_vansize(int);
};
void CTS::set_cargo(int total_cargo){
cargo = total_cargo;
}
void CTS::set_lorryprice(int lorryP){
lorryprice = lorryP;
}
void CTS::set_vanprice(int vanP){
vanprice = vanP;
}
void CTS::set_lorrysize(int lorryS){
lorrysize = lorryS;
}
void CTS::set_vansize(int vanS)
{
vansize = vanS;
}
int main()
{
int cargo, lorryprice, vanprice, lorrysize, vansize, options, i;
ifstream infile;
infile.open("size.txt");
if(infile.is_open()){
infile >> cargo;
infile >> lorryprice;
infile >> vanprice;
infile >> lorrysize;
infile >> vansize;
}
CTS run;
run.set_cargo(cargo);
run.set_lorryprice(lorryprice);
run.set_vanprice(vanprice);
run.set_lorrysize(lorrysize);
run.set_vansize(vansize);
infile.close();
options = (cargo / lorrysize) + 1;
for (i = 0; i < options; i++)
{
cout << i << " " << cargo - lorrysize << endl; // this part we need help with we have no clue
}
/*cout << cargo << endl;
cout << lorryprice << endl;
cout << vanprice << endl;
cout << lorrysize << endl;
cout << vansize << endl;*/
return 0;
}
Once you have the maximum number of lorries, you can subtract the lorries' cargo and calculate the number of vans remaining like so:
for (int num_lorries = 0; num_lorries < options; num_lorries++) {
int remaining_cargo = cargo - lorrysize * num_lorries;
int num_vans = remaining_cargo / vansize;
// Exercise for you: figure out why this bit is needed.
// (consider remaining_cargo=25 and vansize = 10)
if (remaining_cargo % vansize > 0) {
num_vans++;
}
Solution sol(num_lorries, num_vans);
// Do something with sol
}
I'm having a little trouble with my code. It's pretty much supposed to open two files, and compare the first twenty line of the file "StudentAnswers.txt" [inputted as a char into a char array] against a char value in (each line of another file) "CorrectAnswers.txt" in another array at the same position (index). It's like a linear search, but the same position in the arrays. Then a report should be displayed, detailing which question the student missed, the given answer, the correct answer, and if the student passed (got >= 70%) or not, like the following:
Report for Student X:
2 (A/D), 3 (C/D), 5(D/A)
This student passed the exam!
Then it should clear the SAArray, and feed the next twenty lines from StudentAnswers.txt, and start the process all over again. I guess the program has to determine the number of students from (lines of 'StudentAnswers.txt' file / 20).
I'm having trouble displaying the report, and having the array clear itself after the program. I'm guessing this can be done with a while loop and an accumulator for the number of students (to be determined by above equation).
Also, Visual Studio seems to go to "Missed __ questions for a total of ___ %", and then keep looping -858993460.
Any help would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#include <algorithm>
using namespace std;
void GradeReturn(char[], char[], int, int, int);
string PassFail(float);
int main()
{
ifstream SA("StudentAnswers.txt");
ifstream CA("CorrectAnswers.txt");char CAArray[20];
char SAArray[20];
// char SA2Array[20];
bool isCorrect;
int correct;
int incorrect;
int counter;
correct = 0;incorrect = 0;
counter = 0;
cout << endl;
if (!SA.fail())
{
cout << "'StudentAnswers.txt' file opened successfully." << endl;
cout << "'CorrectAnswers.txt' file opened successfully." << endl << endl;
int a = 0;
int b = 0;
while (a < 20)
{
CA >> CAArray[a];
a++;
} // while loop to feed char into the array
while (b < 20)
{
SA >> SAArray[b];
b++;
}
} // while loop to feed char into array
CA.close(); // closing "CorrectAnswers.txt"
SA.close(); // closing "StudentAnswers.txt"
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter);
return 0;
}
void GradeReturn(char CAArray[], char SAArray[], int correct, int incorrect, int counter)
{
float percent;
float hundred;
int student;
int catcher[20];
int writeCatcher; int starter;
int catcher_size;
student = 0;
writeCatcher = 0;
catcher_size = ((sizeof catcher) / 4);
while (counter < 20)
{
if ((CAArray[counter]) == (SAArray[counter]))
{
correct++;
cout << "Good job!" << endl;
} // correct handling
else
{
incorrect++;
cout << "You got question " << counter << " wrong." << endl;
counter >> catcher[writeCatcher];
writeCatcher++;
} // incorrect handling
counter++;
} // while loop to determine if a student got a question right or wrong
static_cast <float> (incorrect); // float conversion
cout << endl; // for cleanliness
percent = ((static_cast <float> (correct)) / 20); // percentage
hundred = percent * 100;
PassFail(percent);
if (PassFail(percent) == "pass")
{
student++;
cout << "Report for Student " << student << ":" << endl;
cout << "-----------------------------" << endl;
cout << "Missed " << incorrect << " questions out of 20 for ";
cout << hundred << " % correct." << endl << endl;
starter = 0;
while (starter < (sizeof catcher)
{
if(1=1)
{
catcher_size
}
else
{
cout << "";
starter++;
}
}
}
else if (PassFail(percent) == "fail")
{
student++;
cout << "Missed " << incorrect << " questions out of 20 for ";
cout << hundred << " % correct." << endl << endl;
while (starter < catcher_size)
{
if ((catcher[starter]) == -858993460)
{
starter++;
}
else
{
cout << "";
starter++;
}
}
}
return;
}
string PassFail(float percent)
{
if (percent >= 0.70) // if <pass>
{
return "pass";
}
else // if <fail>
{
return "fail";
}
cout << endl;
}
To get a loop you should keep streams open instead of closing them after reading 20 lines.
As pseudo code that would be:
a = 0;
while(streams_not_empty)
{
CA >> CAArray[a];
SA >> SAArray[a];
++a;
if (a == 20)
{
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter);
a = 0; // Reset a
}
}
CA.close(); // closing "CorrectAnswers.txt"
SA.close(); // closing "StudentAnswers.txt"
You would also need to pass correct, incorrect, counter by reference so that the GradeReturn can change their value and their by do the accumulation.
Like:
void GradeReturn(char CAArray[], char SAArray[], int& correct, int& incorrect, int& counter)
Further you shouldn't rely on being able to read exactly Nx20 lines from the files every time. A file could have, e.g. 108 (5x20 + 8) lines, so you code should be able to handle the with only 8 lines. In other words, don't hard code 20 in your function like while (counter < 20). Instead pass the number of lines to be handled and do while (counter < number_to_handle).
Something like this as pseudo code:
a = 0;
while(streams_not_empty)
{
CA >> CAArray[a];
SA >> SAArray[a];
++a;
if (a == 20)
{
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter, a);
// ^
a = 0; // Reset a
}
}
if (a != 0)
{
// Process the rest
GradeReturn(&CAArray[counter], &SAArray[counter], correct, incorrect, counter, a);
}
CA.close(); // closing "CorrectAnswers.txt"
SA.close(); // closing "StudentAnswers.txt"
One problem you have is you're trying to compare C-style strings with the == operator. This will compare them essentially as if they were pointers to char, i.e. compare whether they point at the same location in memory, not compare the contents of the string. I urge you to look up array-decay and c-string variables to understand more.
Specifically, if (PassFail(percent) == "pass") isn't going to do what you want it to. strcomp doc, strncmp doc using std::string variables instead of c-style strings would all work, but it would be better simply to compare percent to a value, i.e. if(percent >= 0.70 directly instead of calling PassFail and comparing a string.
There are many other issues here also, you at one point call PassFail but do nothing with the return value. The only side affect of PassFail is cout << endl, if that's what you intend, it's a poor decision and hard to read way to put a newline on the console.
Try asking your compiler for more warnings, that's often helpful in finding these types of issues. -Wall -Wextra work for gcc, you may have to read your compiler manual...
If i ask the user to input a date
and he entered "16 then 5 then 2010"
how can I display it this way
16/5/10
I only did it this way
cout<<day<<"/"<<month<<"/"<<year<<endl;
but the output appears this way
16/5/2010
You could just print the year modulo 100. For example,
int yr1 = 1973;
int yr2 = 2010;
std::cout << (yr1%100) << std::endl; // prints 73
std::cout << (yr2%100) << std::endl; // prints 10