Write a algorithm to accept 5 number and sort it in the ascending order using selection sort

#include<iostream.h>
#include<conio.h>
class selectionsort
{
    Int a[100], n, i, j, temp, loc, min ;
    public: void in( ) ;
        void out( ) ;
        selectionsort( )
        {
            Min = a[0] ;
        }
};
void main( )
{
    clrscr( ) ;
    selectionsort ss ;
    ss.in( ) ;
    ss.out( ) ;
    getch( );
}
void selectionsort::in( )
{
    cout<<”enter the elements to the array: “ <<endl ; 
    for( i = 0; i<5; i++)
    {
        cin>>a[i] ;
    }
voidselectionsort::out( )
{
    for(i=0 ; i<5; i++)
    {
        min = a[i] ;
        loc = i ;
        for( j = i + 1; j<5; j++)
            {
if( a[j]<min)
{
    min = a[j] ;
    loc = j ;
}

    if( loc!= 1)
    {
        temp = a[i] ;
        a[i] = a[loc] ;
        a[loc] = temp ;
    }
    cout<<”The number after selection sorting are:” ;
    for(i=0 ; i<5; i++)
    {
        cout<<endl<<a[i] ;
    }
}

is my answer correct guy's need help on this tanx a lot

Please use code tags to post code. Also, did you try it? I suggest hard coding the input. Then show us the output and tell us the expected output.

David

the output should be

Enter the elements of array
8
32
12
85
49

The number after selection sorting are:
8
12
32
49
85

this what I expected from the above algorithm (do I get this from it)

Please use [code] ...code tags... [/code]. They make your posted code easier to read.

>>(do I get this from it)
Have you compiled and run it? You should be able to answer this question yourself.

A couple of things I noticed:
Your main() is declared void. It should be declared int and return (0) at the end of the program.

You are using the conio.h header. You really shouldn't. This header is NOT a standardized header and its implementation can vary drastically from one compiler to another, if it's even available.

I'm trying to figure out why you did this:

class selectionsort
{
Int a[100], n, i, j, temp, loc, min ;
public: void in( ) ;
void out( ) ;
selectionsort( )
{
Min = a[0] ;
}
};

Why would you do it as a class instead of just a function?

EDIT:
I also not see that your array is declared as " Int " instead of " int ". No wonder you haven't tested this yourself, the syntax is not correct so you can't compile it. You have to keep in mind that C++ is case-sensitive. In C++ "aVariable", "Avariable", "AVARIABLE", "AVariable", and "avariable" are all considered unique/different names. If you want to declare an integer, you must declare it " int " because " Int " is not the same.

Please use code tag. And this is a problem

Int a[100], n, i, j, temp, loc, min ;

I don't know if you can declare vectors and int's on the same line but I've never seen it done that way before. Also "Public: " should go before your parameter declaration.

>>I don't know if you can declare vectors and int's on the same line but I've never seen it done that way before. Also "Public: " should go before your parameter declaration.
It's a little ...odd... but you can. Also, it's an array, not a vector. There is a significant difference, they are not interchangeable terms.

The bigger problem here is the capitalization. There is nothing wrong with the placement of the "public:" access qualifier. The location is a little obscure from a readability standpoint, but from an OOP design standpoint, it's completely correct.

I thought c++ use vectors, are you talking about vector on stack vs vector on heap?

>>I thought c++ use vectors, are you talking about vector on stack vs vector on heap?
No, I am not. Your "thought" is partially true. C++ uses both arrays and vectors. A vector is like an array, but it's enhanced with additional functionalities. The vector has an array inside of it and defines functions to provide semi-dynamic functionality(ies). If given the choice between a vector and an array, you should choose the vector, but it is not the only such construct available.

int myArray[100] = {0}

This is an array of 100 integers, initialized to (0).

vector<int> myVec(100, 0);

This is a vector of 100 integers, all initialized to (0).

Thank you for the clarification. I think the confusion comes from other programing languages (java) that uses "arrays" but provide automatic functionalities such as resize().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.