Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Bubble array - sorting numbers

$
0
0
So I'm having a little trouble when I run my program, to sort the numbers from greatest to highest, when it hits 0 at the end it push it some where and out put some random integer...


/* 
 * File:   main.cpp
 * Author: Matt
 *
 * Created on July 26, 2012, 12:48 PM
 */

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char** argv) {
    
    int Hold;//Holds value to swap
    int end = 10;
    int length = 10;
    
    
    int sortArray[] = {1, 7, 4, 0, 9, 4, 8, 8, 2, 4};//Sort greatest to lowest
    
    for(int count = length - 1; count > 0; count-- )
    {
        for (int i = 0; i < end; i++)//Goes through sortArray
        {
            if (sortArray[i] < sortArray[i +1])//i = 1 less than i = 7
            {
                Hold = sortArray[i];//Hold = 1          i = 1
                sortArray[i] = sortArray[i +1];//7 = 7
                sortArray[i + 1] = Hold;//7 = 1
            }
        for(int i = 0; i < 10 ; i++)
            {
                cout<< sortArray[i]<<", ";
            }
            cout<<endl;
        }
        end--;
    }
    
//    for(int i = 0; i < 10 ; i++)
//    {
//        cout<< sortArray[i]<<" ";
//    }
//    cout<<endl;
    return 0;
}



thanks in adavance

Viewing all articles
Browse latest Browse all 2703

Trending Articles