Numbers in Ascending Order in C++

  • Context: C/C++ 
  • Thread starter Thread starter mdnazmulh
  • Start date Start date
  • Tags Tags
    C++ Numbers
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to take 10 integers from the user and display them in ascending order. Participants explore various sorting techniques and programming practices relevant to this task.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks help on sorting integers without making the code lengthy, expressing difficulty in implementing sorting logic.
  • Another suggests using the standard library function qsort, questioning whether the participant is required to implement a sorting algorithm manually.
  • A participant proposes bubble sort as a simple method, explaining the basic logic of swapping adjacent numbers until sorted.
  • Some participants recommend using the STL vector and the sort() function for a more efficient approach.
  • One participant mentions that the output will not display correctly unless the appropriate output statements are included, suggesting a loop for printing the sorted array.
  • Another participant points out the deprecated use of iostream.h and recommends using the standard iostream header instead.
  • A participant shares a complete code snippet that includes input, sorting using bubble sort, and output, but contains some issues with the output logic.
  • One participant provides a code example that includes error handling for array size input and sorting logic, but also has output issues.

Areas of Agreement / Disagreement

There is no consensus on the best sorting method, as participants suggest different approaches including bubble sort, qsort, and STL sort. Additionally, there are varying opinions on code structure and practices, particularly regarding header files and output methods.

Contextual Notes

Some participants express uncertainty about the requirements of the programming class, which affects their choice of sorting algorithm. There are also unresolved issues related to output formatting and the use of deprecated headers.

Who May Find This Useful

New C++ learners, those interested in sorting algorithms, and individuals seeking guidance on basic programming practices in C++ may find this discussion beneficial.

mdnazmulh
Messages
51
Reaction score
0
i have to write such a program that takes 10 integers from user and show them in ascending order.

My attempt was ---

#include<iostream.h>
#include<conio.h>

int main()
{
int a[10];

for(int i=0; i<10; i++) //this loop is used for getting the 10 integers from user
cin>>a;

...... // here I'm stuck. I couldn't find how should i sort them. I can sort
// number using 'if..else if' statement which makes the program lengthy
// for this propblem.

getch();
return 0;
}

So please help me if anyone knows how to sort 10 integers so that it doesn't make the codes lengthy.
thanks in advance
 
Technology news on Phys.org
Why not just use qsort? Or are you required to actually write the low-level sorting algorithm directly? If so, I'd look at the Wikipedia pages on qsort and heapsort for some ideas; they have pretty good explanations and psuedocode.
 
A bubble sort is quick and easy to code. Simple go through your list swaping adjacent numbers if they are out of order. Repeat until there are no swaps.
 
If you are allowed to use the stl and the vector container, try sort().
 
thanks to all replied...Actually I'm newbie in c++. I haven't studied qsort, heapsort or vector container..
swapping technique is suitable for my level...I used it in the for loop

for(int p=0; p<9; p++)
{
for(int i=0; i<9; i++)
{
if(a>a[i+1]) // comparison between two adjacent array elements
{
holder=a; // holder holds the value temporarily
a=a[i+1];
a[i+1]=holder;
}
}
}
 
Depends what sort of class this is !
If you are supposed to write the sort then bubble sort is easiest - justify your choice over a qsort by saying that the speed difference is small for only 10numbers !
If this is a real programming class then use.

vector<int>a;
for( ...
a.push_back( input )
}

sort(a.begin(),a.end());
 
obviously it vl not show the output coz nobody written count<<a;
line anywhere
i tried to write it at d end of all for loops bt it shows address of a integer
 
akhil12123 said:
count<<a;
You'd need a loop and use count<<a; .
 
mdnazmulh, do not use the header iostream.h. It is deprecated, and not part of standard C++.

Please use the following instead.

Code:
#include <iostream>

And unless you want to prefix standard library names with "std::" use the following.

Code:
using namespace std;

jim mcnamara said:
If you are allowed to use the stl and the vector container, try sort().

std::sort would work with arrays/pointers too.
 
  • #10
Code:
#include <iostream.h>
void main()
{
const int max=50; //sets the maximum size of the array
int num[max]; //the array can be initialized from here num={2,3,6,4,3,7,8,,4...n};
int holder;
//if array is initialized then no need for this for loop
  for(int m=0; m<max; m++)
  {
  cout<<"Enter number "<<(m+1)<<" : ";
  cin>>num[m];
  }


  for(int p=0; p<(max-1); p++)
	{
	 for(int i=0; i<(max-1); i++)
	 {
		if(num[i]>num[i+1]) // comparison between two adjacent array elements
		{
		 holder=num[i]; // holder holds the value temporarily
		 num[i]=num[i+1];
		 num[i+1]=holder;
		}
	 }
  }
  //now to print the sorted array
  for(int j=0; j<max; j++)
  {
  cout<<" "<<num[j];        //This prints the array out in ascending order on the same line
                       //<<endl; could be placed at the end to print on different line
  }
}
 
Last edited by a moderator:
  • #11
#include <iostream.h>
#include<conio.h>

void main ( )
{
int arr[100] , size , hold ;
START :
count << "\n Enter the size of the array ( not more than 100 ) : " ;
cin >> size ;
if ( size > 100 )
{
count << "\n Size is more than 100 ... Please re enter .. " ;
getch ( ) ;
clrscr ( ) ;
goto START ;
}
count << "\n Enter " << size << "elements ... \n" ;
for ( int i = 0 ; i < size ; i ++ )
cin >> arr;
for ( i = 0 ; i < (size-1) ; i++ )
{
for ( int j = i+1 ; j < size ; j++ )
{
if ( arr[j] < arr )
{
hold = arr[j] ;
arr[j] = arr ;
arr = hold;
}
}
}
count << "\n The sorted array : \n ";
for ( i=0 ; i < size ; i++ )
count << arr[size] << " ";
getch ( ) ;
}
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
20
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K