Thursday, December 31, 2009

How do i write a BASIC programme to input 20 N numbers in ascending order?

There are following algorithms to sort the numbers:





■ Bubble sort


■ Selection sort


■ Insertion sort


■ Quick sort





To understand the following program you should have the concepts of for-loop, conditional statements, array and structured programming.





--------------------------------------…


Write a program that takes 10 integer values from user and stores them in an integer array, the array is passed to sort(int arrA[],int size) function and the sorted array (in ascending order) should be displayed by using bubble sort algorithms.


--------------------------------------…





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;iostream.h%26gt;





void sort(int arr[],int size);





void main(void)


{


const int size=10;


int arr[size];





clrscr();


cout%26lt;%26lt;';Please enter the 10 integers:\n\n';;





for(int inp=0;inp%26lt;10;inp++)


{cout%26lt;%26lt;';(';%26lt;%26lt;(inp+1)%26lt;%26lt;';) ';;


cin%26gt;%26gt;arr[inp];}





sort(arr,size);





cout%26lt;%26lt;';\nIntegers in ascending order are: ';%26lt;%26lt;endl;


for(int out=0;out%26lt;size;out++)


{cout%26lt;%26lt;arr[out]%26lt;%26lt;'; ';;}





getch();}





void sort(int arr[],int size)


{int swap=0;





for(int i=0;i%26lt;size-1;i++)


{for(int j=1;j%26lt;size;j++)





{if(arr[j]%26lt;arr[j-1])


{swap=arr[j];


arr[j]=arr[j-1];


arr[j-1]=swap;}}}}





* Note: The above program is written for C++. You can get further help from me to transform the above logic for the GWBASIC.

No comments:

Post a Comment