Return to “Everything & Anything”

Post

C++ Programming

#1
Hey everyone! :D

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. :P

Thanks. :)
Image
Post

Re: C++ Programming

#3
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:

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
}

(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. :)
Image
Post

Re: C++ Programming

#5
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
Post

Re: C++ Programming

#6
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.
Image
Post

Re: C++ Programming

#7
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 = 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
Image
Post

Re: C++ Programming

#8
Scytale wrote:
Sun Sep 03, 2017 8:33 pm
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
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

Re: C++ Programming

#9
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.
Warning: do not ask about physics unless you really want to know about physics.
The LT IRC / Alternate link || The REKT Wiki || PUDDING
Image
Post

Re: C++ Programming

#10
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.
Cornflakes_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
My bad, I didn't realize you meant implied summation over repeated indices
Post

Re: C++ Programming

#11
Scytale wrote:
Mon Sep 04, 2017 10:15 am
Cornflakes_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
My bad, I didn't realize you meant implied summation over repeated indices
I could also be an idiot and conflate the general index notation and einstein notation and make a fool of myself :oops: :lol:
Post

Re: C++ Programming

#12
Cornflakes_91 wrote:
Tue Sep 05, 2017 6:26 am
Scytale wrote:
Mon Sep 04, 2017 10:15 am
Cornflakes_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
My bad, I didn't realize you meant implied summation over repeated indices
I could also be an idiot and conflate the general index notation and einstein notation and make a fool of myself :oops: :lol:
It's all good mah bro! I got fixated on the wrong aspect of it. I think very often the terms are interchangeable anyway

Online Now

Users browsing this forum: No registered users and 12 guests

cron