Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 19th, 2006, 3:17 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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?
cwl157 is offline   Reply With Quote
Old Apr 19th, 2006, 4:02 PM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Apr 19th, 2006, 4:05 PM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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.
cwl157 is offline   Reply With Quote
Old Apr 19th, 2006, 4:44 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Apr 19th, 2006, 5:07 PM   #5
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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;
}
cwl157 is offline   Reply With Quote
Old Apr 19th, 2006, 5:31 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 19th, 2006, 5:45 PM   #7
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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?
cwl157 is offline   Reply With Quote
Old Apr 19th, 2006, 6:29 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
...a starting vertex (the row), ending vertex (the column) and weight all as numbers....
Okay, I don't understand this. To me, a vertex would be a row AND a column, not a row for a starting vertex and a column for an ending vertex. The row number and column number would each represent one of two coordinates of a vertex. I don't know how to help you without a visualization and an explanation of what the expected performance would provide, in terms of a 2D-graphical solution.
__________________
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
DaWei is offline   Reply With Quote
Old Apr 19th, 2006, 7:17 PM   #9
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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.
cwl157 is offline   Reply With Quote
Old Apr 19th, 2006, 8:05 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
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.
Check out this inconsistency: row is one node, column is one node, weight connecting the two nodes is stored at "that" spot. Where is "that" spot? If "that" spot is at array [row][col], then that isn't TWO nodes. Pardon me if I'm being thick here, but I'm not ragging you, I truly don't get it.
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC