using switch statements

So my task was to create a program that calculates the determinant of a 2x2, 3x3, and 4x4 matrix. My first step was to create a library of functions for calculating the three determinants.

There was actually specific directions on how to create these functions, ie: a 4x4 matrix using cofactor exp would calculate three 3x3 determinants (then using the function for the 3x3 det). By hand, I had written it out but could not quite get it to work. So just ended up hard-coding everything to go in my cpp library. http://codepad.org/ZmIfmECy

Next onto the program. I am using a switch statement for each of the three cases (2x2,3x3,4x4). I ran a test with only one switch to make sure my functions were producing the correct values for determinants. Then proceeded to add breaks to subsequent cases.

This is pretty much where I am stuck now, getting various errors http://i.imgur.com/G6lvw.png in debugging (using Xcode).

Lastly after debugging, the plan is to add a do/while loop to the entire thing to allow multiple runs at different cases?

Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* -------------------------------------------------------*/
/*	         Determinant Project						  */
/*														  */
/*														  */
/*													      */

#include <iostream>	//	Required for cout. cin. endl.
#include <cmath>	//	Required for sqrt()
#include <iomanip>
#include "detfunc.h" // Required for Determinant functions.

using namespace std;

int main ()

{
	int N;
	double array4by4[4][4], array3by3[3][3], array2by2[2][2], det4, det3, det2;

	cout <<"Enter size of array (4,3, or 2): "<<endl;
	cin >> N;
	switch (N)
	{
		case 4:
			for(int i=0; i<4; i++)
		{
			for(int j=0; j<4; j++)
			{
				cout<<"Enter Coefficient for row " <<i<< " column " <<j<<endl;
				cin>> array4by4[i][j];
			}
		}
			det4 = det4x4(array4by4);
			cout <<"Determinant: " <<det4<<endl;
		break;
		case 3:
			for(int i=0; i<3; i++)
		{
			for(int j=0; j<3; j++)
				cout<<"Enter Coefficient for row " <<i<< " column " <<j<<endl;
				cin>>array3by3[i][j];
			}
		}
			det3 = det3x3(array3by3);
			cout <<"Determinant: " <<det3<<endl;
		break;
		case 2:
			for(int i=0; i<2; i++)
		{
			for(int j=0; j<2; j++)
				cout<<"Enter Coefficient for row "<<i<< " column " <<j<<endl;
				cin>>array2by2[i][j];
		}
			det2 = det2x2(array2by2);
			cout <<"Determinant: " <<det2<<endl;

	return 0;
	
}
	
	
	
	
You forgot four brackets... lines 40, 51, 55 and 56. Also, there is no break for your case 2: statement. Hope that helps.
Thanks.. Tried going back and noticed the missing brackets. Now line 32 and 46 say
Case label 2/3 not within a switch statement.
While 43 and 47 [quote]break statement not within loop or switch.

/edit got it. Need to look over a little more closely on the brackets. Thanks.

So a do/while within the switch should allow me to run multiple iterations of the code right?
Last edited on
Topic archived. No new replies allowed.