Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,423 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,975 people online right now. Registration is fast and FREE... Join Now!




display the array in sorted(ascending) order

 
Reply to this topicStart new topic

display the array in sorted(ascending) order, need help plss

SPS
19 Jan, 2008 - 09:45 AM
Post #1

New D.I.C Head
*

Joined: 18 Jan, 2008
Posts: 4

this is my code.....its not workin properly...pls help



CODE

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],temp;

for(int i=0;i<=9;i++)
{
cout<<"enter the value"<<(i+1);
cin>>a[i];
}
for(int k=0;k<=9;++k)
{
for(int j=0;j<=k;j++)
{
    if(a[j]>a[j+1])
    {
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
    }
}
}
cout<<"the array in sorted order is";
for(int p=0;p<=9;++p)
cout<<a[p];

cout<<"\n";
getch();

}


User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Display The Array In Sorted(ascending) Order
19 Jan, 2008 - 11:45 AM
Post #2

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
Your code was good but it needed a little modification...

CODE

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    const int ARRAY_SIZE = 10; // size of array a
    int a[ARRAY_SIZE];

    int temp; // temporary location used to swap array elements

    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        cout << "Enter value #" << (i + 1) << " ";
        cin >> a[i];
    }

    // bubble sort
    // loop to control number of passes
    for (int k = 0; k < ARRAY_SIZE - 1; ++k)

        // loop to control number of comparisons per pass
        for(int j = 0; j < ARRAY_SIZE - 1; ++j)

            // compare side-by-side elements and swap item
            // if first element is greater than second element
            if(a[j] > a[j+1])
            {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        
    

    cout << "The sorted array in ascending order\n";
    // output sorted array
    for (int p = 0; p < ARRAY_SIZE; ++p)
        cout << "  " << a[p];

    cout << endl;

    _getch ();

    return 0;
}


This post has been edited by Max_Payne: 19 Jan, 2008 - 11:51 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:32AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month