🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Bit of code I wrote

Started by
6 comments, last by gone_postal 22 years, 6 months ago
Here is a little bit of code that I wrote, and would like some comments on... I've been programming C++ now for about 3 or 4 weeks...
          
// Trying.cpp : Calculates determinant of a matrix, either

// 2x2 or 3x3


#include "stdafx.h"
#include <iostream>

double twoByTwo();
double threeByThree();

bool quit = false;

int main(int argc, char* argv[])
{
	bool done = false;  //quit loop and variable.  If false, program will continue to run

	while (done == false)
	{
		using namespace std;
		//making menu

		cout << "               ***MAIN MENU***";
		cout << "\n  (1)Calculate a two by two determinant";
		cout << "\n  (2)Calculate a three by three determinant";
		cout << "\n  (3)Quit\n";
		int menuChoice; //choice from menu

		cin >> menuChoice;
		double detd;
		switch (menuChoice)//Going to specialized functions or quitting

		{
			case 1:		
				detd = twoByTwo();
				cout << "The determinant is equal to: " << detd << "\n\n";
				break;
			case 2:		
				detd = threeByThree();
				cout << "The determinant is equal to: " << detd << "\n\n";
				break;
			case 3:		
				char exit;
				do
				{
					cout << "Are you sure you want to quit?  Y for yes or N for no:\n";
					cin >> exit;
					switch (exit)
					{
					case 'y':
					case 'Y':
						done = true;
						quit = true;
						break;
					case 'n':
					case 'N':
						done = false;
						quit = true;
						break;
					default:
						cout << "Enter y or n to exit.\n";
						break;
					}
				} while (quit == false);
				break;
			default:	
				cout << "Try again, please.\n\n";
				break;
		}
	}
		return 0;
}


double twoByTwo{
	using namespace std;
	float determinant[2][2];
	cout << "n[x][y] represents the x row and the y column.\n";
	for (int i=0; i<2; i++)
	{
		for (int j=0; j<2; j++)
		{
			cout << "Enter n[" << i+1 << "][" << j+1 << "]:  ";
			cin >> determinant[i][j];
		}
	}
	cout << endl << "The determinant that you entered is:" << endl;
	for (i=0; i<2; i++)
	{
		for (int j=0; j<2; j++)
		{
			cout << determinant[i][j];
			cout << "\t";
		}
		cout << endl;
	}
	cout << endl << endl;
	double det = (determinant[0][0]*determinant[1][1])-(determinant[0][1]*determinant[1][0]);
	return det;
}

double threeByThree()//to calculate determinant of a three by three

{
	using namespace std;
	float determinant[3][3];
	cout << "n[x][y] represents the x row and the y column.\n";
	for (int i=0; i<3; i++)
	{
		for (int j=0; j<3; j++)
		{
			cout << "Enter n[" << i+1 << "][" << j+1 << "]:  ";
			cin >> determinant[i][j];
		}
	}
	cout << endl << "The determinant that you entered is:" << endl;
	for (i=0; i<3; i++)
	{
		for (int j=0; j<3; j++)
		{
			cout << determinant[i][j];
			cout << "\t";
		}
		cout << endl;
	}
	cout << endl << endl;
	double det = ((determinant[0][0]*determinant[1][1]*determinant[2][2])
+(determinant[0][1]*determinant[1][2]*determinant[2][0])
+(determinant[0][2]*determinant[1][0]*determinant[2][1]))
-((determinant[0][2]*determinant[1][1]*determinant[2][0])
+(determinant[0][1]*determinant[1][0]*determinant[2][2])
+(determinant[0][0]*determinant[1][2]*determinant[2][1]));
	return det;
}

        
Thanx for any comments u may have... Edited by - gone_postal on December 2, 2001 10:47:19 PM Edited by - gone_postal on December 2, 2001 12:11:30 AM Edited by - gone_postal on December 2, 2001 12:22:33 AM
Advertisement
Whoah... I didn''t realize that it went over so far to the left... sorry :{
d''oh!

That just shows how stupid I am...
Good work ! Your code is clear, simple and seems to work (I didn''t try to compile it)

Just a few ideas (from simple to more complicated):

  1. The names of your functions should be more representative like Determinant2x2() and Determinant3x3(). This way you won''t need the extra comment (//to calculate...)

  2. You can make 2 consecutive case in a switch statement do the same thing:
    switch (exit){   case ''y'':   case ''Y'':      done = true;      quit = true;      break;   case ''n'':   case ''N'':      done = false;      quit = true;      break;   default:      cout << "Enter y or n to exit.\n";      break;}

  3. You can split instructions on multiple lines. That''s the purpose of the semicolon. You can change line (without a semicolon) and it''s just like spaces, like this:
    double det = ( (matrix[0][0] * matrix[1][1] * matrix[2][2])             + (matrix[0][1] * matrix[1][2] * matrix[2][0])             + (matrix[0][2] * matrix[1][0] * matrix[2][1]) )           - ( (matrix[0][2] * matrix[1][1] * matrix[2][0])             + (matrix[0][1] * matrix[1][0] * matrix[2][2])             + (matrix[0][0] * matrix[1][2] * matrix[2][1]) );

    I changed the "determinant" name because this array is not the determinant! It''s the matrix!

  4. You should separate your functions into input and calculation functions. This way, you could build a matrix and after decide to calculate the product or any other function without copying the input code. To do this you need to pass the matrix as a function argument, like this:
    double InputMatrix2x2(double matrix[2][2]);double Determinant2x2(double matrix[2][2]);

    With this way, you must declare the array in the main function.


I have other ideas but I would like to know what you think of these ?

alexk7@alexk7.com
alexk7
Do you want to edit your post to add extra returns in your code ?
That could maybe make my own post readable...
alexk7
Thanks a lot Alex, I "knew" some things that you said but now it really makes more sense--like how if I don''t say break after a case in a switch statement, I can make multiple cases do the same thing!

I''ll edit my post now...

Anyway, thanks a lot for the great suggestions!
Wait a minute, alex, for the Input and Output functions, shouldn''t the Input one have no input value?
You can''t have a complete matrix (represented as an array of arrays) as the return value of a function.
But when you pass an array as a function argument, it''s always passed by reference. I mean that if you change it, it''s changed in the calling function too. That''s why I said you must declare the matrix in your main function.
My InputMatrix function receives an uninitialized matrix as parameter and fills it with new numbers. When the function returns, the matrix in main() is filled with numbers!

In general, if you want to pass variables by reference, you must add a ''&'' sign after the type. But arrays are automatically passed by reference.

(In practice, the name of an array is a pointer but you don''t have to understand pointers to use them...)
alexk7

This topic is closed to new replies.

Advertisement