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 tovoid display_opening_logo(); //function declaration for the opening logovoid costMatrix(); //function declaration for cost matrixvoid 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 matrixconst 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]; } } }}

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()

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.

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; } } }}

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

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]; } } }}

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; } } }}

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

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.

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).

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.

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.