Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Top sort

Name: Anonymous 2011-11-29 10:45

tried to rough code one for an assignment and well it's past due date and I never managed to get it working

http://pastebin.com/NRQ8cXbp

any ideas how i fucked this up?

Name: Anonymous 2011-11-29 10:48

also it's very shittily coded, but I prefer to start with shit get it to work then refine it, so excuse a few of the bad practices

Name: Anonymous 2011-11-29 10:53

>>2
No, I will not excuse it. Not even indenting your code is inexcusable.

Also, you are deluded, no matter how you refine your shit, it will remain shitty, in a large part because you will remain a shitty programmer yourself.

Name: Anonymous 2011-11-29 10:55

>>3
it is indented...

Name: Anonymous 2011-11-29 11:24

>>4
Not all of it! For example, there's a call to exist function standing out like an eyesore.

Name: Anonymous 2011-11-29 19:38

'if (check == true)
      'check = false;

Name: Anonymous 2011-11-30 1:17

123-127 missing if braces causing loop to terminate after 1 pass, but that isn't your main problem

Name: Anonymous 2011-11-30 3:02

Pasting the code here, so we can laugh at him even if >>1 removes his shitty code from pastebin.

Also saluting myself in the future necromancy of this thread.

#include <iostream>
#include<fstream>
#include<string>
using namespace std;


struct prev;
int verts = 0; //# of verts in the graph
int edge = 0;  //# of edges in the graph
int coun = 0;  //use it for various purposes



struct prev{      //called prev because of many changes, vertice
prev() {          //initialize everything to 0
  name = 0;
  incount = 0;
  outcount = 0;
 // checked = false;
}

  int name;  //so we may only have 1 copy
  int incount;   //degree of the vertex
  int outcount;  //misc bookkeeping
  int *nextout;  //where node is pointing
  int *nextin;   //what nodes are pointing to it
 // bool checked;  //for output
};

void checkin(prev *lead);   //sets the incount and puts connecting nodes in nextin
void print(prev *lead);  //prints number
void rm(prev ld, prev *lead);  //decrements incount by 1
void exist(prev *lead, int temp2);  //checks to see if an inode exists
int find(int k, prev *lead);       //looks for position of a number and returns

int main() {


  int temp, temp2;
  bool check = false;
  prev *lead;

  ifstream infile;
  infile.open ("graph.dat");

  infile >> verts;
  infile >> edge;

  lead = new prev[verts];

  //problem with  input causing repitition
  //the temps show the correct numbers
  //and i have a bool condition checking if it exists or not
  //I don't particularly understand...
  while (!infile.eof()) {


    infile >> temp;   //main vertex
    infile >> temp2;  //what that vertex points to

    lead[coun].nextin = new int[verts]; //init in
    lead[coun].nextout = new int[verts]; //init out


    for (int i = 0; i < verts; i++) //go through all nodes
      {

      if (lead[i].name ==  temp) //if a node exists add out node to list and see if the out node exists
        {
        lead[i].nextout[lead[i].outcount] = temp2;
        lead[i].outcount += 1;
        check = true;
        exist(lead, temp2);
  break;  //exit loop because node now exists
        }
    }

    if (check == false) { //if node doesn't exist create it
      for (int i = 0; i < verts; i++) {

        if (lead[i].name ==  0) {
          lead[i].name = temp;
          lead[i].nextout[lead[i].outcount] = temp2;
          lead[i].outcount += 1;
   exist(lead, temp2);

          break;
        }
      }
        }
    if (check == true)
      check = false;
  coun++;
}

  checkin(lead);
  print(lead);

  infile.close();
  return 0;
}

void print(prev *lead) {


    cout << "{ ";
    for (int i=0; i<edge; i++) {
    for (int j = 0; j <verts; j++) {
    if (lead[j].incount == i)
    cout << lead[j].name;


    }

    }
    cout << " }";


}

void exist(prev *lead, int temp2) {
bool checker = false;
    for (int i = 0; i < verts; i++) {
    if (temp2 == lead[i].name)
     checker = true;
     break;
    }
    if (checker == false) {
    for (int i = 0; i < verts; i++) {
    if (0 == lead[i].name)
     lead[i].name = temp2;
    }

    }

}

void checkin(prev *lead) {
  int temp = 0;
  for (int i = 0; i < verts; i++) {
 for (int j = 0; j < lead[i].outcount; j++) {
      temp = find(lead[i].nextout[i], lead);
      lead[temp].nextin[lead[temp].incount] =  lead[i].name;
      lead[temp].incount += 1;
    }
  }

}

int find(int k, prev *lead) {
  for (int i = 0; i < verts; i++) {
    if (k == lead[i].name)
      return i;
  }
}

void rm(prev ld, prev *lead) {

  for (int i = 0; i < verts; i++) {
    for (int j = 0; j < lead[i].outcount; j++) {
      if (ld.name == lead[i].nextin[j] ) {
        lead[i].incount-= 1;
        break;
      }

    }
  }
}



Contents of file:
17  # of verts
22  # of edges
4 5 #starting vert and where it points
8 5
8 9
8 12
5 1
12 15
5 2
5 6
5 10
5 13
5 16
9 13
12 10
12 13
6 7
10 3
10 11
10 14
13 14
13 16
11 7
14 17

Name: Anonymous 2011-11-30 10:51

>>8
I wouldn't remove it, I mean I'm asking for help
so far this >>7 and line 142 nextout[i] should be nextout[j]

but those still aren't the main problems

Name: Anonymous 2011-12-01 0:19

what the fuck is a top sort? is it like topography?

Name: Anonymous 2011-12-01 4:13

>>10
Topological sort.

Name: Anonymous 2011-12-01 21:36

nope

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List