Trouble sorting an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spano693
    New Member
    • Feb 2012
    • 14

    #1

    Trouble sorting an array

    My program is supposed to create a matrix and add edges and vertices to the matrix, but I have one function, degreeSequence( ), that takes the array sequence and is supposed to output the degree sequence of the matrix. However, when my function goes to sort it and output it, it turns all the values that are supposed to be 2 into 55. Can anyone help?

    Code:
    int* graphType::degreeSequence()
    //Function: returns array of integers representing the degree sequence for the graph
    //Pre: graph and array are initialized
    //Post: returns degree sequence array
    {
        for(int i = 0; i < numberOfVertices; i++){ //loop to order array from largest to smallest
            for(int j = 1; j < numberOfVertices; j++){
                if(sequence[i] < sequence[j]){
                    int placeholder; //holds value of sequence[i]
                    sequence[i] = placeholder;
                    sequence[i] = sequence[j];
                    sequence[j] = placeholder;
                }
            }
        }
    
        for(int i = 0; i < numberOfVertices; i++){
            cout << sequence[i] << '\t';
        }
        cout << endl;
        return 0;
    }
    That's my function that sorts and outputs the array

    thank you in advance!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The trouble is here:
    Code:
    sequence[i] = placeholder;  <-----!!!
                     sequence[i] = sequence[j];
                     sequence[j] = placeholder;

    Comment

    • spano693
      New Member
      • Feb 2012
      • 14

      #3
      what do I do to correct it?

      Comment

      • spano693
        New Member
        • Feb 2012
        • 14

        #4
        Never mind I'm an idiot. I read right over that. Thank you very much.

        Comment

        Working...