![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
2d array
Does anyone know how to take a 2d array and go to a coordinate like
matrix[row][column] and compare each value of the columns in that row and then takes the smallest value and then goes to that columns row and does it again. So it would start by saying go to matrix[row][column] find the smallest value in the row and then go to matrix[column containing the smallest value is the new row][column] and compare each value across that row? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Yeah just loop through and compare the array.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
when i reference the 2d array i have to give it a row and a column but that would give me the value that is at that spot i want the column that has that value and then make that column the next row.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Show us some code of what you mean.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
well i have a whole program ill post the whole thing. I have a 2d array that represents a graph. The user enters a a starting vertex (the row), ending vertex (the column) and weight all as numbers. it goes to the spot in the 2d array and puts the weight number there. This keeps going until the user enters -1 for all of them. Then it asks for a vertex to start on which would be the row and then it looks through that row and finds the smallest value. It then has to add just the row to a finishedVerteices array and then adds the smallest weight to a finishedWeight array and then makes the column it found it at the next row. Then it searches that row and keeps going until the size of the array is met. I also have to have some checks. I have to check to make sure the vertex is finds that has the smallest is not already in the finishedVertices array. I also have to adjust the weights if the next vertex has a better path to a weight. The whole thing should work except the prim function. That is the function that does the searching and adding to the other arrays. And i need a function that will print the arrays also. I have an attempt at this but can not test it because the prim function doesn't work.
Here is the code: #include <iostream>
#include<math.h>
#import<string.h>
using namespace std;
int matrix[50][50];
int finishedWeight[50];
int finishedVertices[50];
char* edges[50];
int counter = 0;
int size = 0;
//fill the matrix with a big number
void fillMatrix(int vertexNum)
{
for(int i = 0; i < vertexNum; i++)
{
for(int j = 0; j < vertexNum; j++)
{
matrix[i][j] = pow(2,32) - 1;
}
}
}
//set the weight of the vertices
void insert(int start, int end, int weight)
{
matrix[start][end] = weight;
}
//add the edge for an undirected graph
void addUndirected(int vertexNum)
{
int start = 0;
int end = 0;
int weight;
size = vertexNum;
fillMatrix(vertexNum);
while(start != -1)
{
cout << "Please enter a starting vertex: ";
cin >> start;
cout << "Please enter an ending vertex: ";
cin >> end;
cout << "Please enter a weight: ";
cin >> weight;
insert(start, end, weight);
insert(end, start, weight);
}
for(int i = 0; i < vertexNum; i++)
{
for(int j = 0; j < vertexNum; j++)
{
cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << endl;
}
}
}
// add the edge for a directed graph
void addDirected(int vertexNum)
{
int start = 0;
int end = 0;
int weight;
size = vertexNum;
fillMatrix(vertexNum);
while(start != -1)
{
cout << "Please enter a starting vertex: ";
cin >> start;
cout << "Please enter an ending vertex: ";
cin >> end;
cout << "Please enter a weight: ";
cin >> weight;
insert(start, end, weight);
}
for(int i = 0; i < vertexNum; i++)
{
for(int j = 0; j < vertexNum; j++)
{
cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << endl;
}
}
}
//execute the algorithm
void prim(int startingVertex)
{
cout << "Size = " << size << endl;
int start = startingVertex;
int end;
int weight;
int minx;
int miny;
int minWeight = pow(2, 32) - 1;
for(int k = 0; k < size; k++)
{
finishedVertices[k] = start;
for(int l = 0; l < size; l++)
{
if(matrix[start][l] < minWeight)
{
for(int i = 0; i < size; i++)
{
if(matrix[start][l] != finishedVertices[i])
{
minx = start;
miny = l;
minWeight = matrix[minx][miny];
}
}
}
}
}
finishedVertices[counter] = minx;
cout << "counter = " << counter << endl;
cout << "finished vertices at 0 = " << finishedVertices[0] << endl;
cout << "finished vertices at 1 = " << finishedVertices[1] << endl;
finishedWeight[counter] = minWeight;
cout << "FinishedWeight at 0 = " << finishedWeight[0] << endl;
cout << "finishedWeight at 1 = " << finishedWeight[1] << endl;
//edges[counter] = minx + ',' + miny;
counter++;
if(counter != size)
prim(2); // this is just to test to see if another vertex would work
// really the parameter for this should be the column the lowest
// weight was in
}
void display()
{
for(int v = 0; v < size; v++)
{
cout << finishedVertices[v];
for(int w = 0; w < size; w++)
{
cout << finishedWeight[w];
}
}
}
// run functions and prompts
int main()
{
int option = 0; // option for starting vertex for algorithm
int option1 = 0; // option for directed or undirected
int option2; // option to see if the algorithm is run again
int vertexNum = 0;
while(option1 != 3)
{
option2 = 1;
cout << "1. Directed Graph. \n"
<< "2. Undirected Graph. \n"
<< "3. Exit. \n"
<< "Selection: ";
cin >> option1;
if(option1 == 1)
{
cout << "Enter the number of vertices ";
cin >> vertexNum;
addDirected(vertexNum);
while(option2 == 1)
{
cout << "Please enter a starting vertex for the algorithm ";
cin >> option;
prim(option);
cout << "Press 1 to run the algorithm with a different starting vertex or 2 to go back to the main menu ";
cin >> option2;
}
}
else if(option1 == 2)
{
cout << "Enter the number of vertices ";
cin >> vertexNum;
addUndirected(vertexNum);
while(option2 == 1)
{
cout << "Please enter a starting vertex for the algorithm ";
cin >> option;
prim(option);
cout << "Press 1 to run the algorithm with a different starting vertex or 2 to go back to the main menu ";
cin >> option2;
}
}
}
return 0;
} |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
So exactly what do you want, besides egg in your beer and a finished product? You've told us what you HAVE, and what you NEED, and what you have that doesn't WORK, but you haven't said WHY it doesn't work. Does that mean we have to bleed our eyeballs out if we want to lend a hand?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
i dont know if i know WHY it doesn't work. I need to make the parameter of the prim method the value of the column of the matrix for the recursive call but how do i get to just the column? I think once i figure how to make the column with the lowest value the new parameter for prim it should work because if i say 1 for the starting vertex and then pass it 2 the second time it does those two correctly. So now i just have to change that 2 to the value of the column that has the smallest value in the starting row. If that makes sense?
|
|
|
|
|
|
#8 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
from what i understand each vertex would be like a node and then a weight in between the 2 nodes and that all together makes an edge of a graph. But instead of having nodes i have a 2d array with the row as the first node and then the column as the second node and then the value stored at that spot is the weight connecting the two nodes. So then i take the node to start at from the user and go to that row in the 2d array and then go through that row looking at each number and storing the lowest number. Then where ever i found that lowest number i have to take the column and go to that row. Then i repeat the process until size is reached. The 2d array will always have the same number of rows and columns.
|
|
|
|
|
|
#10 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|