Input N numbers in array and sort them in Ascending Order using Function

-- Leave a Comment
Hello Here is A c program to arrange numerical data in array in ascending order (1 3 8..). Input N numbers means you need to ask users how much numbers to be inserted. Here we are using max 10 Numbers if you need more you can from Changing the value a[10]. what ever you need. And you should change it in Called function too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<conio.h>
void asce(int[],int);
void main()
 {
    int a[10],n,i;
      printf("Enter total no of array to be inserted in array:- ");
      scanf("%d",&n);
      printf("Enter value to be inserted in array");
      for(i=0;i<n;i++)
         scanf("%d",&a[i]);
      asce(a,n);
      printf("After arrranging in ascending order:- \n");
      for(i=0;i<n;i++)
         printf("%d\t",a[i]);
      getche();
   }
void asce(int a[10],int y)
 {
    int i,j,tmp;
      for (i=0;i<y;i++)
      {
       for(j=0;j<y-1;j++)
          {
             if(a[j]>a[j+1])
                {
                   tmp=a[j+1];
                   a[j+1]=a[j];
                   a[j]=tmp;
                  }
            }
      }
   }

The main concept is the nested for loop. There are 100s ways to perform a program. The return type of called function is void as value is passed by reference.

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University, NCIT. Open Source Enthusiastic.

0 comments:

Post a Comment