Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Bubble array - sorting numbers Posted by imattb on 26 Jul 2012 at 11:12 PM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.