Hey everyone!
From what I've seen, many of you know how to code/program, but do any of you know how to program in C++? I've been having a little trouble with one of my school projects; it would be epic if I could get a little help. I'm stuck at one of the major functions of my program, I'm not very smart.
Thanks.
Post
Fri Sep 01, 2017 2:29 am
#2
Re: C++ Programming
Post your issue.
Can't tell you if we can help until we see the issue.
Can't tell you if we can help until we see the issue.
<Cuisinart8> apparently without the demon driving him around Silver has the intelligence of a botched lobotomy patient ~ Mar 04 2020
console.log(`What's all ${this} ${Date.now()}`);
console.log(`What's all ${this} ${Date.now()}`);
Post
Fri Sep 01, 2017 10:40 am
#3
Re: C++ Programming
Okay, thanks.
So my friend and I are doing a project together for school in C++. We're making a calculator for matrices in math.
For those of you who don't know what it is, it's a two dimensional array that stores numbers and all arithmetic operations except division and the modulus operation can be performed on it. In addition to this, we can find the transpose and inverse. It sounds complicated but it involves pretty simple concepts.
https://www.khanacademy.org/math/precal ... the-matrix
https://www.mathsisfun.com/algebra/matr ... ction.html
http://ibgwww.colorado.edu/~carey/p7291 ... lgebra.pdf
Anyway, we got the operations into the coding which wasn't a huge issue, though the inverse function was HUGE.
As a part of the project we want to store the data inputted by the user and the operations performed as history in a binary file. There are three functions related to this:
1. Storing the history.
2. Viewing the history.
3. Deleting the history.
My friend pretty much gave up on it and I had to rewrite it a million times. The current program has no syntax, type and semantic errors now, but I'm not sure if there are any logical and run time errors. I just want to know if what I've done to save the history of the operations is right and if there are ways by which I can make my program shorter. Also my teacher doesn't like it when we use global variables. I was wondering are there places in my code in which I can avoid/change those. I'm sorry as it's kind of long.
here's the complete code:
https://docs.google.com/document/d/1r0U ... sp=sharing
Here are the basic functions related to the history:
(don't criticize my indentation. I usually use tab spaces but my buddy hates indentation so I had to use 4 spaces. Makes me cringe.)
...that's the easy part. I've got six(?) operations and when those are performed, the history has to be saved. I can't really post the operations individually as there might be huge variations when trying to incorporate it into the actual program so I put the current program above.
Thank you for the help.
So my friend and I are doing a project together for school in C++. We're making a calculator for matrices in math.
For those of you who don't know what it is, it's a two dimensional array that stores numbers and all arithmetic operations except division and the modulus operation can be performed on it. In addition to this, we can find the transpose and inverse. It sounds complicated but it involves pretty simple concepts.
https://www.khanacademy.org/math/precal ... the-matrix
https://www.mathsisfun.com/algebra/matr ... ction.html
http://ibgwww.colorado.edu/~carey/p7291 ... lgebra.pdf
Anyway, we got the operations into the coding which wasn't a huge issue, though the inverse function was HUGE.
As a part of the project we want to store the data inputted by the user and the operations performed as history in a binary file. There are three functions related to this:
1. Storing the history.
2. Viewing the history.
3. Deleting the history.
My friend pretty much gave up on it and I had to rewrite it a million times. The current program has no syntax, type and semantic errors now, but I'm not sure if there are any logical and run time errors. I just want to know if what I've done to save the history of the operations is right and if there are ways by which I can make my program shorter. Also my teacher doesn't like it when we use global variables. I was wondering are there places in my code in which I can avoid/change those. I'm sorry as it's kind of long.
here's the complete code:
https://docs.google.com/document/d/1r0U ... sp=sharing
Here are the basic functions related to the history:
Code: Select all
struct HISTORY
{
int x;
int y;
int z;
int A[20][20];
int B[20][20];
char Operation[20];
int C[20][20];
char type[10];
char defined;
}HIS;
void SaveHistory(HISTORY X)
//This function will save the operations history.
{
ofstream H("HISTORY.DAT", ios::app | ios::binary);
H.write((char*)&X, sizeof(X));
H.close();
}
void DisplayHistory()
//This function will display the history.
{
ifstream I("HISTORY.DAT", ios::in | ios::binary);
HISTORY Y;
int Count = 0;
while(I.read((char*)&Y, sizeof(Y)))
{
Count++;
cout<<"\tOPERATION #"<<Count<<":\n\n";
cout<<"Matrix #1:"<<endl;
DISPLAY(Y.A, Y.x, Y.y);
if(strcmpi(Y.type, "binary"))
{
cout<<"\n\nMatrix #2:"<<endl;
if(strcmpi(Y.Operation, "multiplication"))
DISPLAY(Y.B, Y.y, Y.z);
else
DISPLAY(Y.B, Y.x, Y.y);
}
cout<<"\n\nOperation:"<<Y.Operation;
if(Y.defined != 'N')
{
if(strcmpi(Y.Operation, "multiplication"))
{
cout<<"\n\nResult:"<<endl;
DISPLAY(Y.C, Y.x, Y.z);
}
else
{
cout<<"\n\nResult:"<<endl;
DISPLAY(Y.C, Y.x, Y.y);
}
}
else
cout<<"Not defined."<<endl;
getch();
cout<<"\n\n-----------------------------------------------------------------------------\n\n";
}
I.close();
}
void ClearHistory()
//This one will clear history.
{
/* for this one I think we just have to overwrite the file. yeah*/
ofstream J("HISTORY.DAT", ios::out | ios::binary);
HISTORY Z;
J.write((char*)&Z, sizeof(Z));
J.close();
//still have to test whether this works
}
...that's the easy part. I've got six(?) operations and when those are performed, the history has to be saved. I can't really post the operations individually as there might be huge variations when trying to incorporate it into the actual program so I put the current program above.
Thank you for the help.
Post
Sun Sep 03, 2017 7:03 am
#4
Re: C++ Programming
looking at the einsteinian form of the operations you are supposed to program may gives you insight on how to do them efficiently in code.
because its based around indices which are implicitly iterated over
https://en.wikipedia.org/wiki/Einstein_notation
because its based around indices which are implicitly iterated over
https://en.wikipedia.org/wiki/Einstein_notation
Post
Sun Sep 03, 2017 8:33 pm
#5
Re: C++ Programming
Curious about the Einstein notation here usage in code CF, is it really possible to signify co- and contravariance in code and iterate over the appropriate indices?
Or do you mean in the more general form of tensor notation as opposed to classical vector notation?
edit: OR, do you write all vectors as 2D arrays but make the covariant ones 1xn and contravariant nx1? That makes more sense to me
Or do you mean in the more general form of tensor notation as opposed to classical vector notation?
edit: OR, do you write all vectors as 2D arrays but make the covariant ones 1xn and contravariant nx1? That makes more sense to me
Post
Mon Sep 04, 2017 1:42 am
#6
Re: C++ Programming
I really don't know how Einstein notation can be used here. It's usually used for vectors, right? Those are column/row matrices. But my program deals with fully fledged ones. As far as I know, the input, display and arithmetic functions (addition, multiplication, subtraction) functions are efficient. I'm not sure about the others.
Post
Mon Sep 04, 2017 1:58 am
#7
Re: C++ Programming
Einsteinian notation is for vectors or matrices of arbitrary dimensions. Say you have matric A and B with elements Aij and Bij, then for example multiplication is written as:
(AB)ij = Sum[k = 1 to k = m] ( Aik Bkj )
which is easy to code because Aij =
However, don't think there's an easy way to invert a matrix with that notation.
(AB)ij = Sum[k = 1 to k = m] ( Aik Bkj )
which is easy to code because Aij =
A[i][k]
However, don't think there's an easy way to invert a matrix with that notation.
Warning: do not ask about physics unless you really want to know about physics.
The LT IRC / Alternate link || The REKT Wiki || PUDDING
The LT IRC / Alternate link || The REKT Wiki || PUDDING
Post
Mon Sep 04, 2017 2:23 am
#8
I just thought that the generally iterative structure might give her insights on how to simplify her matrix code
Re: C++ Programming
to be honest i didnt think about any specific uses.Scytale wrote: ↑Sun Sep 03, 2017 8:33 pmCurious about the Einstein notation here usage in code CF, is it really possible to signify co- and contravariance in code and iterate over the appropriate indices?
Or do you mean in the more general form of tensor notation as opposed to classical vector notation?
edit: OR, do you write all vectors as 2D arrays but make the covariant ones 1xn and contravariant nx1? That makes more sense to me
I just thought that the generally iterative structure might give her insights on how to simplify her matrix code
Post
Mon Sep 04, 2017 2:42 am
#9
Re: C++ Programming
Anyhow, actual feedback:
the most glaring 'problem' I see against good coding practice is that your write history function needs to be updated every time you add or change functions. What I would do, as an example, is this:
Make every operation a class, deriving from a base 'Operation' class with functions:
-input()
-result()
-name() and other string functions (for your 'result\n' and 'multiplication' and whatnot strings that depend on the operation)
Then, your history function gets the object for the operation (not knowing which it is) and does the writing, getting the information about the operation from the object and not by dong string compares in a large if-else structure. That way, when adding a new operation, you only have to make the new subclass with correct functions and your history still works.
the most glaring 'problem' I see against good coding practice is that your write history function needs to be updated every time you add or change functions. What I would do, as an example, is this:
Make every operation a class, deriving from a base 'Operation' class with functions:
-input()
-result()
-name() and other string functions (for your 'result\n' and 'multiplication' and whatnot strings that depend on the operation)
Then, your history function gets the object for the operation (not knowing which it is) and does the writing, getting the information about the operation from the object and not by dong string compares in a large if-else structure. That way, when adding a new operation, you only have to make the new subclass with correct functions and your history still works.
Warning: do not ask about physics unless you really want to know about physics.
The LT IRC / Alternate link || The REKT Wiki || PUDDING
The LT IRC / Alternate link || The REKT Wiki || PUDDING
Post
Mon Sep 04, 2017 10:15 am
#10
Re: C++ Programming
Sorry to interject again, but something Dino said jumped out - just on the maths side, I'm curious how you find the inverse of your matrices in your code. Do you use elimination methods? And do you have checks to see whether the given matrix is invertible?
Also just to supplement Dino's comment, Einstein notation (or more generally, summation notation) could be useful for any inner products (i.e. matrix products or dot-products between vectors). Unless you're doing tensor algebra though, involving outer products and the like, it may be best not to worry too much about this. If you go on the wiki page for Einstein notation, ignore the stuff about contra- and covariant arrays or vectors - they're not relevant to what you're trying to do.
Also just to supplement Dino's comment, Einstein notation (or more generally, summation notation) could be useful for any inner products (i.e. matrix products or dot-products between vectors). Unless you're doing tensor algebra though, involving outer products and the like, it may be best not to worry too much about this. If you go on the wiki page for Einstein notation, ignore the stuff about contra- and covariant arrays or vectors - they're not relevant to what you're trying to do.
My bad, I didn't realize you meant implied summation over repeated indicesCornflakes_91 wrote: ↑Mon Sep 04, 2017 2:23 am
to be honest i didnt think about any specific uses.
I just thought that the generally iterative structure might give her insights on how to simplify her matrix code
Post
Tue Sep 05, 2017 6:26 am
#11
Re: C++ Programming
I could also be an idiot and conflate the general index notation and einstein notation and make a fool of myselfScytale wrote: ↑Mon Sep 04, 2017 10:15 amMy bad, I didn't realize you meant implied summation over repeated indicesCornflakes_91 wrote: ↑Mon Sep 04, 2017 2:23 am
to be honest i didnt think about any specific uses.
I just thought that the generally iterative structure might give her insights on how to simplify her matrix code
Post
Tue Sep 05, 2017 10:02 am
#12
Re: C++ Programming
It's all good mah bro! I got fixated on the wrong aspect of it. I think very often the terms are interchangeable anywayCornflakes_91 wrote: ↑Tue Sep 05, 2017 6:26 amI could also be an idiot and conflate the general index notation and einstein notation and make a fool of myselfScytale wrote: ↑Mon Sep 04, 2017 10:15 amMy bad, I didn't realize you meant implied summation over repeated indicesCornflakes_91 wrote: ↑Mon Sep 04, 2017 2:23 am
to be honest i didnt think about any specific uses.
I just thought that the generally iterative structure might give her insights on how to simplify her matrix code