473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. int* graphType::degreeSequence()
  2. //Function: returns array of integers representing the degree sequence for the graph
  3. //Pre: graph and array are initialized
  4. //Post: returns degree sequence array
  5. {
  6.     for(int i = 0; i < numberOfVertices; i++){ //loop to order array from largest to smallest
  7.         for(int j = 1; j < numberOfVertices; j++){
  8.             if(sequence[i] < sequence[j]){
  9.                 int placeholder; //holds value of sequence[i]
  10.                 sequence[i] = placeholder;
  11.                 sequence[i] = sequence[j];
  12.                 sequence[j] = placeholder;
  13.             }
  14.         }
  15.     }
  16.  
  17.     for(int i = 0; i < numberOfVertices; i++){
  18.         cout << sequence[i] << '\t';
  19.     }
  20.     cout << endl;
  21.     return 0;
  22. }
  23.  
That's my function that sorts and outputs the array

thank you in advance!
Feb 20 '12 #1

✓ answered by weaknessforcats

The trouble is here:
Expand|Select|Wrap|Line Numbers
  1. sequence[i] = placeholder;  <-----!!!
  2.                  sequence[i] = sequence[j];
  3.                  sequence[j] = placeholder;
  4.  

3 1613
weaknessforcats
9,208 Expert Mod 8TB
The trouble is here:
Expand|Select|Wrap|Line Numbers
  1. sequence[i] = placeholder;  <-----!!!
  2.                  sequence[i] = sequence[j];
  3.                  sequence[j] = placeholder;
  4.  
Feb 20 '12 #2
what do I do to correct it?
Feb 20 '12 #3
Never mind I'm an idiot. I read right over that. Thank you very much.
Feb 20 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
25
by: Allie | last post by:
How would I go about sorting this structure by title? typedef struct { char author; char title; char code; int hold; int loan; } LIBRARY;
4
by: Andrew Poulos | last post by:
Say I have two arrays with the same number of elements that are related: var a1 = ; var b2 = ; I need to sort a1 but have the order of the elements in b2 reflect the any new order in a1. So...
0
by: Bomac8 | last post by:
I have a text file with a four field array like this: DET-01-002737,DET-01-002737,YES,64239764b32fefc915a593f41bdb5730. My program sorts them in the order where the 3rd field says "Yes" or "NO". ...
4
by: VooDoo | last post by:
Hi, I am getting confused with sorting arrays... $arraytest =array(5) { =string(2) "39" =string(2) "44" => string(2) "77" =string(3) "150" =string(3) "464" } why do i get NULL value if i try to...
5
by: Tartifola | last post by:
Hi, I'm working with numerical array and I'm a little lost on a particular sorting of one of them. In particular I have an array like a = array(,]) and I need to sort it using only the first...
4
by: spl | last post by:
I have an array of objects. I want to sort the array of a given numeric value that exists in each of the objects. Suppose the array of objects is called student and the numeric value I want to...
12
by: dillipkumar | last post by:
Hi, I have a Hash: %hash= 'student' => , I want to sort the hash according to sorting of 'roll_no' .. After sorting It should be displayed %hash= 'student' => ,
3
by: dondigital | last post by:
I'm trying to figure out the best approach to grab data from an array. So heres the deal. I have byte array called curveData filled will hex bytes that I've downloaded from a unit under test....
6
by: tc18a | last post by:
I'm looking for a fast algorithm for sorting an array of integers with respect to a master list. For example: Master List: 7 3 9 1 32 6 5 4 99 100 201 13 Array that needs to be sorted array1:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.