Solved

C++ Help with Bubble Sort

Posted on 2014-04-22
9
282 Views
Last Modified: 2014-04-23
hello, I need help with a bubble sort for my V_prime_matrix. I watched a youtube video of how it is done, but I am getting errors. This may be due to becasue I have the sorting function outside of int main.

Can anyone help me?


#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <conio.h>
#include <fstream>
using namespace std;

ofstream output_file; //declare file to output results to

void display_opening_logo(); //function declaration for the opening logo
void costMatrix();			 //function declaration for cost matrix
void construct_V_prime_matrix();
void sorted_V_prime_matrix();
void construct_d_prime_matrix();
float f_star();
void calculateVmatrix();    //function declaration for the to-be calculated V matrix
const char SKIP = ' ';

int C_matrix[4][4] = { 0 };
int V_matrix[4][4] = { 0 };
int f_bar[1][1] = { 0 };
int V_prime_matrix[6][1] = { 0 };
int d_prime_matrix[1][6] = { 0 };

int d_matrix[4][4] = { { 0, 8, 10, 2 },
					   { 8, 0, 4, 7 },
					   { 10, 4, 0, 9 },
				       { 2, 7, 9, 0 } };

int p_matrix[4][4] = { { 0, 4, 2, 3 },
					   { 4, 0, 8, 27 },
					   { 2, 8, 0, 30 },
					   { 3, 27, 30, 0 } };

double b_matrix[4][4] = { { 0, 0.5, 4, 1 },
						  { 0.5, 0, 0.5, 0.33333333333333333 },
					      { 4, 0.5, 0, 0.1666666667 },
						  { 1, 0.3333333333333333, 0.1666666667, 0 } };



int main()
{
	int i, j, x, k;

	display_opening_logo();											//function call for the opening logo
	cout << setw(16) << SKIP << endl;
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	_getch();

	cout << setw(65) << "This is the P matrix. It represents the number of trips " << endl;
	cout << setw(50) << "between departmnts i and k." << endl;
	
	for (i = 0; i < 4; i++)   
	{
		for (int j = 0; j < 4; j++)
		cout << p_matrix[i][j] << " "; 
		cout << endl;
	}

	cout << setw(16) << SKIP << endl;
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	_getch();
	cout << setw(16) << SKIP << endl;
	
	cout << setw(65) << "This is the b matrix. It represents the cost of moving " << endl;
	cout << setw(65) << "one unit of material between departments i and k through " << endl;
	cout << setw(50) << "a distance of one unit" << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			cout << b_matrix[i][j] << " ";
		cout << endl;
	}

	cout << setw(16) << SKIP << endl;
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	_getch();
	cout << setw(16) << SKIP << endl;

	cout << setw(65) << "This is the d matrix. It represents the distance " << endl;
	cout << setw(50) << "between sites j and l." << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			cout << d_matrix[i][j] << " ";
		cout << endl;
	}

	cout << setw(16) << SKIP << endl;
	calculateVmatrix();									//function call for the calculated V matrix
	costMatrix();										//function call for the cost matrix
	float cost_of_original_layout = f_star();
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	char ch = _getch();
	cout << "The cost of the original existing layout is " << "$" << fixed << showpoint << setprecision(2) << cost_of_original_layout << endl;
	


	construct_V_prime_matrix();
	sorted_V_prime_matrix();
	for (int index = 0; index < 6; index++)
	{
		cout << V_prime_matrix[index] << " ";
		cout << endl;
	}

	
	output_file.open("CN_JH_VM_EO_CS_ET.txt"); //opens file to recieve data
	output_file << "************************************************************************** " << endl;
	output_file << "*" << setw(33) << SKIP << "WELCOME" << setw(33) << "*" << endl;
	output_file << "*" << setw(73) << "*" << endl;
	output_file << "*" << setw(16) << SKIP << "To the Qudratic Assignment Problem Calculator" << setw(12) << "*" << endl;
	output_file << "************************************************************************** " << endl;
	output_file << setw(16) << SKIP << endl;
	output_file << "*" << setw(16) << SKIP << "For IEGR 468: Advanced Material Handling Systems" << setw(9) << "*" << endl;
	output_file << setw(16) << SKIP << endl;
	output_file << setw(28) << SKIP << "By:Jeffrey Haley" << endl;
	output_file << setw(28) << SKIP << "   Vaughn Mason" << endl;
	output_file << setw(28) << SKIP << "   Carlos Neal" << endl;
	output_file << setw(28) << SKIP << "   Emmanuel Onafeko" << endl;
	output_file << setw(28) << SKIP << "   Christina Speaks" << endl;
	output_file << setw(28) << SKIP << "   Elvis Tangwan" << endl;
	output_file << setw(16) << SKIP << endl;
	output_file << "This calculator will find the minimum material handling cost required for " << endl;
	output_file << setw(60) << "a layout given the initial deparments and sites." << endl;
	output_file << "The cost of the original existing layout is " << "$" << fixed << showpoint << setprecision(2) << cost_of_original_layout << endl;
	
	output_file << setw(65) << "This is the P matrix. It represents the number of trips " << endl;
	output_file << setw(50) << "between departmnts i and k." << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			output_file << p_matrix[i][j] << " ";
		output_file << endl;
	}
	
	output_file << setw(65) << "This is the b matrix. It represents the cost of moving " << endl;
	output_file << setw(65) << "one unit of material between departments i and k through " << endl;
	output_file << setw(50) << "a distance of one unit" << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			output_file << b_matrix[i][j] << " ";
		output_file << endl;
	}

	output_file << setw(65) << "This is the d matrix. It represents the distance " << endl;
	output_file << setw(50) << "between sites j and l." << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			output_file << d_matrix[i][j] << " ";
		output_file << endl;
	}

	output_file << "The cost of the original existing layout is " << "$" << fixed << showpoint << setprecision(2) << cost_of_original_layout << endl;

	output_file.close(); //closes file

	system("PAUSE");
	return 0;

}

void display_opening_logo()  // function for the opening logo
{
	cout << "************************************************************************** " << endl;
	cout << "*" << setw(33) << SKIP << "WELCOME" << setw(33) << "*" << endl;
	cout << "*" << setw(73) << "*" << endl;
	cout << "*" << setw(16) << SKIP << "To the Qudratic Assignment Problem Calculator" << setw(12) << "*" << endl;
	cout << "************************************************************************** " << endl;
	cout << setw(16) << SKIP << endl;
	cout << "*" << setw(16) << SKIP << "For IEGR 468: Advanced Material Handling Systems" << setw(9) << "*" << endl;
	cout << setw(16) << SKIP << endl;
	cout << setw(28) << SKIP << "By:Jeffrey Haley" << endl;
	cout << setw(28) << SKIP << "   Vaughn Mason" << endl;
	cout << setw(28) << SKIP << "   Carlos Neal" << endl;
	cout << setw(28) << SKIP << "   Emmanuel Onafeko" << endl;
	cout << setw(28) << SKIP << "   Christina Speaks" << endl;
	cout << setw(28) << SKIP << "   Elvis Tangwan" << endl;
	cout << setw(16) << SKIP << endl;
	cout << "This calculator will find the minimum material handling cost required for " << endl;
	cout << setw(60) << "a layout given the initial deparments and sites." << endl;
	
}

void calculateVmatrix()			// calculate the V matrix using the b and p matrices
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			V_matrix[i][j] = b_matrix[i][j] * p_matrix[i][j];
		}
	}
}

void costMatrix()			//calculates the cost matrix from the V and d matrices
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			C_matrix[i][j] = V_matrix[i][j] * d_matrix[i][j];
		}
	}
}

float f_star()   //calculates the f_star from the upper diagonal half of the cost matrix, and calls it the og_layout_cost
{
	int og_layout_cost = 0;
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			if (i == j || j > i)
			{
				og_layout_cost += C_matrix[i][j];
			}
		}
	}
	return og_layout_cost;
}

void construct_V_prime_matrix()   //calculates the f_star from the upper diagonal half of the cost matrix, and calls it the og_layout_cost
{
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			if (i < j || j > i)
			{
				V_prime_matrix[i][j] = V_matrix[i][j];
			}
		}
	}
}

void sorted_V_prime_matrix()
{
	
	for (int counter = 1; counter < 6; counter++)
	{
		for (int index = 0; index < 6; index++)
		{
			if (V_prime_matrix[index] > V_prime_matrix[index + 1])
			{
				int swapholder;
				swapholder = V_prime_matrix[index];
				V_prime_matrix[index] = V_prime_matrix[index + 1];
				V_prime_matrix[index + 1] = swapholder;
			}
		}
	}
}



void construct_d_prime_matrix()   //calculates the f_star from the upper diagonal half of the cost matrix, and calls it the og_layout_cost
{
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			if (j > i)
			{
				d_prime_matrix[i][j] = d_matrix[i][j];
			}
		}
	}
}

Open in new window

0
Comment
Question by:college_student
  • 4
  • 4
9 Comments
 
LVL 33

Expert Comment

by:sarabande
ID: 40016904
if (i < j || j > i)

the condition is weird cause i<j and j>i is the same. therefore the initialization of the matrix is incomplete (it fills only the right-upper triangle of the matrix).

generally indexing is wrong. if you have a 4x4 matrix loops may not run from 1 to less than 6.

regarding bubble sort:
it is not quite clear how you want the matrix values to be sorted. you could have (only) the columns sorted or the rows. if that is the case you need a sort for each row (column) separately.

or you may want to have all values sorted where again there are alternatives to do it horizontally like

0 1 2 3
4 5 6 .
...
 
or vertically

0 4 ..
1 5 ..
2 6 ..
3 ...

or you even could sort it diagonally like:

0 1 3 6
2 4 7 .
5 8 ..
....

if you know the sort direction you would need a new index which runs from 0 to 15 and where you calculate a mapping to the corresponding index pair depending how you want to sort. for example if you want to sort all values of the matrix by rows (horizontally) you would have x = n/4 and y = n%4 to get an index pair [x][y] from n.

the bubble sort has an outer loop where n runs from 0 to 14 (<15) and an inner loop where m runs from n+1 to 15 (< 16). in the inner loop you calculate both matrix pairs (x1, y1) and (x2, y2) from n and m and compare the values. if they have wrong direction you make a swap on the values.

Sara
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 40017382
int V_prime_matrix[6][1] = { 0 };
int d_prime_matrix[1][6] = { 0 };

Those two lines of code are 'weird' because why are you declaring a 2-dimensional array, yet then go and set the size of one of the 2-dimensions to 1.  That's effectively a 1-dimensional array, BUT every time you use the array, you have to use BOTH dimensions...

...and that's the problem I found in compiling your code.  When you access V_prime_matrix, you've got to use two sets of brackets '[ ]' because you've declared the variables as two dimensions.

Now I don't know if the program is executing correctly, but the following code change will allow your program to compile and run:
void sorted_V_prime_matrix()
{
    for (int counter = 1; counter < 6; counter++)
    {
        for (int index = 0; index < 6; index++)
        {
            if (V_prime_matrix[index] > V_prime_matrix[index + 1])
            {
                int swapholder;
                swapholder = V_prime_matrix[index][0];
                V_prime_matrix[index][0] = V_prime_matrix[index + 1][0];
                V_prime_matrix[index + 1][0] = swapholder;
            }
        }
    }
}

Open in new window

0
 

Author Comment

by:college_student
ID: 40017402
Thank you both. the prime matrices are supposed to take the 6 values in the upper diagonal half of the d and V matrices and sort them in ascending order.

The code you posted has allowed my program to compile and run (thanks) but it has not sorted the values
0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 40017513
I'm still finding two issues.

The 1st relates to V_prime_matrix being declared to 2-D matrix, yet you only need a 1-D matrix.  The result is that your construct_V_prime_matrix isn't loading this 1-D declared as a 2-D matrix correctly.

Here is a hack that (assuming the rest of the logic is correct) will properly load this 1-D/2-D matrix:
void construct_V_prime_matrix()   //calculates the f_star from the upper diagonal half of the cost matrix, and calls it the og_layout_cost
{
	int x=0;
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			if (i < j || j > i)
			{
				V_prime_matrix[x++][0] = V_matrix[i][j];
			}
		}
	}
}

Open in new window

As you can see not, the 2nd dimension of the V_prime_matrix is not properly hardcoded to [0].  I then add an 'x' variable that will load the 6 indexes of V_prime_matrix properly.

The second is that once I hard-coded the 2nd index to [0], the sort algorithm wasn't working correctly... specifically the limits on the counters of the inner loop and outer loop were sort of swapped... the inner loop needs to only go from 0 to 4 because the logic attempts to access element 'index+1'.  By having the inner loop go all the way to 5, you were accessing element '5+1', which is actually 1 element BEYOND the matrix.  So by making the inner loop count 0 to 4 and the outer loop count 0 to 5 (or 1 to 6), then the bubble sort works correctly.
void sorted_V_prime_matrix()
{
	
	for (int counter = 0; counter < 6; counter++)
	{
		for (int index = 0; index < 5; index++)
		{
			if (V_prime_matrix[index][0] > V_prime_matrix[index + 1][0])
			{
				int swapholder;
				swapholder = V_prime_matrix[index][0];
				V_prime_matrix[index][0] = V_prime_matrix[index + 1][0];
				V_prime_matrix[index + 1][0] = swapholder;
			}
		}
	}
}

Open in new window


From what I can tell, the sum total of your issues simply go back to that 2-D matrix that you really only need to be 1-D.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:college_student
ID: 40017549
thanks for that, i understand what your saying the v prime matrix should only be 1D not 2d, my professor just told me to do it that way

also, how would I call the sorted v prime matrix in int main becasue how i have it now, it is not coming out right

construct_V_prime_matrix();
	for (i = 0; i < 1; i++)
	{
		for (int j = 0; j < 6; j++)
			cout << V_prime_matrix[i][j] << " ";
		cout << endl;
	}

Open in new window

0
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 40017614
Notice that in all the code changes I've shown, V_prime_matrix is ALWASY accessed
V_prime_matrix[i][0]

Open in new window

This is because you declared the 2nd dimension to size 1.

But in your code sample above, you are incrementing j, and using that as the 2nd index.

So the cleanest solution would be a for loop with i going from 0 to 5 and accessing V_prime_matrix just like in the code sample above (2nd dimension hard-coded to 0).

But I can understand wanting to have two for loops since your array has two dimensions.  In that case, you just need to swap the index i to go from 0 to 5 (the range for the 1st dimension) and the index j to go from 0 to 0 (the range for the 2nd dimension).
0
 

Author Comment

by:college_student
ID: 40017644
thanks, it does display the prime matrix to the screen now. but it is still not sorted, im not sure what im doing wrong

#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <conio.h>
#include <fstream>
using namespace std;

ofstream output_file; //declare file to output results to

void display_opening_logo(); //function declaration for the opening logo
void costMatrix();			 //function declaration for cost matrix
void construct_V_prime_matrix();
void sorted_V_prime_matrix();
void construct_d_prime_matrix();
float f_star();
void calculateVmatrix();    //function declaration for the to-be calculated V matrix
const char SKIP = ' ';

int C_matrix[4][4] = { 0 };
int V_matrix[4][4] = { 0 };
int f_bar[1][1] = { 0 };
int V_prime_matrix[1][6] = { 0 };
int d_prime_matrix[1][6] = { 0 };

int d_matrix[4][4] = { { 0, 8, 10, 2 },
					   { 8, 0, 4, 7 },
					   { 10, 4, 0, 9 },
				       { 2, 7, 9, 0 } };

int p_matrix[4][4] = { { 0, 4, 2, 3 },
					   { 4, 0, 8, 27 },
					   { 2, 8, 0, 30 },
					   { 3, 27, 30, 0 } };

double b_matrix[4][4] = { { 0, 0.5, 4, 1 },
						  { 0.5, 0, 0.5, 0.33333333333333333 },
					      { 4, 0.5, 0, 0.1666666667 },
						  { 1, 0.3333333333333333, 0.1666666667, 0 } };



int main()
{
	int i, j, x, k;

	display_opening_logo();											//function call for the opening logo
	cout << setw(16) << SKIP << endl;
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	_getch();

	cout << setw(65) << "This is the P matrix. It represents the number of trips " << endl;
	cout << setw(50) << "between departmnts i and k." << endl;
	
	for (i = 0; i < 4; i++)   
	{
		for (int j = 0; j < 4; j++)
		cout << p_matrix[i][j] << " "; 
		cout << endl;
	}

	cout << setw(16) << SKIP << endl;
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	_getch();
	cout << setw(16) << SKIP << endl;
	
	cout << setw(65) << "This is the b matrix. It represents the cost of moving " << endl;
	cout << setw(65) << "one unit of material between departments i and k through " << endl;
	cout << setw(50) << "a distance of one unit" << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			cout << b_matrix[i][j] << " ";
		cout << endl;
	}

	cout << setw(16) << SKIP << endl;
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	_getch();
	cout << setw(16) << SKIP << endl;

	cout << setw(65) << "This is the d matrix. It represents the distance " << endl;
	cout << setw(50) << "between sites j and l." << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			cout << d_matrix[i][j] << " ";
		cout << endl;
	}

	cout << setw(16) << SKIP << endl;
	calculateVmatrix();									//function call for the calculated V matrix
	costMatrix();										//function call for the cost matrix
	float cost_of_original_layout = f_star();
	cout << "Press <Enter> to continue " << endl;
	cout << setw(16) << SKIP << endl;
	char ch = _getch();
	cout << "The cost of the original existing layout is " << "$" << fixed << showpoint << setprecision(2) << cost_of_original_layout << endl;
	
	construct_V_prime_matrix();
	for (i = 0; i < 6; i++)
	{
		for (int j = 0; j < 1; j++)
			cout << V_prime_matrix[i][0] << " ";
		cout << endl;
	}

	

	
	output_file.open("CN_JH_VM_EO_CS_ET.txt"); //opens file to recieve data
	output_file << "************************************************************************** " << endl;
	output_file << "*" << setw(33) << SKIP << "WELCOME" << setw(33) << "*" << endl;
	output_file << "*" << setw(73) << "*" << endl;
	output_file << "*" << setw(16) << SKIP << "To the Qudratic Assignment Problem Calculator" << setw(12) << "*" << endl;
	output_file << "************************************************************************** " << endl;
	output_file << setw(16) << SKIP << endl;
	output_file << "*" << setw(16) << SKIP << "For IEGR 468: Advanced Material Handling Systems" << setw(9) << "*" << endl;
	output_file << setw(16) << SKIP << endl;
	output_file << setw(28) << SKIP << "By:Jeffrey Haley" << endl;
	output_file << setw(28) << SKIP << "   Vaughn Mason" << endl;
	output_file << setw(28) << SKIP << "   Carlos Neal" << endl;
	output_file << setw(28) << SKIP << "   Emmanuel Onafeko" << endl;
	output_file << setw(28) << SKIP << "   Christina Speaks" << endl;
	output_file << setw(28) << SKIP << "   Elvis Tangwan" << endl;
	output_file << setw(16) << SKIP << endl;
	output_file << "This calculator will find the minimum material handling cost required for " << endl;
	output_file << setw(60) << "a layout given the initial deparments and sites." << endl;
	output_file << "The cost of the original existing layout is " << "$" << fixed << showpoint << setprecision(2) << cost_of_original_layout << endl;
	
	output_file << setw(65) << "This is the P matrix. It represents the number of trips " << endl;
	output_file << setw(50) << "between departmnts i and k." << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			output_file << p_matrix[i][j] << " ";
		output_file << endl;
	}
	
	output_file << setw(65) << "This is the b matrix. It represents the cost of moving " << endl;
	output_file << setw(65) << "one unit of material between departments i and k through " << endl;
	output_file << setw(50) << "a distance of one unit" << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			output_file << b_matrix[i][j] << " ";
		output_file << endl;
	}

	output_file << setw(65) << "This is the d matrix. It represents the distance " << endl;
	output_file << setw(50) << "between sites j and l." << endl;

	for (i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
			output_file << d_matrix[i][j] << " ";
		output_file << endl;
	}

	output_file << "The cost of the original existing layout is " << "$" << fixed << showpoint << setprecision(2) << cost_of_original_layout << endl;

	output_file.close(); //closes file

	system("PAUSE");
	return 0;

}

void display_opening_logo()  // function for the opening logo
{
	cout << "************************************************************************** " << endl;
	cout << "*" << setw(33) << SKIP << "WELCOME" << setw(33) << "*" << endl;
	cout << "*" << setw(73) << "*" << endl;
	cout << "*" << setw(16) << SKIP << "To the Qudratic Assignment Problem Calculator" << setw(12) << "*" << endl;
	cout << "************************************************************************** " << endl;
	cout << setw(16) << SKIP << endl;
	cout << "*" << setw(16) << SKIP << "For IEGR 468: Advanced Material Handling Systems" << setw(9) << "*" << endl;
	cout << setw(16) << SKIP << endl;
	cout << setw(28) << SKIP << "By:Jeffrey Haley" << endl;
	cout << setw(28) << SKIP << "   Vaughn Mason" << endl;
	cout << setw(28) << SKIP << "   Carlos Neal" << endl;
	cout << setw(28) << SKIP << "   Emmanuel Onafeko" << endl;
	cout << setw(28) << SKIP << "   Christina Speaks" << endl;
	cout << setw(28) << SKIP << "   Elvis Tangwan" << endl;
	cout << setw(16) << SKIP << endl;
	cout << "This calculator will find the minimum material handling cost required for " << endl;
	cout << setw(60) << "a layout given the initial deparments and sites." << endl;
	
}



void calculateVmatrix()			// calculate the V matrix using the b and p matrices
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			V_matrix[i][j] = b_matrix[i][j] * p_matrix[i][j];
		}
	}
}

void costMatrix()			//calculates the cost matrix from the V and d matrices
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			C_matrix[i][j] = V_matrix[i][j] * d_matrix[i][j];
		}
	}
}

float f_star()   //calculates the f_star from the upper diagonal half of the cost matrix, and calls it the og_layout_cost
{
	int og_layout_cost = 0;
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			if (i == j || j > i)
			{
				og_layout_cost += C_matrix[i][j];
			}
		}
	}
	return og_layout_cost;
}

void sorted_V_prime_matrix()
{
	for (int counter = 0; counter < 6; counter++)
	{
		for (int index = 0; index < 5; index++)
		{
			if (V_prime_matrix[index][0] > V_prime_matrix[index + 1][0])
			{
				int swapholder;
				swapholder = V_prime_matrix[index][0];
				V_prime_matrix[index][0] = V_prime_matrix[index + 1][0];
				V_prime_matrix[index + 1][0] = swapholder;
			}
		}
	}
}

void construct_V_prime_matrix()   //calculates the f_star from the upper diagonal half of the cost matrix, and calls it the og_layout_cost
{
	int x = 0;
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
			{
				if (i < j || j > i)
			{
				V_prime_matrix[x++][0] = V_matrix[i][j];
			}
		}
	}
}


void construct_d_prime_matrix()   //calculates the f_star from the upper diagonal half of the cost matrix, and calls it the og_layout_cost
{
	for (int i = 0; i<4; i++)
	{
		for (int j = 0; j<4; j++)
		{
			if (j > i)
			{
				d_prime_matrix[i][j] = d_matrix[i][j];
			}
		}
	}
}

Open in new window

0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 40017802
You've dropped the call to sorted_V_prime_matrix().

Your original code called this function on line 107 just after the call to construct_V_prime_matrix();

Your new code has construct_V_prime_matrix() at line 104, but no call to sorted_V_prime_matrix().
0
 

Author Closing Comment

by:college_student
ID: 40017812
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now