I am supposed to sort an array in ascending order..I have tried but then i cant get the results i want.I also tried to look it up in this forum but i still can't get it...Here is part of the code:

void sort()
{
	int size=10;
	int j,temp,i;
	for(i=0;i<(size-1);i++)
	{		
		for(j=i+1;j<size;j++)
		{
			if(queue[i]>queue[j])
			{
				temp=queue[i];
				queue[i]=queue[j];
				queue[j]=temp;
			}
			
		}
	}
	cout<<"Status: sorting done."<<endl;
	
 }

The result i getting is zero for everything...Please help me..

Member Avatar for iamthwee

Where are you passing the contents of queue into your function?

Unless you're declaring it as global.
Also where is your print bit?

ok here is the whole code

# include<iostream>
using namespace std;


int queue[10];
int lastElement=-1;
void sort()
{
	int size=10;
	int j,temp,i;
	for(i=0;i<(size-1);i++)
	{		
		for(j=i+1;j<size;j++)
		{
			if(queue[i]>queue[j])
			{
				temp=queue[i];
				queue[i]=queue[j];
				queue[j]=temp;
			}
			
		}
	}
	cout<<"Status: sorting done."<<endl;
	
 }


void show(int size)
{
	int a=-1;
	for(int b=0;b<=lastElement;b++)
	{
		cout<<"Item "<<b<<": "<<queue[++a]<<endl;
	}
}
void subno(void)
{
	int sub=0,b=0,c=0;
	cout<<"Status: Last number successfully removed."<<endl;
	if(lastElement!=0)
	{
	queue[--lastElement];
	cout<<queue[0]<<queue[1]<<queue[2]<<queue[3];

	}
}
void addno(void)
{
	
	int b=0;
		cout<<"Enter number to add: ";
		cin>>queue[++lastElement];
		cout<<queue[0]<<queue[1]<<queue[2]<<queue[3];
}
void main(void)
{
	int b=10,c=0,e=0;
	int a=0;
	
		do
		{
			cout<<"Queue system [Free:"<<b<<"]"<<endl;
			for(int f=1;f<36;f++)
			{
				cout<<"=";
			}
			cout<<endl;
			cout<<"1. Add number to the end of queue."<<endl;
			cout<<"2. Remove number from end of queue."<<endl;
			cout<<"3. sort the queue in ascending order."<<endl;
			cout<<"4. show queue content."<<endl;
			cout<<"5. Quit."<<endl;
			cout<<"choice: ";
			cin>>a;
			cout<<endl;
		
		if(a==1)
		{
			addno();
			b--;
		}
		else if(a==2)
		{
			subno();
			b++;
		}
		else if(a==3)
		{
			sort();
		}
		else if(a==4)
		{
			show(10);
		}

		}
	
while(b!=0);

}
Member Avatar for iamthwee

And what happens if you change:

void sort()
{
  int size = 10;
  int j, temp, i;
  for ( i = 0; i < ( size - 1 ); i++ )
  {
    for ( j = i + 1; j < size; j++ )
    {
      if ( queue[i] > queue[j] )
      {
        temp = queue[i];
        queue[i] = queue[j];
        queue[j] = temp;
      }
    }
  }
  cout << "Status: sorting done." << endl;

  for ( int i = 0 ; i < 10; i++ )
  {
    cout << queue[i] << " ";
  }
}

erm i get all zeros at the start then i get the number i keyed in but the numbers are sorted out.

i still cannot solve the zeros problem...

Nothing wrong with the program. I tried it at my end.
Enter all ten array elements and then try.
If you do'nt want to add all the elements then

In the show() function change the line
for(int b=0;b<=lastElement;b++)
to for(int b=0;b<size;b++)

------ Pradhan

comment out both of these lines:

cout<<queue[0]<<queue[1]<<queue[2]<<queue[3];

in sort() change this:

int size=10;
nt j,temp,i;
for( i= 0; i < size - 1; i++)
{		
  for(j = i+1; j < size; j++)

to this:

int j, temp, i;
for(i = 0; i < lastElement; i++)
{		
   for(j = i+1; j <= lastElement; j++)

change show(int size) to show()
Then you should be able to work with any size queue having between 1 and 10 elements.
There are other changes I think you should make, too, but that should make it funtional enough for now.

Ahh Thanks alot guys...It solved the zero problem...

Here you go:

#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cout<<"Enter number of elements you want to take in array: ";
  cin>>n;
  cout<<"\nEnter array elements:\n";
  int arr[n];
  for(int i=0;i<n;i++)
  {
    cin>>arr[i];
  }
  // Inbuit Sort function
  sort(arr,arr+n);
  cout<<"\nArray after sorting in increasing order:\n";
  for(int i=0;i<n;i++)
  {
    cout<<arr[i]<<" ";
  }
  return 0;
}

Reference: sort array in increasing order using C++ inbuilt function

all this is a long process. declare you array and just get it fill
arry(7) as integer
// then sort it this way
array.sort(arry)

sort() - sort arrays in ascending order.
rsort() - sort arrays in descending order.
asort() - sort associative arrays in ascending order, according to the value.
ksort() - sort associative arrays in ascending order, according to the key.
arsort() - sort associative arrays in descending order, according to the value.

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.